python实现移动木板小游戏
作者:weixin_44313115 发布时间:2022-03-28 17:21:32
标签:python,移动木板
本文实例为大家分享了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()
来源:https://blog.csdn.net/weixin_44313115/article/details/108940295
0
投稿
猜你喜欢
- 这篇文章主要介绍了python打包成so文件过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- 代码如下:SELECT * FROM Orders WHERE OrderGUID IN('BC71D821-9E25-
- 使用MySQL,安全问题不能不注意。以下是MySQL提示的23个注意事项:1.如果客户端和服务器端的连接需要跨越并通过不可信任的网络,那么就
- 如果我们想对一个表的每一行做出比较复杂的操作,大多会想到用游标,本文中,我们将换一种思路,用SQL Server 2005中的新函数ROW_
- http通过StreamingHttpResponse完成连续的数据传输长链接问题http服务之间传递结果流一个由flask封装起来的算法,
- 静态方法:将下面的代码复制到<body>~</body>内 程序代码 <table cellpadd
- 网站设计时,有一个最常用的指导性原则:页面长度原则上不超过3屏,宽度不超过1屏。这个原则明显是从用户的体验出发,特别是宽度不超过一屏,其最基
- string.Template()string.Template()内添加替换的字符, 使用"$"符号, 或 在字符串内
- 下文要实现的可视化大屏内容:需要在地图上将我司船舶的轨迹展示出来。听起来很简单,一开始我也是这样想的。通过一些BI工具应该可以轻松实现,比如
- 一、背景故事圣诞节风波马上不就到圣诞节了嘛,我看到朋友圈里很多小伙伴再纷纷炫耀自己收到的专属圣诞树,也有小伙伴私信我,说还没有自己的圣诞树!
- 实现步骤:图像灰度化边缘检测根据Canny检测得出来的Mat寻找轮廓算出最大轮廓周长or面积根据获取到的最大轮廓下标进行轮廓绘制画出最大矩形
- 感觉这个东西很常用,很多其它js框架里有,但是太大了,用不了太多,自己写了个。需要的朋友可以参考下。下面是html测试代码:<!DOC
- Windows下采用PyInstall将py文件转换成exe可执行文件好不容易写完的py文件,想做成exe文件,最开始选择用py2exe,结
- 栈是一种线性数据结构,用先进后出或者是后进先出的方式存储数据,栈中数据的插入删除操作都是在栈顶端进行,常见栈的函数操作包括empty()&n
- 浏览器经常Cache你的页面,这是一个很麻烦的问题,下面先提出几种方案来解决一般的问题:(把下面的代码加入到asp程序的最开始位置)<
- Web标准的web UI——来源、谬误与个人理解序我从2004年末开始接触web标准,2005年5月正式采取完全的web标准方式的网页制作,
- 本文实例为大家分享了python实现图片转换成素描和漫画格式的具体代码,供大家参考,具体内容如下原图图片转换后的成果源码# -*- codi
- flask-login 模块简介Flask-Login 是一个 Flask 模块,可以为 Flask 应用程序提供用户登录功能。要在 Fla
- 没有多大变动,主要是返回错误信息,以便调用函数部分可以alert出来。据说可以用正则表达式校验,下次再研究下。//-------------
- ASP从发布至今已经7年了,使用ASP技术已经相当成熟,自从微软推出了ASP.NET之后就逐渐停止了对ASP版本的更新。但是由于有很多人仍然