成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術文章
文章詳情頁

python實現移動木板小游戲

瀏覽:2日期:2022-07-09 08:25:07

本文實例為大家分享了python實現移動木板小游戲的具體代碼,供大家參考,具體內容如下

一、游戲簡介

本游戲是通過python編寫的小游戲,給初學者熟悉python編程語言拋磚引玉,希望有所幫助。成型的效果圖如下:

python實現移動木板小游戲

python實現移動木板小游戲

二、編寫步驟

1.引入庫

代碼如下:

###### AUTHOR:破繭狂龍 ############ DATE:20201002 ############ DESCRIPTION:移動的木板 ######import pygamefrom pygame.locals import *import sysimport timeimport random

2.初始化

代碼如下:

pygame.init()BLACK = (0, 0, 0) # 黑色WHITE = (255, 255, 255) # 白色bg_color = (0,0,70) # 背景顏色red = (200, 0, 0)green = (0, 200, 0)bright_red = (255, 0, 0)bright_green = (0, 255, 0)smallText = pygame.font.SysFont(’SimHei’, 20) #comicsansmsmidlText = pygame.font.SysFont(’SimHei’, 50)barsize = [30, 10]SCREEN_SIZE = [400, 500] # 屏幕大小BALL_SIZE = [15, 15] # 球的尺寸fontcolor = (255,255,255) # 定義字體的顏色myimg = r'imgb1.jpg'background = pygame.image.load(myimg) # 圖片位置background = pygame.transform.scale(background, SCREEN_SIZE)# ball 初始位置ball_pos_x = SCREEN_SIZE[0] // 2 - BALL_SIZE[0] / 2ball_pos_y = 0# ball 移動方向ball_dir_y = 1 # 1:downball_pos = pygame.Rect(ball_pos_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])clock = pygame.time.Clock() # 定時器screen = pygame.display.set_mode(SCREEN_SIZE)# 設置標題pygame.display.set_caption(’python小游戲-移動木板’)# 設置圖標image = pygame.image.load(myimg)pygame.display.set_icon(image)

3.相關自定義函數

代碼如下:

###### 自定義函數 ######def button(msg, x, y, w, h, ic, ac, action=None): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if x + w > mouse[0] > x and y + h > mouse[1] > y: pygame.draw.rect(screen, ac, (x, y, w, h)) if click[0] == 1 and action != None: action() else: pygame.draw.rect(screen, ic, (x, y, w, h)) textSurf, textRect = text_objects(msg, smallText) textRect.center = ((x + (w / 2)), (y + (h / 2))) screen.blit(textSurf, textRect)def text_objects(text, font): textSurface = font.render(text, True, fontcolor) return textSurface, textSurface.get_rect()def quitgame(): pygame.quit() quit()def message_diaplay(text): largeText = pygame.font.SysFont(’SimHei’, 115) TextSurf, TextRect = text_objects(text, largeText) TextRect.center = ((screen[0] / 2), (screen[1] / 2)) screen.blit(TextSurf, TextRect) pygame.display.update() time.sleep(2) game_loop()

4.相關自定義函數

代碼如下:

def game_first_win(): intro = True while intro: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() screen.fill(bg_color) ###游戲名稱 TextSurf, TextRect = text_objects(’移動木板’, midlText) TextRect.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 - 70 )) screen.blit(TextSurf, TextRect) ###作者 TextSurf_ZZ, TextRect_ZZ = text_objects(’AUTHOR:破繭狂龍’, smallText) TextRect_ZZ.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 + 30)) screen.blit(TextSurf_ZZ, TextRect_ZZ) button('開始', 60, 400, 100, 50, green, bright_green, game_loop) button('取消', 230, 400, 100, 50, red, bright_red, quitgame) pygame.display.update() clock.tick(15)###### 移動的木板游戲類 ######def game_loop(): pygame.mouse.set_visible(1) # 移動鼠標不可見 ###變量### score = 0 #分數 count_O = 0 #循環的次數變量1 用于統計等級 count_N = 0 #循環的次數變量2 用于統計等級 c_level = 1 #等級 x_change = 0 #移動的變量 x = SCREEN_SIZE[0] // 2 - barsize[0] // 2 y = SCREEN_SIZE[1] - barsize[1] # ball 初始位置 ball_pos_pz = ball_pos while True: bar_move_left = False bar_move_right = False ###當每次滿X分后,升級等級 if count_O != count_N and score % 5 == 0: c_level += 1 count_O = count_N ###### 獲取鍵盤輸入 ###### for event in pygame.event.get(): if event.type == QUIT: # 當按下關閉按鍵 pygame.quit() sys.exit() # 接收到退出事件后退出程序 elif event.type == KEYDOWN: ##按鍵盤Q鍵 暫停 if event.key == pygame.K_q: time.sleep(10) ##左移動 if event.key == pygame.K_LEFT: bar_move_left = True x_change = -30 else: bar_move_left = False ##右移動 if event.key == pygame.K_RIGHT: bar_move_right = True x_change = +30 else: bar_move_right = False if event.key != pygame.K_LEFT and event.key != pygame.K_RIGHT: bar_move_left = False bar_move_right = False ##木板的位置移動 if bar_move_left == True and bar_move_right == False: x += x_change if bar_move_left == False and bar_move_right == True: x += x_change ##填充背景 screen.blit(background, (0, 0)) # (0,0)代表圖片位置起點x 軸 Y軸 ##獲取最新的木板位置,并渲染在前臺 bar_pos = pygame.Rect(x, y, barsize[0], BALL_SIZE[1]) bar_pos.left = x pygame.draw.rect(screen, WHITE, bar_pos) ## 球移動,并渲染在前臺 ball_pos_pz.bottom += ball_dir_y * 3 pygame.draw.rect(screen, WHITE, ball_pos_pz) ## 判斷球是否落到板上 if bar_pos.top <= ball_pos_pz.bottom and ( bar_pos.left <= ball_pos_pz.right and bar_pos.right >= ball_pos_pz.left): score += 1 # 分數每次加1 count_N += 1 elif bar_pos.top <= ball_pos_pz.bottom and ( bar_pos.left > ball_pos_pz.right or bar_pos.right < ball_pos_pz.left): print('Game Over: ', score) return score ## 更新球下落的初始位置 if bar_pos.top <= ball_pos_pz.bottom: ball_x = random.randint(0, SCREEN_SIZE[0] - BALL_SIZE[0]) ball_pos_pz = pygame.Rect(ball_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1]) ######### 顯示游戲等級 ######### TextSurf_lev, TextRect_lev = text_objects('等級 : ' + str(c_level), smallText) TextRect_lev.center = (60, 20) screen.blit(TextSurf_lev, TextRect_lev) ######### 顯示分數結果 ######### TextSurf_sco, TextRect_sco = text_objects('分數 : ' + str(score), smallText) TextRect_sco.center = (60, 50) screen.blit(TextSurf_sco, TextRect_sco) pygame.display.update() # 更新軟件界面顯示 clock.tick(60)

# 三、完整的代碼

代碼如下:

###### AUTHOR:破繭狂龍 ############ DATE:20201002 ############ DESCRIPTION:移動的木板 ######import pygamefrom pygame.locals import *import sysimport timeimport randompygame.init()BLACK = (0, 0, 0) # 黑色WHITE = (255, 255, 255) # 白色bg_color = (0,0,70) # 背景顏色red = (200, 0, 0)green = (0, 200, 0)bright_red = (255, 0, 0)bright_green = (0, 255, 0)smallText = pygame.font.SysFont(’SimHei’, 20) #comicsansmsmidlText = pygame.font.SysFont(’SimHei’, 50)barsize = [30, 10]SCREEN_SIZE = [400, 500] # 屏幕大小BALL_SIZE = [15, 15] # 球的尺寸fontcolor = (255,255,255) # 定義字體的顏色myimg = r'imgb1.jpg'background = pygame.image.load(myimg) # 圖片位置background = pygame.transform.scale(background, SCREEN_SIZE)# ball 初始位置ball_pos_x = SCREEN_SIZE[0] // 2 - BALL_SIZE[0] / 2ball_pos_y = 0# ball 移動方向ball_dir_y = 1 # 1:downball_pos = pygame.Rect(ball_pos_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])clock = pygame.time.Clock() # 定時器screen = pygame.display.set_mode(SCREEN_SIZE)# 設置標題pygame.display.set_caption(’python小游戲-移動木板’)# 設置圖標image = pygame.image.load(myimg)pygame.display.set_icon(image)###### 自定義函數 ######def button(msg, x, y, w, h, ic, ac, action=None): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if x + w > mouse[0] > x and y + h > mouse[1] > y: pygame.draw.rect(screen, ac, (x, y, w, h)) if click[0] == 1 and action != None: action() else: pygame.draw.rect(screen, ic, (x, y, w, h)) textSurf, textRect = text_objects(msg, smallText) textRect.center = ((x + (w / 2)), (y + (h / 2))) screen.blit(textSurf, textRect)def text_objects(text, font): textSurface = font.render(text, True, fontcolor) return textSurface, textSurface.get_rect()def quitgame(): pygame.quit() quit()def message_diaplay(text): largeText = pygame.font.SysFont(’SimHei’, 115) TextSurf, TextRect = text_objects(text, largeText) TextRect.center = ((screen[0] / 2), (screen[1] / 2)) screen.blit(TextSurf, TextRect) pygame.display.update() time.sleep(2) game_loop()def game_first_win(): intro = True while intro: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() screen.fill(bg_color) ###游戲名稱 TextSurf, TextRect = text_objects(’移動木板’, midlText) TextRect.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 - 70 )) screen.blit(TextSurf, TextRect) ###作者 TextSurf_ZZ, TextRect_ZZ = text_objects(’AUTHOR:破繭狂龍’, smallText) TextRect_ZZ.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 + 30)) screen.blit(TextSurf_ZZ, TextRect_ZZ) button('開始', 60, 400, 100, 50, green, bright_green, game_loop) button('取消', 230, 400, 100, 50, red, bright_red, quitgame) pygame.display.update() clock.tick(15)###### 移動的木板游戲類 ######def game_loop(): pygame.mouse.set_visible(1) # 移動鼠標不可見 ###變量### score = 0 #分數 count_O = 0 #循環的次數變量1 用于統計等級 count_N = 0 #循環的次數變量2 用于統計等級 c_level = 1 #等級 x_change = 0 #移動的變量 x = SCREEN_SIZE[0] // 2 - barsize[0] // 2 y = SCREEN_SIZE[1] - barsize[1] # ball 初始位置 ball_pos_pz = ball_pos while True: bar_move_left = False bar_move_right = False ###當每次滿X分后,升級等級 if count_O != count_N and score % 5 == 0: c_level += 1 count_O = count_N ###### 獲取鍵盤輸入 ###### for event in pygame.event.get(): if event.type == QUIT: # 當按下關閉按鍵 pygame.quit() sys.exit() # 接收到退出事件后退出程序 elif event.type == KEYDOWN: ##按鍵盤Q鍵 暫停 if event.key == pygame.K_q: time.sleep(10) ##左移動 if event.key == pygame.K_LEFT: bar_move_left = True x_change = -30 else: bar_move_left = False ##右移動 if event.key == pygame.K_RIGHT: bar_move_right = True x_change = +30 else: bar_move_right = False if event.key != pygame.K_LEFT and event.key != pygame.K_RIGHT: bar_move_left = False bar_move_right = False ##木板的位置移動 if bar_move_left == True and bar_move_right == False: x += x_change if bar_move_left == False and bar_move_right == True: x += x_change ##填充背景 screen.blit(background, (0, 0)) # (0,0)代表圖片位置起點x 軸 Y軸 ##獲取最新的木板位置,并渲染在前臺 bar_pos = pygame.Rect(x, y, barsize[0], BALL_SIZE[1]) bar_pos.left = x pygame.draw.rect(screen, WHITE, bar_pos) ## 球移動,并渲染在前臺 ball_pos_pz.bottom += ball_dir_y * 3 pygame.draw.rect(screen, WHITE, ball_pos_pz) ## 判斷球是否落到板上 if bar_pos.top <= ball_pos_pz.bottom and ( bar_pos.left <= ball_pos_pz.right and bar_pos.right >= ball_pos_pz.left): score += 1 # 分數每次加1 count_N += 1 elif bar_pos.top <= ball_pos_pz.bottom and ( bar_pos.left > ball_pos_pz.right or bar_pos.right < ball_pos_pz.left): print('Game Over: ', score) return score ## 更新球下落的初始位置 if bar_pos.top <= ball_pos_pz.bottom: ball_x = random.randint(0, SCREEN_SIZE[0] - BALL_SIZE[0]) ball_pos_pz = pygame.Rect(ball_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1]) ######### 顯示游戲等級 ######### TextSurf_lev, TextRect_lev = text_objects('等級 : ' + str(c_level), smallText) TextRect_lev.center = (60, 20) screen.blit(TextSurf_lev, TextRect_lev) ######### 顯示分數結果 ######### TextSurf_sco, TextRect_sco = text_objects('分數 : ' + str(score), smallText) TextRect_sco.center = (60, 50) screen.blit(TextSurf_sco, TextRect_sco) pygame.display.update() # 更新軟件界面顯示 clock.tick(60)####程序執行順序######game_first_win()game_loop()pygame.quit()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久
国产一区亚洲| 亚洲午夜精品17c| 日本成人中文字幕| 欧美精品色综合| 欧美福利一区二区三区| 一区二区三区成人在线视频| 欧美日韩综合在线免费观看| 色综合亚洲欧洲| 午夜精品久久久久影视| 精品少妇一区二区三区日产乱码 | 免费久久久一本精品久久区| 成人看片黄a免费看在线| 久久日韩粉嫩一区二区三区| 国产伦精品一区二区三区| 97久久精品人人爽人人爽蜜臀| 亚洲一区二区黄色| 国产欧美在线观看一区| 欧美日韩一区二区欧美激情| 亚洲成人资源| 国产98色在线|日韩| 婷婷综合久久一区二区三区| 国产精品免费av| 91精品欧美福利在线观看| 日韩网站在线| 欧美不卡高清| 丁香激情综合国产| 久久不见久久见免费视频7| 最新热久久免费视频| 在线播放视频一区| 一本高清dvd不卡在线观看| 亚洲性图久久| 午夜欧美视频| 不卡的看片网站| 国产综合色在线视频区| 五月天精品一区二区三区| 亚洲人成小说网站色在线| 中日韩免费视频中文字幕| 精品国产sm最大网站| 欧美日韩在线三区| 欧美视频第二页| 国产伦精品一区二区三区视频黑人 | 色综合久久久久久久| 亚洲国产一区二区三区a毛片| 国产在线精品一区二区夜色| 午夜日韩在线电影| 亚洲一区日韩精品中文字幕| 国产精品久久久久久妇女6080| 欧美一区二区三区免费| 在线视频观看一区| 久久精品三级| 亚洲一区二区三区色| 日韩视频在线播放| 亚洲伦伦在线| 亚洲夜间福利| 欧美激情一区二区三区在线视频| 成人免费看黄yyy456| 国产毛片精品视频| 精品亚洲免费视频| 黄色精品一二区| 蜜臀精品一区二区三区在线观看 | 欧美色老头old∨ideo| 久久久www| 91豆麻精品91久久久久久| 久久国产精品亚洲va麻豆| 先锋影音久久久| 亚洲欧美日韩精品久久久| 米奇777在线欧美播放| 久久资源在线| 欧美日韩色一区| 欧美一区二区福利视频| 日韩一区二区三区视频在线| 欧美男男青年gay1069videost| 欧美视频你懂的| 日韩欧美激情四射| 久久久久97国产精华液好用吗| 久久久亚洲精华液精华液精华液| 国产视频亚洲色图| 亚洲伦在线观看| 青娱乐精品在线视频| 国产精品一二三四| av在线不卡电影| 狠狠色噜噜狠狠狠狠色吗综合| 亚洲精品三级| 在线观看av不卡| 久久综合国产精品| 亚洲精品成人精品456| 日韩影院精彩在线| 丁香一区二区三区| 一区二区亚洲精品| 久久青青草综合| 欧美久久久久久蜜桃| 国产香蕉久久精品综合网| 亚洲午夜精品在线| 国产精品综合av一区二区国产馆| 欧美啪啪一区| 91福利国产精品| 国产婷婷一区二区| 人人超碰91尤物精品国产| 99麻豆久久久国产精品免费优播| 国产精品1区2区3区在线观看| 国产精品国产亚洲精品看不卡15| 狼狼综合久久久久综合网| 欧美电视剧在线看免费| 依依成人精品视频| 成人一区二区三区中文字幕| 99xxxx成人网| 欧洲另类一二三四区| 中文字幕乱码亚洲精品一区| 精品亚洲免费视频| 一区二区三区国产盗摄 | 日韩精品在线看片z| 一卡二卡三卡日韩欧美| 成人av电影在线观看| 色av成人天堂桃色av| 中文字幕乱码亚洲精品一区| 国产一区二区调教| 久久本道综合色狠狠五月| 中文字幕va一区二区三区| 国产一区 二区| 国产日韩一区欧美| 国产色一区二区| 国产一区二区免费在线| 久久综合五月| 亚洲欧美日韩国产综合| 91天堂素人约啪| 日韩一级高清毛片| 日本aⅴ免费视频一区二区三区| 欧美午夜不卡影院在线观看完整版免费| 欧美午夜精品久久久| 日韩国产欧美三级| 久久福利电影| 一区二区三区不卡视频在线观看 | 日韩精品一区二区三区视频播放| 免费一级欧美片在线观看| 一区二区三区国产盗摄| 亚洲欧美另类小说| 亚洲一级影院| 国产欧美日韩在线视频| 国产成人激情av| 欧美日本在线播放| 精品中文字幕一区二区| 雨宫琴音一区二区在线| 在线播放一区二区三区| 蜜臀av性久久久久蜜臀aⅴ流畅| 欧美日韩亚洲综合一区| 国产河南妇女毛片精品久久久 | gogogo免费视频观看亚洲一| 欧美成人性福生活免费看| 成人av影院在线| 国产精品福利在线播放| 亚洲国产精品久久久久久女王 | 久久久久久久久一| 欧美日韩大片一区二区三区| 久久99精品视频| 国产精品日韩二区| 国产精品亚洲一区二区三区在线| 亚洲与欧洲av电影| 国产校园另类小说区| 欧美三级蜜桃2在线观看| 亚洲三级色网| 99精品久久免费看蜜臀剧情介绍| 丝袜美腿一区二区三区| 久久免费视频色| 色欧美乱欧美15图片| 91年精品国产| 国产精品88888| 国产欧美日韩视频一区二区| 欧洲一区二区三区在线| 美女被吸乳得到大胸91| 一区二区中文字幕在线| 色婷婷亚洲精品| 亚洲精品美女91| 亚洲成人av一区| 欧美日韩一区二区三区四区五区| 成人国产电影网| 日韩欧美电影一区| 国内国产精品久久| 国产精品久久免费看| 欧美系列一区二区| 国产精品v欧美精品v日本精品动漫| 日韩av在线免费观看不卡| 中文字幕乱码一区二区免费| 一本色道亚洲精品aⅴ| 日本欧美一区二区| 日韩欧美亚洲国产另类| 午夜精品区一区二区三| 亚洲制服欧美中文字幕中文字幕| 欧美制服丝袜第一页| 粉嫩aⅴ一区二区三区四区| 亚洲视频中文字幕| 色老头久久综合| 国模套图日韩精品一区二区| 国产精品午夜春色av| 欧美日韩在线免费视频| 欧美va天堂在线| 亚洲图片一区二区| 欧美电影免费观看高清完整版| 亚洲天堂男人| 麻豆精品国产91久久久久久| 日韩午夜电影av|