gpt4 book ai didi

python - 如何使用 PyGame 让球从墙上弹起?

转载 作者:行者123 更新时间:2023-12-01 00:08:17 25 4
gpt4 key购买 nike

在你批评我在提问之前没有进行谷歌搜索或研究之前,我事先做过研究,但没有结果。

我正在尝试创建 Atari Breakout 游戏。我目前正致力于让球从墙上反弹。我对此进行了研究,发现很多博客和 YouTube 视频(还有 Stack Overflow 问题: thisthis )谈论 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类的话,能给我解释一下或者给我一个学习的地方吗?

最佳答案

问题是多个嵌套循环。您有一个应用程序循环,所以使用它。
在循环中不断移动球:

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()

关于python - 如何使用 PyGame 让球从墙上弹起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59802794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com