圆月山庄资源网 Design By www.vgjia.com
本文实例为大家分享了python实现移动木板小游戏的具体代码,供大家参考,具体内容如下
一、游戏简介
本游戏是通过python编写的小游戏,给初学者熟悉python编程语言抛砖引玉,希望有所帮助。
成型的效果图如下:
二、编写步骤
1.引入库
代码如下:
###### AUTHOR:破茧狂龙 ###### ###### DATE:20201002 ###### ###### DESCRIPTION:移动的木板 ###### import pygame from pygame.locals import * import sys import time import 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) #comicsansms midlText = pygame.font.SysFont('SimHei', 50) barsize = [30, 10] SCREEN_SIZE = [400, 500] # 屏幕大小 BALL_SIZE = [15, 15] # 球的尺寸 fontcolor = (255,255,255) # 定义字体的颜色 myimg = r"img\b1.jpg" background = pygame.image.load(myimg) # 图片位置 background = pygame.transform.scale(background, SCREEN_SIZE) # ball 初始位置 ball_pos_x = SCREEN_SIZE[0] // 2 - BALL_SIZE[0] / 2 ball_pos_y = 0 # ball 移动方向 ball_dir_y = 1 # 1:down ball_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 pygame from pygame.locals import * import sys import time import random 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) #comicsansms midlText = pygame.font.SysFont('SimHei', 50) barsize = [30, 10] SCREEN_SIZE = [400, 500] # 屏幕大小 BALL_SIZE = [15, 15] # 球的尺寸 fontcolor = (255,255,255) # 定义字体的颜色 myimg = r"img\b1.jpg" background = pygame.image.load(myimg) # 图片位置 background = pygame.transform.scale(background, SCREEN_SIZE) # ball 初始位置 ball_pos_x = SCREEN_SIZE[0] // 2 - BALL_SIZE[0] / 2 ball_pos_y = 0 # ball 移动方向 ball_dir_y = 1 # 1:down ball_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,移动木板
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月02日
2024年11月02日
- 《暗喻幻想》顺风耳作用介绍
- 崔健1985-梦中的倾诉[再版][WAV+CUE]
- 黄子馨《追星Xin的恋人们2》HQ头版限量编号[WAV+CUE]
- 孟庭苇《情人的眼泪》开盘母带[低速原抓WAV+CUE]
- 孙露《谁为我停留HQCD》[低速原抓WAV+CUE][1.1G]
- 孙悦《时光音乐会》纯银CD[低速原抓WAV+CUE][1.1G]
- 任然《渐晚》[FLAC/分轨][72.32MB]
- 英雄联盟新英雄安蓓萨上线了吗 新英雄安蓓萨技能介绍
- 魔兽世界奥杜尔竞速赛什么时候开启 奥杜尔竞速赛开启时间介绍
- 无畏契约CGRS准星代码多少 CGRS准星代码分享一览
- 张靓颖.2012-倾听【少城时代】【WAV+CUE】
- 游鸿明.1999-五月的雪【大宇国际】【WAV+CUE】
- 曹方.2005-遇见我【钛友文化】【WAV+CUE】
- Unity6引擎上线:稳定性提升、CPU性能最高提升4倍
- 人皇Sky今日举行婚礼!电竞传奇步入新篇章