파이썬 프로그래밍 클래스에 우주선 게임을 만들고 싶습니다. sprite.spritecollideany
을 사용하여 내 플레이어 스프라이트 spaceship
이 spriteGroup
그룹의 소행성 스프라이트와 충돌하는지 확인하고 싶습니다. 그러나 내가하는 일과 상관없이 그것은 효과가없는 것 같습니다.파이 게임에 충돌이 발생하지 않습니다. 스프라이트
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Asteroids and Spaceships")
background = pygame.image.load("background.png")
background = background.convert()
white = 255,255,255
#first I make the asteroids with this class.
class asteroids(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = 30
self.height = 30
self.i1 = pygame.image.load("smallasteroid.png")
self.i1 = self.i1.convert()
self.i1.set_colorkey(white)
self.rect = self.i1.get_rect()
self.rect.left = x
self.rect.top = y
self.i2 = pygame.image.load("smallasteroid2.png")
self.i2 = self.i2.convert()
self.i2.set_colorkey(white)
self.rect = self.i2.get_rect()
self.rect.left = x
self.rect.top = y
self.i3 = pygame.image.load("mediumasteroid.png")
self.i3 = self.i3.convert()
self.i3.set_colorkey(white)
self.rect = self.i3.get_rect()
self.rect.left = x
self.rect.top = y
self.current = 0
def render(self, image_num):
if image_num == 1:
self.current = 1
if image_num == 2:
self.current = 2
if image_num == 3:
self.current = 3
def update(self):
if self.current == 1:
screen.blit(self.i1, (self.x,self.y))
self.y += random.randint(7,11)
if self.y > screen.get_height():
self.y = 0
if self.current == 2:
screen.blit(self.i2, (self.x,self.y))
self.y += random.randint(5,9)
if self.y > screen.get_height():
self.y = 0
if self.current == 3:
screen.blit(self.i3, (self.x,self.y))
self.y += random.randint(3,6)
if self.y > screen.get_height():
self.y = 0
#and then this is the class for the spaceship
class spaceship(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = 40
self.height = 60
self.image = pygame.image.load("spaceship.png")
self.image = self.image.convert()
self.image.set_colorkey(white)
self.rect = self.image.get_rect()
self.rect.left = x
self.rect.top = y
def update(self):
screen.blit(self.image,(self.x,self.y))
if self.y > screen.get_height():
self.y = 0
if self.y < screen.get_height()-610:
self.y = screen.get_height()
if self.x > screen.get_width():
self.x = 0
if self.x < screen.get_width()-610:
self.x = screen.get_width()
#main is where I have the game run
def main():
x_ship, y_ship = 0,0
player = spaceship(300,550)
spriteGroup = pygame.sprite.Group() #my asteroid sprites are grouped here
for i in range(25):
image_num = random.randint(0,3)
asteroid = asteroids(random.randint(0,500),random.randint(0, 200))
asteroid.render(image_num)
spriteGroup.add(asteroid)
clock = pygame.time.Clock()
keepGoing = True
while keepGoing:
#this is what I tried to use to check for collisions. I know it's not working but I don't know why.
if pygame.sprite.spritecollideany(player,spriteGroup):
#the program quits on collision.
pygame.quit()
for event in pygame.event.get():
if (event.type == pygame.QUIT):
keepGoing = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
keepGoing = False
if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_LEFT):
x_ship = -4
if (event.key == pygame.K_RIGHT):
x_ship = 4
if (event.key == pygame.K_UP):
y_ship = -4
if (event.key == pygame.K_DOWN):
y_ship = 4
if (event.type==pygame.KEYUP):
if (event.key==pygame.K_LEFT):
x_ship = 0
if (event.key==pygame.K_RIGHT):
x_ship = 0
if (event.key==pygame.K_UP):
y_ship = 0
if (event.key==pygame.K_DOWN):
y_ship = 0
player.x += x_ship
player.y += y_ship
screen.blit(background, (0,0))
spriteGroup.clear(screen, background)
player.update()
spriteGroup.update()
clock.tick(50)
pygame.display.flip()
pygame.quit()
main()
충돌이 작동하지 않는 이유를 알아낼 수 없습니다
여기에 코드입니다.
향후 참조 할 수 있도록 시도해 본 내용 중 일부 또는 문제가 있다고 생각되는 부분을 간략하게 언급 할 가치가 있습니다. 자리 표시 자 애셋 (self.i1 = pygame.Surface ((30,30)); self.i1.fill ((255,25,25)) 대신 실제 코드 대신 준비된 코드를 게시 할 수도 있습니다 예를 들어 우리가 쓸모가없는 이미지)를 추가 편집하지 않고 실행할 수 있습니다. 원래 이미지 참조를 그대로 두었으므로 아직 이해할 수 있지만 다음 번에 약간의 도움이 될 것입니다. – Augusta
죄송합니다. 나는 생각하지 않았습니다. 제안 해 주셔서 감사합니다. 다음 번에 내가 게시 할 것을 기억할 것입니다. – alexianFirefly
걱정할 필요가 없습니다! : D 만약 여러분이 코드를 시도해보고 싶다면'소행성 .__ init__ '의'i2'와'i3' 데이터와 같은 중복 코드와 알려진 기능 코드를 지우는 것도 고려할 수 있습니다. 부수적이고 오류와 관련이 없는지 확인하십시오. (가끔은 서둘러야 할 때가 있습니다.) – Augusta