python中的pygame弹球游戏代码_pygame实现弹球游戏本⽂实例为⼤家分享了pygame实现弹球游戏的具体代码,供⼤家参考,具体内容如下
pygame弹球游戏
写的很简陋
pip install pygame 安装pygame模块
代码,复制运⾏即可
import pygame
import random
pygame.init()
win = pygame.display.set_mode((600, 600)) # 画布窗⼝的⼤⼩
pygame.display.set_caption("弹球游戏") # 窗⼝标题
x, y = 300, 0 # ⽅块的起点
width, height = 10, 10 # ⽅块的宽,⾼
speed = 1 # 速度
def _randomOK():
return random.randint(0, 1)
stop = False
_random = _randomOK()
str1 = "暂停中"
baffle = 250
status = 0
count = 0
top = 0
while True:
# 刷新频率, ⼩球移动速度
pygame.time.Clock().tick(1000)
for event in ():
# 窗⼝x事件
pe == pygame.QUIT:
exit(0)
pe == pygame.KEYDOWN:
# 回车事件
if event.key == 13:
str1 = "暂停中"
stop = not stop
if status == 1:
x, y = 300, 0
keys = _pressed()
if stop:
pygame.display.set_caption(str1) # 窗⼝标题continue
if y >= 590:
status = 1
stop = not stop
str1 = "游戏结束,回车重新开始,反弹次数" + str(count) count = 0
pygame.display.set_caption("弹球游戏") # 窗⼝标题if y == 0:
top = 0
if top == 0:
if _random == 0: # 向下左弹
x -= speed
y += speed
elif _random == 1:
x += speed
y += speed
else:
if _random == 0: # 向上左弹
x -= speed
y -= speed
elif _random == 1: # 向上右弹
x += speed
y -= speed
# ⽅向箭头响应
if keys[pygame.K_LEFT]:
baffle -= speed
if baffle < 0:
baffle = 0
if keys[pygame.K_RIGHT]:
baffle += speed
if baffle > 500:
baffle = 500
# 碰撞逻辑
if 500 <= y <= 520:
print(x, y)
print(baffle)
# y ⾼度坐标 200 x 宽度坐标 200
# x坐标加300 ⼤于 宽度初始坐标, ⼩于 宽度+300
if baffle <= x <= baffle + 100:
count += 1
top = 1
random python# 防⽌跑出边界
if x > _size()[0] - width:
_random = _randomOK()
x = _size()[0] - width
if x < 0:
_random = _randomOK()
x = 0
if y > _size()[1] - height:
_random = _randomOK()
y = _size()[1] - height
if y < 0:
_random = _randomOK()
y = 0
# 将每⼀帧的底⾊先填充成⿊⾊
win.fill((64, 158, 255))
# 画⽅块
(win, (255, 0, 0), (x, y, width, height))
# 挡板设置,
(win, (255, 255, 255), (baffle, 500, 100, 20)) # 更新画布
pygame.display.update()
pygame.quit()
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持我们。本⽂标题: pygame实现弹球游戏
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论