电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

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 編程
相關文章:
主站蜘蛛池模板: 微型驱动系统解决方案-深圳市兆威机电股份有限公司 | 天津热油泵_管道泵_天津高温热油泵-天津市金丰泰机械泵业有限公司【官方网站】 | 危废处理系统,水泥厂DCS集散控制系统,石灰窑设备自动化控制系统-淄博正展工控设备 | 烟台金蝶财务软件,烟台网站建设,烟台网络推广 | 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | SDG吸附剂,SDG酸气吸附剂,干式酸性气体吸收剂生产厂家,超过20年生产使用经验。 - 富莱尔环保设备公司(原名天津市武清县环保设备厂) | 宁夏档案密集柜,智能密集柜,电动手摇密集柜-盛隆柜业宁夏档案密集柜厂家 | KBX-220倾斜开关|KBW-220P/L跑偏开关|拉绳开关|DHJY-I隔爆打滑开关|溜槽堵塞开关|欠速开关|声光报警器-山东卓信有限公司 | 卧涛科技有限公司科技项目申报公司|高新技术企业申报|专利申请 | 宿松新闻网 宿松网|宿松在线|宿松门户|安徽宿松(直管县)|宿松新闻综合网站|宿松官方新闻发布 | 青岛球场围网,青岛车间隔离网,青岛机器人围栏,青岛水源地围网,青岛围网,青岛隔离栅-青岛晟腾金属制品有限公司 | 顶呱呱交易平台-行业领先的公司资产交易服务平台 | 手术室净化厂家-成都做医院净化工程的公司-四川华锐-15年特殊科室建设经验 | 工业风机_环保空调_冷风机_工厂车间厂房通风降温设备旺成服务平台 | 步进驱动器「一体化」步进电机品牌厂家-一体式步进驱动 | 北京模型公司-军事模型-工业模型制作-北京百艺模型沙盘公司 | 环压强度试验机-拉链拉力试验机-上海倾技仪器仪表科技有限公司 | 博莱特空压机|博莱特-阿特拉斯独资空压机品牌核心代理商 | 炉门刀边腹板,焦化设备配件,焦化焦炉设备_沧州瑞创机械制造有限公司 | 猎头招聘_深圳猎头公司_知名猎头公司| 一体化隔油提升设备-餐饮油水分离器-餐厨垃圾处理设备-隔油池-盐城金球环保产业发展有限公司 | 气动隔膜泵-电动隔膜泵-循环热水泵-液下排污/螺杆/管道/化工泵「厂家」浙江绿邦 | 冲击式破碎机-冲击式制砂机-移动碎石机厂家_青州市富康机械有限公司 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 仿古建筑设计-仿古建筑施工-仿古建筑公司-汉匠古建筑设计院 | 透平油真空滤油机-变压器油板框滤油机-滤油车-华之源过滤设备 | 深圳市超时尚职业培训学校,培训:月嫂,育婴,养老,家政;化妆,美容,美发,美甲. | 聚合甘油__盐城市飞龙油脂有限公司 | 车件|铜件|车削件|车床加工|五金冲压件-PIN针,精密车件定制专业厂商【东莞品晔】 | 微型气泵-真空-蠕动-水泵-厂家-深圳市品亚科技有限公司 | 异噻唑啉酮-均三嗪-三丹油-1227-中北杀菌剂厂家 | 免费网站网址收录网_海企优网站推荐平台| 工业胀紧套_万向节联轴器_链条-规格齐全-型号选购-非标订做-厂家批发价格-上海乙谛精密机械有限公司 | 春腾云财 - 为企业提供专业财税咨询、代理记账服务 | 湿地保护| 贵州水玻璃_-贵阳花溪闽兴水玻璃厂| 不锈钢散热器,冷却翅片管散热器厂家-无锡市烨晟化工装备科技有限公司 | 微动开关厂家-东莞市德沃电子科技有限公司 | 不锈钢列管式冷凝器,换热器厂家-无锡飞尔诺环境工程有限公司 | ?水马注水围挡_塑料注水围挡_防撞桶-常州瑞轩水马注水围挡有限公司 | 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司 |