gpt4 book ai didi

python - Pygame 碰撞错误

转载 作者:行者123 更新时间:2023-11-28 20:33:37 25 4
gpt4 key购买 nike

我正在用 pygame 编写一个简单的躲避类游戏,希望我能开发它来构建类似 Asteroids 的游戏。

我使用 .collidelist() 来识别与我随机生成的“小行星”的碰撞。但是当玩家进入小行星下方或上方时,会检测到碰撞。

# -------------------- COLLISION  --------------------
collision = player_rect.collidelist(objects)
if collision != -1:
lives -= 1
if lives >= 0:
gameLoop()
else:
pygame.quit()
quit()

我的小行星似乎只在垂直方向移动,而不是水平方向,即使我已经改变了它们的水平值。

for x in random_x_pos:
random_y_pos[random_x_pos.index(x)] += object_y_vel[random_x_pos.index(x)]
x += object_x_vel[random_x_pos.index(x)]

这是我的其余代码:

import pygame, random
from pygame.locals import *

pygame.init()
pygame.display.set_caption('Dodge')

#icon = pygame.image.load('icon.png')
#pygame.display.set_icon(icon)

# -------------------- Colours --------------------
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,155,0)
blue = (0,0,255)
pink = (233,30,98)

# -------------------- Variables --------------------
display_width = 600
display_height = 600
FPS = 60
lives = 3

clock = pygame.time.Clock()
gameDisplay = pygame.display.set_mode((display_width, display_height))

# -------------------- Actual Game --------------------
def gameLoop():
global lives
# -------------------- Player --------------------
player_dim = 20
x_pos = ((display_width / 2) - player_dim)
y_pos = ((display_height / 2) - player_dim)
vel = 5
player_rect = pygame.draw.rect(gameDisplay, pink, (x_pos, y_pos, player_dim, player_dim))

# -------------------- Random Objects --------------------
object_dimensions = 30
amount_of_objects = 10
random_x_pos, random_y_pos, object_x_vel, object_y_vel, objects = [], [], [], [], []
for int in range(amount_of_objects):
x, y, x_vel, y_vel = random.randint(0, display_width), random.randint(0, display_height), random.randint(-5,5), random.randint(-5,5)
rect1 = pygame.draw.rect(gameDisplay, white, (x, y, 30, 30))
again = rect1.colliderect(player_rect)
while again:
x, y = random.randint(0, display_width), random.randint(0, display_height)
rect1 = pygame.draw.rect(gameDisplay, white, (x, y, 30, 30))
again = rect1.colliderect(player_rect)
random_x_pos.append(x)
random_y_pos.append(y)
object_x_vel.append(x_vel)
object_y_vel.append(y_vel)

# -------------------- Event Handling --------------------
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
pygame.quit()
quit()
if keys[pygame.K_UP] and y_pos > 0:
y_pos -= vel
if keys[pygame.K_DOWN] and (y_pos + player_dim) < display_height:
y_pos += vel
if keys[pygame.K_LEFT] and x_pos > 0:
x_pos -= vel
if keys[pygame.K_RIGHT] and (x_pos + player_dim) < display_width:
x_pos += vel

for x in random_x_pos:
random_y_pos[random_x_pos.index(x)] += object_y_vel[random_x_pos.index(x)]
x += object_x_vel[random_x_pos.index(x)]

# -------------------- COLLISION --------------------
collision = player_rect.collidelist(objects)
if collision != -1:
lives -= 1
if lives >= 0:
gameLoop()
else:
pygame.quit()
quit()

gameDisplay.fill(black)
player_rect = pygame.draw.rect(gameDisplay, pink, (x_pos, y_pos, player_dim, player_dim))
for obj in random_x_pos:
y = random_y_pos[random_x_pos.index(obj)]
var = pygame.draw.rect(gameDisplay, white, (obj, y, object_dimensions, object_dimensions))
objects.append(var)
pygame.display.update()
clock.tick(FPS)


gameLoop()

最佳答案

for obj in random_x_pos: 循环中,您疯狂地向 objects 列表添加新的矩形。您无需移动和绘制这些旧矩形,因此您可以很快获得数千个不可见矩形,它们仍用于碰撞检测。

您必须重构代码。我建议创建一个矩形列表及其速度。

objects = []

for i in range(amount_of_objects):
rect = pygame.Rect(random.randint(0, display_width), random.randint(0, display_height), 30, 30)
velocity = [random.randint(-5,5), random.randint(-5,5)]
# Put the rect and the velocity into a list and append it to the objects.
objects.append([rect, velocity])

更新 for 循环中的矩形位置。您可以同时进行碰撞检测。

collision = False
for rect, velocity in objects:
rect.x += velocity[0]
rect.y += velocity[1]

if player_rect.colliderect(rect):
collision = True

if collision:
lives -= 1
# etc.

像这样绘制矩形:

for rect, velocity in objects:
pygame.draw.rect(gameDisplay, white, rect)

关于python - Pygame 碰撞错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50288147/

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