Python Pygame实现落球游戏详解
作者:我的天才女友 时间:2021-06-23 00:54:38
引包
引入对应的包,和原来一样写一个打印文字的方法
import sys, random, pygame
from pygame.locals import *
def print_text(font, x, y, text, color=(255, 255, 255)):
img_text = font.render(text, True, color)
screen.blit(img_text, (x, y))
初始化配置
初始化游戏,pygame窗口的一些信息,以及游戏中用的的一些参数。
pygame.init()
# 定时器
mainClock = pygame.time.Clock()
# 设置屏幕和窗口标题
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("落球游戏")
# 设置字体
font1 = pygame.font.SysFont("方正粗黑宋简体", 24)
pygame.mouse.set_visible(False)
# 设置颜色变量
white = 255, 255, 255
red = 220, 50, 50
yellow = 230, 230, 50
black = 0, 0, 0
# 生命数
lives = 3
# 分
score = 0
# 游戏开始flg
game_over = True
# 鼠标位置
mouse_x = mouse_y = 0
pos_x = 300
pos_y = 460
# 球落下的x和y轴坐标
bomb_x = random.randint(0, 500)
bomb_y = -50
# 球下落的速度
vel_y = 0.7
捕捉事件
捕捉事件,如果游戏结束按下鼠标,则游戏重新开始, mouse_x, mouse_y捕捉鼠标位置, move_x, move_y获取鼠标的偏移
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == MOUSEMOTION:
mouse_x, mouse_y = event.pos
move_x, move_y = event.rel
elif event.type == MOUSEBUTTONUP:
if game_over:
game_over = False
lives = 3
score = 0
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
填充屏幕让球下落
让屏幕填充一个暖色调,如果游戏未开始,屏幕中显示 “点击开始新游戏”, 否则球下落。
如果球大于500,则重置一个新球,生命减去一,如果生命没有了则游戏结束。
这里没有使用元素相碰撞原理,有两个相对的位置,如果球大于挡板的垂直坐标,切球的x坐标大于挡板的开始位置,小于挡板的宽度,则分数添加,重置球的位置。
screen.fill((255, 166, 77))
if game_over:
print_text(font1, 100, 200, "点击新游戏")
else:
bomb_y += vel_y
if bomb_y > 500:
bomb_x = random.randint(0, 500)
bomb_y = -50
lives -= 1
if lives == 0:
game_over = True
elif bomb_y > pos_y:
if bomb_x > pos_x and bomb_x < pos_x + 120:
score += 120
bomb_x = random.randint(0, 500)
bomb_y = -50
绘制球和挡板,绘制屏幕
根据bomb_x,bomb_y来显示球,并绘制一个阴影
矩形一样
显示生命数和分数,更新屏幕,设置每秒的频率为20
这里明显有很大的问题,因为圆绘制的时候点是圆心,所以比较的时候就会出错,如果你用矩形的右部分去接球心左少部分,显示还是接不到,这里我们不深究,简单的游戏是实现了,优化我放在第二部分。
pygame.draw.circle(screen, black, (bomb_x - 4, int(bomb_y) - 4), 30, 0)
pygame.draw.circle(screen, yellow, (bomb_x, int(bomb_y)), 30, 0)
pygame.draw.rect(screen, black, (pos_x - 4, pos_y - 4, 120, 40), 0)
pygame.draw.rect(screen, red, (pos_x, pos_y, 120, 40), 0)
print_text(font1, 0, 0, "生命数: " + str(lives))
print_text(font1, 500, 0, "分数: " + str(score))
pygame.display.update()
mainClock.tick(20)
完整代码
import sys, random, pygame
from pygame.locals import *
def print_text(font, x, y, text, color=(255, 255, 255)):
img_text = font.render(text, True, color)
screen.blit(img_text, (x, y))
pygame.init()
# 定时器
mainClock = pygame.time.Clock()
# 设置屏幕和窗口标题
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("落球游戏")
# 设置字体
font1 = pygame.font.SysFont("方正粗黑宋简体", 24)
pygame.mouse.set_visible(False)
# 设置颜色变量
white = 255, 255, 255
red = 220, 50, 50
yellow = 230, 230, 50
black = 0, 0, 0
# 生命条数
lives = 3
# 分
score = 0
# 游戏开始flg
game_over = True
# 鼠标位置
mouse_x = mouse_y = 0
pos_x = 300
pos_y = 460
# 球落下的x和y轴坐标
bomb_x = random.randint(0, 500)
bomb_y = -50
# 球下落的速度
vel_y = 0.7
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == MOUSEMOTION:
mouse_x, mouse_y = event.pos
move_x, move_y = event.rel
elif event.type == MOUSEBUTTONUP:
if game_over:
game_over = False
lives = 3
score = 0
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
screen.fill((255, 166, 77))
if game_over:
print_text(font1, 100, 200, "点击新游戏")
else:
bomb_y += vel_y
if bomb_y > 500:
bomb_x = random.randint(0, 500)
bomb_y = -50
lives -= 1
if lives == 0:
game_over = True
elif bomb_y > pos_y:
if bomb_x > pos_x and bomb_x < pos_x + 120:
score += 120
bomb_x = random.randint(0, 500)
bomb_y = -50
pygame.draw.circle(screen, black, (bomb_x - 4, int(bomb_y) - 4), 30, 0)
pygame.draw.circle(screen, yellow, (bomb_x, int(bomb_y)), 30, 0)
pos_x = mouse_x
if pos_x < 0:
pos_x = 0
elif pos_x > 500:
pos_x = 500
pygame.draw.rect(screen, black, (pos_x - 4, pos_y - 4, 120, 40), 0)
pygame.draw.rect(screen, red, (pos_x, pos_y, 120, 40), 0)
print_text(font1, 0, 0, "生命数: " + str(lives))
print_text(font1, 500, 0, "分数: " + str(score))
pygame.display.update()
mainClock.tick(20)
来源:https://blog.csdn.net/qq_40801987/article/details/122518043
标签:Python,Pygame,落球游戏
0
投稿
猜你喜欢
使用SQL Server 2008管理非结构化数据
2009-01-08 15:28:00
CSS Position
2009-05-17 14:27:00
pandas读取CSV文件时查看修改各列的数据类型格式
2023-09-26 16:44:58
在laravel中使用Symfony的Crawler组件分析HTML
2023-11-17 18:54:07
我的ImageMagick使用心得
2008-10-21 11:05:00
PyTorch实现图像识别实战指南
2022-01-08 14:27:24
python中torch.nn.identity()方法详解
2021-05-21 16:51:01
asp简单可逆运算字符串加密解密函数
2010-05-04 16:42:00
php之Aes加密案例讲解
2023-06-11 12:59:12
asp中获取内容中所有图片与获取内容中第一个图片的代码
2011-02-20 10:51:00
asp Driver和Provider两种连接字符串连接Access时的区别
2011-03-09 11:19:00
Python装饰器用法实例总结
2023-11-18 07:37:36
JavaScript文档生成工具
2007-10-26 11:59:00
富文本编辑器的基本原理与实践
2008-06-13 13:28:00
asp的日期转换星座函数
2010-06-09 21:05:00
python tkinter库的Text记录点击路经和删除记录详情
2021-04-15 03:41:13
Webpack4 使用Babel处理ES6语法的方法示例
2023-08-30 08:12:37
Python程序设计入门(1)基本语法简介
2023-09-12 05:03:31
Python实现控制手机电脑拍照并自动发送邮箱
2022-10-08 01:52:33
xmlHttp msxml3.dll 错误 '800c0008' 解决办法
2008-08-31 20:44:00