gpt4 book ai didi

python - 无法为我的 Ping Pong pygame 创建一个 Hitbox

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:07 27 4
gpt4 key购买 nike

这是我的脚本,它需要制作一个碰撞盒,以便球从物体上弹开。这是一个非常基本的乒乓球游戏。我尝试过坐标并尝试观看其他视频和论坛,我真的很感激有人能帮助我。我根本不明白,这是一项学校作业。我很懒,但是有人可以为我做一个碰撞盒吗?或者至少帮助我。

这是脚本:

import pygame
pygame.init()

width = 500
height = 400
#pygame.title("Ping Pong Level 1")
screen = pygame.display.set_mode((width, height))
y = 30
g = 30
x = 40
f = 100
velxX = 2
velfF = 2
velX = 50
velF = 20

clock = pygame.time.Clock()


def draw():
screen.fill((0, 0, 0))
player = pygame.draw.rect(screen, (255, 255, 255), (30, y, 30, 100))
player1 = pygame.draw.rect(screen, (255, 255, 255), (440, g, 30, 100))
line = pygame.draw.rect(screen, (0, 0, 255), (250, 0, 10, 400))

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()


pressed = pygame.key.get_pressed()
if pressed[pygame.K_w] and y > 0:
y -= 5
if pressed[pygame.K_s] and y < height - 100:
y += 5
if pressed[pygame.K_UP] and g > 0:
g -= 5
if pressed[pygame.K_DOWN] and g < height - 100:
g += 5


velF += velfF
velX += velxX

if velF < 0 or velF > height - 5:
velfF *= -1
if velX < 0 or velX > width - 5:
velxX *= -1

pygame.draw.circle(screen, (0, 255, 0), (velX, velF), 5)
clock.tick(60)
pygame.display.flip()

draw()

screen.mainloop()

最佳答案

好的,让我们逐步完成这一步。

首先,让我们重新排序您的代码,这样我们就有了一个不错的标准主循环:事件处理 -> 状态更新 -> 绘制。我们看到您有一个 draw 函数,但是您并没有在这个函数中绘制所有内容。此外,您在 draw 之前调用了 pygame.display.flip()screen.mainloop() 没有意义,sys.exit() 也不会工作,因为您没有导入 sys

第一步:

import pygame

def main():
pygame.init()

width = 500
height = 400
#pygame.title("Ping Pong Level 1")
screen = pygame.display.set_mode((width, height))
y = 30
g = 30
x = 40
f = 100
velxX = 2
velfF = 2
velX = 50
velF = 20

clock = pygame.time.Clock()

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return

pressed = pygame.key.get_pressed()
if pressed[pygame.K_w] and y > 0:
y -= 5
if pressed[pygame.K_s] and y < height - 100:
y += 5
if pressed[pygame.K_UP] and g > 0:
g -= 5
if pressed[pygame.K_DOWN] and g < height - 100:
g += 5

velF += velfF
velX += velxX

if velF < 0 or velF > height - 5:
velfF *= -1
if velX < 0 or velX > width - 5:
velxX *= -1

screen.fill((0, 0, 0))
pygame.draw.circle(screen, (0, 255, 0), (velX, velF), 5)
player = pygame.draw.rect(screen, (255, 255, 255), (30, y, 30, 100))
player1 = pygame.draw.rect(screen, (255, 255, 255), (440, g, 30, 100))
line = pygame.draw.rect(screen, (0, 0, 255), (250, 0, 10, 400))
pygame.display.flip()

clock.tick(60)

if __name__ == '__main__':
main()

接下来,让我们看看你的游戏状态和绘图。您有一堆名称奇怪的变量,以及您不使用或没有意义的变量(例如 velxXfplayer1) 您还绘制了一些矩形,所以让我们使用适当的数据结构来解决问题:有用的 RectVector2类。

第 2 步:

import pygame

def main():
pygame.init()

screen = pygame.display.set_mode((500, 400))
screen_rect = screen.get_rect()

left_paddle = pygame.Rect((30, 30, 30, 100))
right_paddle = pygame.Rect((440, 30, 30, 100))

ball_position = pygame.Vector2((50, 20))
ball_direction = pygame.Vector2((1, 1)).normalize()
ball_speed = 5

clock = pygame.time.Clock()

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return

pressed = pygame.key.get_pressed()
if pressed[pygame.K_w]: left_paddle.move_ip(0, -5)
if pressed[pygame.K_s]: left_paddle.move_ip(0, 5)
if pressed[pygame.K_UP]: right_paddle.move_ip(0, -5)
if pressed[pygame.K_DOWN]: right_paddle.move_ip(0, 5)

# keep paddles on the screen
left_paddle.clamp_ip(screen_rect)
right_paddle.clamp_ip(screen_rect)

ball_position += ball_direction * ball_speed

# flip direction on edge
if ball_position[1] < 0 or ball_position[1] > screen_rect.height - 5:
ball_direction = pygame.Vector2(ball_direction[0], -ball_direction[1])

if ball_position[0] < 0 or ball_position[0] > screen_rect.width - 5:
ball_direction = pygame.Vector2(-ball_direction[0], ball_direction[1])

screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255, 255, 255), left_paddle)
pygame.draw.rect(screen, (255, 255, 255), right_paddle)
pygame.draw.rect(screen, (0, 0, 255), (250, 0, 10, 400))
pygame.draw.circle(screen, (0, 255, 0), [int(v) for v in ball_position], 5)
pygame.display.flip()

clock.tick(60)

if __name__ == '__main__':
main()

到目前为止,还不错。现在实现碰撞检测很容易。我们可以计算下一步球的位置,然后检查它们是否会与桨碰撞。由于我们使用的是 Rect类,很容易检查碰撞(collidepoint)并使它们变大(inflate,因为球的大小大于一个像素)。

第 3 步:

import pygame

def main():
pygame.init()

screen = pygame.display.set_mode((500, 400))
screen_rect = screen.get_rect()

left_paddle = pygame.Rect((30, 30, 30, 100))
right_paddle = pygame.Rect((440, 30, 30, 100))

ball_position = pygame.Vector2((150, 120))
ball_direction = pygame.Vector2((1, 1)).normalize()
ball_speed = 5

clock = pygame.time.Clock()

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return

pressed = pygame.key.get_pressed()
if pressed[pygame.K_w]: left_paddle.move_ip(0, -5)
if pressed[pygame.K_s]: left_paddle.move_ip(0, 5)
if pressed[pygame.K_UP]: right_paddle.move_ip(0, -5)
if pressed[pygame.K_DOWN]: right_paddle.move_ip(0, 5)

# keep paddles on the screen
for paddle in (left_paddle, right_paddle):
paddle.clamp_ip(screen_rect)

ball_position += ball_direction * ball_speed

# flip direction on edge
if ball_position[1] < 0 or ball_position[1] > screen_rect.height - 5:
ball_direction = pygame.Vector2(ball_direction[0], -ball_direction[1])

if ball_position[0] < 0 or ball_position[0] > screen_rect.width - 5:
ball_direction = pygame.Vector2(-ball_direction[0], ball_direction[1])

next_ball_position = ball_position + ball_direction * ball_speed
for paddle in (left_paddle, right_paddle):
if paddle.inflate(5, 5).collidepoint(next_ball_position):
ball_direction = pygame.Vector2(-ball_direction[0], ball_direction[1])

screen.fill((0, 0, 0))
for paddle in (left_paddle, right_paddle):
pygame.draw.rect(screen, (255, 255, 255), paddle)
pygame.draw.rect(screen, (0, 0, 255), (250, 0, 10, 400))
pygame.draw.circle(screen, (0, 255, 0), [int(v) for v in ball_position], 5)
pygame.display.flip()

clock.tick(60)

if __name__ == '__main__':
main()

enter image description here

请注意,这仅在球击中前面的 Racket 时有效,但您可以轻松检查球相对于 Racket 的位置以确定是否要更改球方向的 yx 轴。

而且由于我们使用矢量表示球的方向,因此让球以 45° 以外的角度移动也很容易。

关于python - 无法为我的 Ping Pong pygame 创建一个 Hitbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55276071/

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