我正在制作一个类似于Galaga的游戏,我正在进行计算机课程。我遇到了碰撞问题而无法让错误消失。我希望能够让激光在与敌人的碰撞中消失,并且在10次左右的命中后敌人消失。我不明白所提出的错误或我如何解决问题。
谢谢
代码:
import pygame, sys, pygame.mixer
from sys import exit
import random
from pygame import *
import time
h = 700
w = 700
m = 0
h = 0
score = 0
screen = pygame.display.set_mode((w, h))
clock = pygame.time.Clock()
done = False
x = 400
y = 400
white = (255, 255, 255)
background = pygame.image.load('water.png')
pygame.init()
class Player:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 100
self.height = 100
self.velocity = 0
self.images = pygame.image.load('speeder.png')
self.numImages = 6
self.cImage = 0
def update(self):
self.speedx = 0
self.speedy = 0
pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT]:
self.speedx -= 8
if pressed[pygame.K_RIGHT]:
self.speedx += 8
self.x += self.speedx
if pressed[pygame.K_DOWN]:
self.cImage = 0
self.y += 4
if pressed[pygame.K_UP] and (self.cImage <= self.numImages - 1):
self.cImage += 1
self.y -= 10
if self.cImage >= 5:
self.cImage = 5
def render(self, screen):
screen.blit(self.images, (self.x, self.y), (self.cImage * self.width, 0, self.width, self.height))
class Enemy1:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 128
self.height = 128
self.velocity = 0
self.images = pygame.image.load('enemy1.png')
self.numImages = 7
self.cImage = 0
self.z = 0
self.j = 0
self.rect = self.images.get_rect()
def update(self):
self.j += 1
self.speedx = 0
self.speedy = 0
self.y += 1
if self.z <= 100:
self.x += random.randint(1, 2)
elif self.z > 100 and self.z < 200:
self.x += random.randint(-2, -1)
#self.cImage += 1
if self.z >= 200:
self.z = 0
if self.j % 10 == 0:
self.cImage += 1
if self.cImage == 7:
self.cImage = 0
self.z += random.randint(0, 5)
def render(self, screen):
screen.blit(self.images, (self.x, self.y), (self.cImage * self.width, 0, self.width, self.height))
class Laser(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('lazer.png')
self.rect = self.image.get_rect()
self.m = 0
def update(self):
self.m += 1
if (self.m % 5) != 0 or self.m % 4 != 1:
self.rect.y -= 13
def kill(self):
self.image = None
class RLaser(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('lazer.png')
self.rect = self.image.get_rect()
self.m = 0
def update(self):
self.m += 1
if (self.m % 5) != 0 or self.m % 4 != 1:
self.rect.y += 13
class Boss(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = 50
self.height = 100
self.velocity = 0
self.images = pygame.image.load('boss.png')
self.numImages = 5
self.cImage = 0
self.z = 0
self.rect = self.images.get_rect()
def update(self):
self.speedx = 0
self.speedy = 0
self.y += 1
if self.z <= 100:
self.x += random.randint(1, 2)
elif self.z > 100 and self.z < 200:
self.x += random.randint(-2, -1)
self.cImage += 1
if self.z >= 200:
self.z = 0
if self.cImage == 5:
self.cImage = 4
self.z += random.randint(0, 5)
def render(self, screen):
screen.blit(self.images, (self.x, self.y), (self.cImage * self.width, 0, self.width, self.height))
pygame.init()
all_sprites_list = pygame.sprite.Group()
laser_list = pygame.sprite.Group()
#enemy1blast_list = pygame.sprite.Group()
player = Player(400, 590)
boss = Boss(300, 100)
enemy = Enemy1(100, 50)
j = 0
while not done:
j += 1
screen.blit(background, (0, (-690 * 2) + j))
screen.blit(background, (0, -690 + j))
screen.blit(background, (0, j))
screen.blit(background, (0, 690 + j))
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pressed = pygame.key.get_pressed()
#j += 1
#if j % 10 == 0:
# enemy1blaster = Enemy1Blast()
# enemy1blaster.rect.x = enemy.x
# enemy1blaster.rect.y = enemy.y
#all_sprites_list.add(enemy1blaster)
#enemy1blast_list.add(enemy1blaster)
if pressed[pygame.K_SPACE]:
m += 2
if (m % 4) != 0:
laser = Laser()
laser.rect.x = (player.x + 14)
laser.rect.y = (player.y - 6)
all_sprites_list.add(laser)
laser_list.add(laser)
for laser in laser_list:
if laser.rect.x >= enemy.rect.x or laser.rect.x <= enemy.rect.x and laser.rect.y >= enemy.rect.y or laser.rect.y <= enemy.rect.y:
laser.kill()
if pressed[pygame.K_SPACE]:
h += 2
if (h % 4) != 0:
l = Laser()
l.rect.x = (player.x + 61)
l.rect.y = (player.y - 6)
all_sprites_list.add(l)
laser_list.add(l)
lsound = pygame.mixer.Sound("Laser.wav")
lsound.play()
if j % 20 == 0 and boss.y <= 700:
bl = RLaser()
bl2 = RLaser()
bl.rect.x = (boss.x + 1)
bl.rect.y = (boss.y + 50)
bl2.rect.x = (boss.x + 19)
bl2.rect.y = (boss.y + 50)
all_sprites_list.add(bl)
all_sprites_list.add(bl2)
laser_list.add(bl)
laser_list.add(bl2)
blsound = pygame.mixer.Sound("Laser.wav")
blsound.play()
#if bool(bl) == True:
#bl.rect.y += 26
all_sprites_list.update()
#if bool(bl) == True:
#bl.rect.y += 26
#for laser in laser_list:
# if pygame.sprite.collide_rect(boss, laser):
# laser.kill()
# print "asdfasdf"
if player.y <= 200:
player.y = 202
if player.y >= 633:
player.y = 631
if player.x >= 623:
player.x = 621
if player.x <= -30:
player.x = -28
#Collision_Detector(laser, l, boss)
boss.render(screen)
boss.update()
enemy.render(screen)
enemy.update()
player.render(screen)
player.update()
boss_laser_hlist = pygame.sprite.spritecollide(boss, laser_list, False)
enemy_laser_hlist = pygame.sprite.spritecollide(enemy, laser_list, False)
for laser in enemy_laser_hlist:
score += 1
for laser in boss_laser_hlist:
score += 1
print score
all_sprites_list.draw(screen)
pygame.display.update()
clock.tick(30)
pygame.quit()