[英]Collisions still aren't getting detected in pygame
我已经尝试对我提出的另一个问题进行一些更改,这是链接: pygame 中未检测到碰撞
但无论如何,我正在尝试制作一个小行星风格的游戏,其中小行星可以在玩家射击时被摧毁,而船会在被小行星击中时被摧毁。 问题是,我的碰撞根本没有被检测到,它们无害地相互穿过。
由于人们上次无法运行我的代码,所以这里是我正在使用的精灵:
这是我的代码:
import pygame
from math import sin, cos, pi
from random import randint
scr_width = 800
scr_height = 600
window = pygame.display.set_mode((scr_width, scr_height))
pygame.display.set_caption("Asteroids")
clock = pygame.time.Clock()
space_img = pygame.image.load("sprites/space.jpg")
red = (255, 0, 0)
# todo object collisions
# todo shooting at larger intervals
# todo score system
# todo asteroid division
class Ship:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 0
self.vel = 0
self.vel_max = 12
self.angle = 0
self.image = pygame.image.load("sprites/ship_off.png")
self.image_copy = pygame.transform.rotate(self.image, self.angle)
self.mask = pygame.mask.from_surface(self.image_copy)
self.rect = pygame.Rect(self.x - (self.image_copy.get_width()) / 2,
self.y - (self.image_copy.get_height()) / 2,
self.image_copy.get_width(), self.image_copy.get_height())
def draw(self):
self.image = pygame.image.load("sprites/ship_off.png")
self.image_copy = pygame.transform.rotate(self.image, self.angle)
window.blit(self.image_copy,
(self.x - (self.image_copy.get_width()) / 2, self.y - (self.image_copy.get_height()) / 2))
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.image = pygame.image.load("sprites/ship_on.png")
self.image_copy = pygame.transform.rotate(self.image, self.angle)
window.blit(self.image_copy,
(self.x - (self.image_copy.get_width()) / 2, self.y - (self.image_copy.get_height()) / 2))
def move(self):
keys = pygame.key.get_pressed()
# todo acceleration and thrust mechanics
if keys[pygame.K_w]:
self.vel = min(self.vel + 1, self.vel_max)
elif self.vel > 0:
self.vel = self.vel - 0.4
if keys[pygame.K_a]:
self.angle += 7
if keys[pygame.K_d]:
self.angle -= 7
self.x += self.vel * cos(self.angle * (pi / 180) + (90 * pi / 180))
self.y -= self.vel * sin(self.angle * (pi / 180) + 90 * (pi / 180))
# So that if it leaves one side it comes from the other
if self.y < 0:
self.y = (self.y - self.vel) % 600
elif self.y > 600:
self.y = (self.y + self.vel) % 600
elif self.x < 0:
self.x = (self.x - self.vel) % 800
elif self.x > 800:
self.x = (self.x + self.vel) % 800
class Asteroid(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
y_values = [1, 599]
self.x = randint(0, 800)
self.y = y_values[randint(0, 1)]
# If object spawns from the top, it moves down instead of moving up and de-spawning immediately
if self.y == y_values[0]:
self.neg = -1
else:
self.neg = 1
self.speed = randint(5, 10)
self.ang = randint(0, 90) * (pi / 180)
self.ang_change = randint(1, 5)
self.asteroid_angle = randint(0, 80)
self.image = pygame.image.load("sprites/asteroid.png")
self.image_copy = pygame.transform.rotate(self.image, self.ang)
self.mask = pygame.mask.from_surface(self.image_copy)
self.rect = pygame.Rect(self.x - (self.image_copy.get_width()) / 2,
self.y - (self.image_copy.get_height()) / 2,
self.image_copy.get_width(), self.image_copy.get_height())
def generate(self):
self.ang += self.ang_change
self.image = pygame.image.load("sprites/asteroid.png")
self.image_copy = pygame.transform.rotate(self.image, self.ang)
window.blit(self.image_copy,
(self.x - (self.image_copy.get_width()) / 2, self.y - (self.image_copy.get_height()) / 2))
class Projectiles(pygame.sprite.Sprite):
def __init__(self, x, y, angle):
super().__init__()
self.x = x
self.y = y
self.angle = angle
self.vel = 20
self.image = pygame.image.load("sprites/bullet.png")
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.rect = pygame.Rect(self.x - (self.image.get_width()) / 2,
self.y - (self.image.get_height()) / 2, 5, 5)
def draw(self):
window.blit(self.image, (self.x - 2, self.y))
def redraw():
window.blit(space_img, (0, 0))
ship.draw()
for asteroid in asteroids:
asteroid.generate()
for bullet in bullets:
bullet.draw()
pygame.display.update()
def collisions():
pygame.sprite.spritecollide(ship, asteroids, True, pygame.sprite.collide_mask)
pygame.sprite.groupcollide(asteroids, bullets, True, True, pygame.sprite.collide_mask)
# main loop
run = True
ship = Ship(400, 300)
next_fire = pygame.time.get_ticks() + 400
asteroids = pygame.sprite.Group()
bullets = pygame.sprite.Group()
while run:
clock.tick(60)
keys = pygame.key.get_pressed()
pygame.time.delay(35)
collisions()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if keys[pygame.K_SPACE]:
if len(bullets) < 11 and pygame.time.get_ticks() >= next_fire:
bullets.add(
Projectiles(round(ship.x + ship.width - 6.5 // 2), round(ship.y + ship.width - 6.5 // 2), ship.angle))
next_fire = pygame.time.get_ticks() + 400
for bullet in bullets:
if 800 > bullet.x > 0 and 600 > bullet.y > 0:
bullet.x += bullet.vel * cos(bullet.angle * (pi / 180) + 90 * (pi / 180))
bullet.y -= bullet.vel * sin(bullet.angle * (pi / 180) + 90 * (pi / 180))
else:
bullet.kill()
# To limit the number of asteroids on screen
if len(asteroids) < 5:
asteroids.add(Asteroid())
for asteroid in asteroids:
if 800 > asteroid.x > 0 and 600 > asteroid.y > 0:
asteroid.x += asteroid.speed * cos(asteroid.asteroid_angle * (pi / 180) + 90 * (pi / 180))
asteroid.y -= asteroid.speed * sin(asteroid.asteroid_angle * (pi / 180) + 90 * (pi / 180)) * asteroid.neg
if asteroid.x < 0:
asteroid.x = (asteroid.x - asteroid.speed) % 800
elif asteroid.x > 800:
asteroid.x = (asteroid.x + asteroid.speed) % 800
else:
asteroid.kill()
window.fill((0, 0, 0))
ship.move()
redraw()
pygame.quit()
希望拥有精灵可以提供帮助。
spritecollide
和groupcollide
操作使用Sprite
object 的.rect
属性来检测碰撞。
因此,当您更改 Sprite object ( .x
, .y
) 的 position 时,您也必须更新.rect
属性。
在 class Ship
的方法move
结束时:
class Ship:
# [...]
def move(self):
# [...]
self.rect.center = (self.x, self.y) # <---- ADD
对于主应用程序循环中的bullets
和asteroids
:
while run:
# [...]
for bullet in bullets:
if 800 > bullet.x > 0 and 600 > bullet.y > 0:
bullet.x += bullet.vel * cos(bullet.angle * (pi / 180) + 90 * (pi / 180))
bullet.y -= bullet.vel * sin(bullet.angle * (pi / 180) + 90 * (pi / 180))
bullet.rect.center = (bullet.x, bullet.y) # <---- ADD
else:
bullet.kill()
# To limit the number of asteroids on screen
if len(asteroids) < 5:
asteroids.add(Asteroid())
for asteroid in asteroids:
if 800 > asteroid.x > 0 and 600 > asteroid.y > 0:
asteroid.x += asteroid.speed * cos(asteroid.asteroid_angle * (pi / 180) + 90 * (pi / 180))
asteroid.y -= asteroid.speed * sin(asteroid.asteroid_angle * (pi / 180) + 90 * (pi / 180)) * asteroid.neg
if asteroid.x < 0:
asteroid.x = (asteroid.x - asteroid.speed) % 800
elif asteroid.x > 800:
asteroid.x = (asteroid.x + asteroid.speed) % 800
asteroid.rect.center = (asteroid.x, asteroid.y) # <---- ADD
else:
asteroid.kill()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.