日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

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

python實現移動木板小游戲

瀏覽:22日期: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 編程
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产精品婷婷| 视频一区在线视频| 亚洲va中文在线播放免费| 久久久国产精品一区二区中文| 亚洲欧洲一区二区天堂久久| 热久久久久久| 99精品综合| 日韩av电影一区| 久久高清免费| 欧美日韩伊人| 136国产福利精品导航网址| 涩涩涩久久久成人精品| 欧美丰满日韩| 午夜天堂精品久久久久| 成人在线视频免费| 亚洲乱码久久| 久久精品国内一区二区三区水蜜桃| 亚洲免费毛片| 欧美日中文字幕| 国产精品日韩精品在线播放| 久久婷婷亚洲| 国产亚洲精aa在线看 | 国产一级一区二区| 亚洲欧洲美洲国产香蕉| www成人在线视频| 久久国产精品色av免费看| 美女网站一区| 精品午夜av| 日本成人一区二区| 黄页网站一区| 97国产精品| 国产精品探花在线观看| 久久av在线| 国产精品99一区二区| 欧美激情麻豆| 青青草91视频| 久久99伊人| 91精品国产91久久久久久黑人| 国产免费av一区二区三区| 欧美性感美女一区二区| 国产精品第十页| 日韩欧美美女在线观看| 亚洲爱爱视频| 麻豆高清免费国产一区| 亚洲精品在线国产| 国产精品99免费看| 98精品视频| 久久午夜影院| 国产欧美一区二区三区米奇| 丝袜国产日韩另类美女| 久久国产亚洲| 黑森林国产精品av| 欧美1区2区3| 青青国产91久久久久久| 久久高清精品| 美女精品视频在线| 国产精品宾馆| 欧美国产极品| 欧美国产日本| 国产欧美日韩免费观看| 免费日本视频一区| 香蕉久久夜色精品国产| 91久久国产| 国产一区二区三区四区| 国产亚洲第一伦理第一区| 91精品啪在线观看国产爱臀| 亚欧成人精品| 免费成人性网站| 蜜臀av一区二区三区| 亚洲大片在线| 亚洲第一精品影视| 一区视频在线| 不卡一区综合视频| 欧美成人综合| 亚洲精品888| 国产成人精品三级高清久久91| 久久av免费| 国产精品亚洲综合在线观看| 国产欧美日韩精品高清二区综合区| 亚洲专区视频| 日韩精品免费视频人成| 亚洲免费在线| 蜜桃91丨九色丨蝌蚪91桃色| 亚洲精品极品| 国产精品探花在线观看| 日韩精品久久久久久久电影99爱| 国产亚洲在线观看| 国产精品久久久久久模特| 日韩高清成人| 亚洲啊v在线免费视频| 久久伊人久久| 欧美精品一卡| 国产精品白丝久久av网站| 欧美日韩中文一区二区| 奇米狠狠一区二区三区| 日韩精品91| 日韩高清一区在线| 日韩精品dvd| 亚洲最新av| 日韩一区电影| 亚洲毛片一区| 亚洲天堂免费电影| 日韩精品欧美成人高清一区二区| 中文在线а√天堂| 日韩激情中文字幕| 亚洲网站视频| 久久精品理论片| 免费国产自线拍一欧美视频| 麻豆精品新av中文字幕| 国产精品免费看| 老牛影视精品| 日韩av资源网| 激情久久久久久久| 91p九色成人| 国精品一区二区| 牛牛精品成人免费视频| 三级欧美在线一区| 亚洲黄色中文字幕| 久久精品凹凸全集| 最新国产拍偷乱拍精品| sm捆绑调教国产免费网站在线观看| 日本不卡视频在线| 亚洲激情欧美| 日韩欧美不卡| 欧美激情视频一区二区三区在线播放| 亚洲一区二区三区高清不卡| 国产精选在线| 国产精品羞羞答答在线观看| 老鸭窝毛片一区二区三区| 国产成人精品福利| 国产伦精品一区二区三区视频| 国产精品腿扒开做爽爽爽挤奶网站| 三级在线看中文字幕完整版| 美女国产一区二区三区| 日韩av资源网| 色综合视频一区二区三区日韩| 黄色欧美日韩| 久久久天天操| 成人三级高清视频在线看| 国产午夜精品一区在线观看| 久色成人在线| 伊人成人在线视频| 欧美日韩精品免费观看视完整 | 亚洲免费观看高清完整版在线观| 欧美freesex黑人又粗又大| 国产精品v一区二区三区| 亚洲精品麻豆| 国产精品毛片在线看| 极品裸体白嫩激情啪啪国产精品| 精品中文字幕一区二区三区四区| 青草国产精品| 日韩 欧美一区二区三区| 免费美女久久99| 国产精品日本| 亚洲精品99| 精品中文一区| 欧美成人综合| av亚洲在线观看| 激情综合亚洲| 免费av一区| 亚洲午夜91| 五月天久久网站| 一区视频在线| 三级欧美韩日大片在线看| 丝瓜av网站精品一区二区 | 亚洲欧美综合| 狠狠干综合网| 亚洲永久字幕| 亚洲一区有码| 日韩欧乱色一区二区三区在线| 亚洲精品韩国| 亚洲va久久久噜噜噜久久| 日韩一区二区三区四区五区| 日韩精选在线| 国产精品亚洲综合色区韩国| 国产极品模特精品一二| 你懂的国产精品| 黄色网一区二区| 国产高潮在线| 91精品精品| 最新日韩欧美| 亚洲免费专区| 国产精品高清一区二区| 精品无人区麻豆乱码久久久| 精品国产成人| 婷婷综合六月| 欧美另类专区| 亚洲精品三级| 国产精品一区二区三区四区在线观看 | 精品亚洲精品| 九九色在线视频| 亚洲午夜视频| 亚洲精品影视| 国产精品草草| 成人福利av| 激情五月综合| 天堂俺去俺来也www久久婷婷| 国产伦理久久久久久妇女| 国产一区二区视频在线看| 在线天堂中文资源最新版|