问题描述
我尝试对我提出的另一个问题进行一些更改,这是链接:
ship_off =
space =
bullet =
asteroid =
这是我的代码:
导入pygame从数学导入 sin, cos, pi来自随机导入 randintscr_width = 800scr_height = 600window = pygame.display.set_mode((scr_width, scr_height))pygame.display.set_caption(小行星")时钟 = pygame.time.Clock()space_img = pygame.image.load("sprites/space.jpg")红色 = (255, 0, 0)# todo 对象碰撞# 以更大的间隔拍摄待办事项# 待办事项评分系统# todo 小行星分裂船级:def __init__(self, x, y):自我.x = x自我.y = yself.width = 0self.vel = 0self.vel_max = 12self.angle = 0self.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())定义绘制(自我):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))键 = pygame.key.get_pressed()如果键[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))定义移动(自我):键 = pygame.key.get_pressed()# todo 加速和推力力学如果键[pygame.K_w]:self.vel = min(self.vel + 1, self.vel_max)elif self.vel >0:self.vel = self.vel - 0.4如果键[pygame.K_a]:self.angle += 7如果键[pygame.K_d]:self.angle -= 7self.x += self.vel * cos(self.angle * (pi/180) + (90 * pi/180))self.y -= self.vel * sin(self.angle * (pi/180) + 90 * (pi/180))# 所以如果它离开一侧它来自另一侧如果 self.y 800:self.x = (self.x + self.vel) % 800类小行星(pygame.sprite.Sprite):def __init__(self):super().__init__()y_values = [1, 599]self.x = randint(0, 800)self.y = y_values[randint(0, 1)]# 如果对象从顶部生成,它会向下移动而不是向上移动并立即取消生成如果 self.y == y_values[0]:self.neg = -1别的:self.neg = 1self.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())定义生成(自我):self.ang += self.ang_changeself.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))类射弹(pygame.sprite.Sprite):def __init__(self, x, y, 角度):super().__init__()自我.x = x自我.y = yself.angle = 角度self.vel = 20self.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)定义绘制(自我):window.blit(self.image, (self.x - 2, self.y))定义重绘():window.blit(space_img, (0, 0))船.draw()对于小行星中的小行星:asteroid.generate()对于子弹中的子弹:子弹.draw()pygame.display.update()定义冲突():pygame.sprite.spritecollide(船,小行星,真,pygame.sprite.collide_mask)pygame.sprite.groupcollide(小行星,子弹,真,真,pygame.sprite.collide_mask)# 主循环运行 = 真船 = 船(400, 300)next_fire = pygame.time.get_ticks() + 400小行星 = pygame.sprite.Group()子弹 = pygame.sprite.Group()运行时:时钟滴答(60)键 = pygame.key.get_pressed()pygame.time.delay(35)碰撞()对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误如果键[pygame.K_SPACE]:如果 len(bullets) = next_fire:子弹.添加(弹丸(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对于子弹中的子弹:如果 800 >子弹.x >0 和 600 >子弹.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))别的:子弹.kill()# 限制屏幕上小行星的数量如果 len(asteroids) 0 和 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如果 asteroid.x 800:asteroid.x = (asteroid.x + asteroid.speed) % 800别的:asteroid.kill()window.fill((0, 0, 0))船舶移动()重绘()pygame.quit()
希望拥有精灵能有所帮助.
操作
I've tried making some of the changes from the other question I had asked, here's the link:Collisions aren't being detected in pygame
But anyway, I'm trying to make an asteroids style game, where the asteroids can be destroyed when shot by the player and the ship will be destroyed when hit by an asteroid. The problem is, my collisions aren't being detected at all and they're harmlessly passing through each other.
Since people couldn't run my code last time, here are the sprites I'm using:
ship_on =
ship_off =
space =
bullet =
asteroid =
and here's my code:
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()
Hopefully, having sprites can help.
The operations spritecollide
and groupcollide
use the .rect
attribute of a Sprite
object to detect the collision.
Hence, when you change the position of a Sprite object (.x
, .y
), then you have to update the .rect
attribute, too.
At the end of the method move
of the class Ship
:
class Ship:
# [...]
def move(self):
# [...]
self.rect.center = (self.x, self.y) # <---- ADD
And for the bullets
and asteroids
in the main application loop:
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()
这篇关于在 pygame 中仍未检测到碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!