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

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

python實現移動木板小游戲

瀏覽:19日期: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久久,三上悠亚国产精品一区二区三区
日本久久成人网| 美女久久久久久 | 国产精品亲子伦av一区二区三区 | 亚洲小说春色综合另类电影| 亚洲高清成人| 免费av一区| 激情综合网站| 一本色道精品久久一区二区三区| 99精品电影| 亚洲特级毛片| 欧美女激情福利| 老鸭窝毛片一区二区三区| 在线亚洲激情| 亚洲免费一区三区| 日韩一区二区三区四区五区| 国产欧美一区二区三区米奇| 久久精品五月| 国产毛片久久久| 老司机免费视频一区二区| 国产精品白丝久久av网站| sm捆绑调教国产免费网站在线观看| 电影91久久久| 亚洲精品.com| 影音先锋国产精品| 日日夜夜免费精品视频| 国产精品网址| 欧美亚洲激情| 日韩精品久久久久久| 日本成人手机在线| 日韩av在线中文字幕| 国产一区成人| 精品视频久久| 免费国产自久久久久三四区久久 | 日韩在线一二三区| 国产精品qvod| 91九色精品国产一区二区| 日韩午夜视频在线| 色偷偷偷在线视频播放| 久久aⅴ国产紧身牛仔裤| 国产视频一区二| 亚洲午夜精品久久久久久app| 蜜臀精品久久久久久蜜臀| 久久精品国产久精国产爱| 欧美午夜精彩| 麻豆高清免费国产一区| 亚洲精品护士| 老牛影视精品| 日韩精选在线| 欧美日韩水蜜桃| 在线中文字幕播放| 久久久国产精品入口麻豆| 日韩激情av在线| 视频在线在亚洲| www.九色在线| 久久精品午夜| 日本不卡视频一二三区| 巨乳诱惑日韩免费av| 视频二区不卡| 久久精品国产久精国产| 日韩福利在线观看| 亚洲深夜av| 久久天堂av| 日韩精品免费一区二区在线观看 | 91亚洲国产高清| 免费亚洲婷婷| 日韩成人一级| 日韩激情一区二区| 日日摸夜夜添夜夜添国产精品| 久久国产精品久久w女人spa| 亚洲欧美日韩专区| 狠狠爱www人成狠狠爱综合网| 偷拍欧美精品| 亚洲国产专区校园欧美| 欧美精选一区二区三区| 狠狠色综合网| 香蕉久久久久久| 久久精品xxxxx| 精品网站aaa| 日韩在线观看| 国产网站在线| 国产亚洲在线观看| 日韩在线一区二区| 日韩不卡一二三区| 精品少妇av| 激情亚洲影院在线观看| 午夜欧美精品久久久久久久| 蜜桃视频一区二区| 91精品国产自产观看在线 | 麻豆成人av在线| 日韩三区在线| 综合国产精品| 色乱码一区二区三区网站| 日韩av一级| 日韩中文字幕视频网| 久久精品资源| 91九色精品| 国产精品高清一区二区| 久久久夜夜夜| 亚洲免费专区| 国产午夜久久av| 欧美+亚洲+精品+三区| 91精品国产自产精品男人的天堂| 国产一二在线播放| 中文字幕日韩欧美精品高清在线| 六月丁香综合在线视频| 亚洲精选91| 日韩高清成人| 国产精品红桃| 黄色国产精品| jizzjizz中国精品麻豆| 婷婷精品久久久久久久久久不卡| 肉色欧美久久久久久久免费看| 亚洲综合二区| 特黄特色欧美大片| 日韩欧美精品一区二区综合视频| 日韩精品电影| 久久99影视| 日本h片久久| 狠狠色狠狠色综合日日tαg| 国产999精品在线观看 | 精品精品99| 国产精品免费精品自在线观看| 国产一区观看| 久久蜜桃精品| 国产欧美精品| 欧美亚洲专区| 日韩在线视频一区二区三区| 国产偷自视频区视频一区二区| 免费亚洲婷婷| 久久亚洲人体| 日韩av在线播放中文字幕| 亚洲视频二区| 免费国产自线拍一欧美视频| 婷婷国产精品| 久久精品91| 久久人人88| 亚洲国产综合在线看不卡| 久久裸体视频| 国产综合婷婷| 六月丁香综合| 水蜜桃久久夜色精品一区的特点| 日韩视频在线一区二区三区 | 成人福利av| 亚洲午夜久久久久久尤物| 亚洲成人三区| 最新国产精品视频| 综合亚洲视频| 久久国产三级| 麻豆视频一区| 精品一区二区三区亚洲| 久久uomeier| 午夜日韩av| 亚洲一级淫片| 成人在线视频免费看| 91亚洲一区| 免播放器亚洲一区| 国产精品啊v在线| 久久蜜桃资源一区二区老牛| 黄色亚洲精品| 久久av综合| 日韩亚洲在线| 麻豆中文一区二区| 欧美不卡在线| 国产精品亚洲综合在线观看| 国产精品一级| 99精品综合| 国产欧美自拍| 亚洲高清影视| 九九久久国产| 亚洲毛片在线免费| 亚洲一级少妇| 久久国产免费看| 女人天堂亚洲aⅴ在线观看| 日本午夜精品| 国产视频欧美| 日韩免费看片| 久久精品 人人爱| 久久福利一区| 香蕉人人精品| 日本色综合中文字幕| 日韩中文字幕麻豆| 91综合网人人| 你懂的亚洲视频| 日韩精品免费视频一区二区三区| 欧美日韩国产探花| 伊人久久视频| 91欧美在线| 牛牛精品成人免费视频| 亚洲无线观看| 99国产精品| 99久久久久| 久久精品亚洲欧美日韩精品中文字幕| 国产日产一区| 亚洲精品中文字幕99999| 亚洲伦乱视频| 中文字幕一区久| 精品亚洲成人| 国产精品探花在线观看| 91精品一区| 日韩专区欧美专区|