如何用 PyGame 让球从墙上弹起?
- 2025-02-08 08:52:00
- admin 原创
- 48
问题描述:
在您批评我提问之前没有先进行 Google 搜索或做研究之前,我事先已经做过研究,但无济于事。
我正在尝试创建 Atari Breakout 游戏。目前我被困在让球从墙上弹起的问题上。我对此进行了研究,发现很多博客和 YouTube 视频(以及 Stack Overflow 问题:这个和这个)都在谈论 PyGame 的 vector2 类。我也阅读了关于 vector2 的 PyGame 文档,但我不知道如何让它工作。
我目前正在编写一个脚本,让球从墙上弹起。一开始,要求玩家按下空格键,球就会自动向东北方向移动。它应该在撞到顶墙时从顶墙上弹起,但它却飞进了墙里。这是我的方法:
import pygame
pygame.init()
screenWidth = 1200
screenHeight = 700
window = pygame.display.set_mode((screenWidth,screenHeight))
pygame.display.set_caption('Atari Breakout')
class Circle():
def __init__(self, x, y, radius):
self.x = x
self.y = y
self.radius = radius
self.vel_x = 1
self.vel_y = 1
def check_hit():
global hit
if (((screenWidth-box.x)<=box.radius) or ((box.x)<=box.radius) or ((box.y)<=box.radius) or ((screenHeight-box.y)<=box.radius)):
# meaning hit either four walls
if (((screenWidth-box.x)<=box.radius) or ((box.x)<=box.radius)):
# hit right, left
print('hit right, left')
hit = True
elif (((box.y)<=box.radius) or ((screenHeight-box.y)<=box.radius)):
# hit top, bottom
print('hit top, bottom')
hit = True
# main loop
run = True
box = Circle(600,300,10)
hit = False
# (screenWidth-box.x)<=box.radius hit right wall
while run: # (box.x)<=box.radius hit left wall
# (box.y)<=box.radius hit top wall
pygame.time.Clock().tick(60) # (screenHeight-box.y)<=box.radius hit bottom wall
for event in pygame.event.get():
if event == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE] and (box.y)>box.radius:
while True:
box.y -= box.vel_y
box.x += box.vel_x
window.fill((0,0,0))
pygame.draw.circle(window, (44,176,55), (box.x, box.y), box.radius)
pygame.display.update()
check_hit()
if hit == False:
continue
elif hit == True:
break
if (box.y)<=box.radius or (screenHeight-box.y)<=box.radius:
# hit top, bottom
box.vel_x *= 1
box.vel_y *= -1
print('changed')
if (box.y)<=box.radius:
# hit top
print('hi')
while True:
box.x += box.vel_x # <-- myguess is this is the problem
box.y += box.vel_y
window.fill((0,0,0))
pygame.draw.circle(window, (44,176,55), (box.x, box.y), box.radius)
pygame.display.update()
elif (screenWidth-box.x)<=box.radius or (box.x)<=box.radius:
# hit right, left
box.vel_x *= -1
box.vel_y *= 1
window.fill((0,0,0))
pygame.draw.circle(window, (44,176,55), (box.x, box.y), box.radius)
pygame.display.update()
print('Where are you going')
pygame.quit()
我猜问题出在我标记的地方。标记的位置如下:
if (box.y)<=box.radius or (screenHeight-box.y)<=box.radius:
# hit top, bottom
box.vel_x *= 1
box.vel_y *= -1
print('changed')
if (box.y)<=box.radius:
# hit top
print('hi')
while True:
box.x += box.vel_x # <-- myguess is this is the problem
box.y += box.vel_y
window.fill((0,0,0))
pygame.draw.circle(window, (44,176,55), (box.x, box.y), box.radius)
pygame.display.update()
但我不知道为什么。我的理论是:球向上移动,撞到顶墙,check_hit()
踢入并使hit
= True,然后vel_x
和vel_y
会相应改变(如果撞到顶墙,vel_x
应该保持不变,而vel_y
应该乘以 -1)。然后它会向下移动,因此从顶墙上“弹起”。
注意:目前我只让顶墙正常工作。当我能弄清楚如何先从顶墙反弹时,其他三个墙就会完成。
你能帮我看看问题出在哪里吗?如果这种操作需要使用 vector2 类,你能给我解释一下吗,或者给我一个学习它的地方吗?
解决方案 1:
问题在于多重嵌套循环。您有一个应用循环,因此请使用它。
在循环中连续移动球:
box.y -= box.vel_y
box.x += box.vel_x
通过对象为球定义一个矩形区域pygame.Rect
:
bounds = window.get_rect() # full screen
或者
bounds = pygame.Rect(450, 200, 300, 200) # rectangular region
当球击中边界时改变运动方向:
if box.x - box.radius < bounds.left or box.x + box.radius > bounds.right:
box.vel_x *= -1
if box.y - box.radius < bounds.top or box.y + box.radius > bounds.bottom:
box.vel_y *= -1
请看示例:
box = Circle(600,300,10)
run = True
start = False
clock = pygame.time.Clock()
while run:
clock.tick(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
start = True
bounds = pygame.Rect(450, 200, 300, 200)
if start:
box.y -= box.vel_y
box.x += box.vel_x
if box.x - box.radius < bounds.left or box.x + box.radius > bounds.right:
box.vel_x *= -1
if box.y - box.radius < bounds.top or box.y + box.radius > bounds.bottom:
box.vel_y *= -1
window.fill((0,0,0))
pygame.draw.rect(window, (255, 0, 0), bounds, 1)
pygame.draw.circle(window, (44,176,55), (box.x, box.y), box.radius)
pygame.display.update()
相关推荐
热门文章
项目管理软件有哪些?
热门标签
云禅道AD