我一直在尝试调试一个不允许敌人随机射击的问题。我注意到敌人1.x并没有从0开始移动。我不知道为什么,因为它显然在整个屏幕上从左到右移动。
clock = pygame.time.Clock()
class Ship():
global w, h, win
def __init__(self,x = w/2,y = 500):
self.x = x
self.y = y
self.vel = 20
self.health = 100
self.alive = True
def draw(self):
self.playerhealth()
win.blit(invader,(self.x, self.y))
pygame.draw.rect(win,(255,255,255),(self.x,self.y,30,30),1)
def hit(self):
print('hit')
if self.health > 0:
if self.health - 15 >= 0:
self.health -= 15
else:
self.health = 0
else:
self.alive = False
def playerhealth(self):
thefont= pygame.font.SysFont('Arial', 15)
words = thefont.render('Health: {}'.format(str(round(self.health))),0, (255, 255, 255))
win.blit(words,(10,h -20))
class Enemy1():
global win
def __init__(self,x,y,end,width = 30,height = 30,color = (220,100,100)):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.end = end
self.path = [self.x, self.end]
self.speed = 2
self.hitbox = [self.x, self.y,self.width,self.height]
self.health = 300
self.alive = True
def draw(self, win):
self.movement()
pygame.draw.rect(win,self.color,(self.hitbox))
def movement(self):
if self.speed > 0:
if self.hitbox[0] < self.path[1]+ self.speed:
self.hitbox[0] += self.speed
self.x += self.speed
else:
self.speed = self.speed * -1
self.hitbox[0] += self.speed
self.x += self.speed
else:
if self.hitbox[0]> self.path[0] - self.speed:
self.hitbox[0] += self.speed
self.x += self.speed
else:
self.speed = self.speed * -1
self.hitbox[0] += self.speed
self.x += self.speed
def hit(self):
if self.health > 0:
if self.health - 15 >= 0:
self.health -= 15
else:
self.health = 0
else:
self.alive = False
def enemyhealth(self):
myfont = pygame.font.SysFont('Arial', 15)
text = myfont.render('Enemy Health: {}'.format(str(round(self.health))), 0, (255, 255, 255))
win.blit(text,(10,10))
class PlayerShots():
def __init__(self,x,y,radius,color):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.speed = 30
def draw(self,win):
pygame.draw.circle(win,self.color, (self.x, self.y), self.radius)
class EnemyShots():
def __init__(self,x,y,radius,color):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.speed = 30
def draw(self,win):
pygame.draw.circle(win,self.color, (self.x, self.y), self.radius)
def redraw():
win.fill((0,0,0))
enemy1.enemyhealth()
player.playerhealth()
player.draw()
if enemy1.alive:
enemy1.draw(win)
for bullet in bullets:
bullet.draw(win)
for bullet in enemybullets:
bullet.draw(win)
pygame.display.update()
#
gameon = True
player = Ship()
enemy1 = Enemy1(0,150,330)
bullets = []
enemybullets = []
while gameon:
clock.tick(27)
print(enemy1.x)
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameon = False
for bullet in bullets:
if bullet.y - bullet.radius < enemy1.hitbox[1] + enemy1.hitbox[3] and bullet.y + bullet.radius > enemy1.hitbox[1]:
if bullet.x - bullet.radius > enemy1.hitbox[0] and bullet.x - bullet.radius < enemy1.hitbox[0] + enemy1.hitbox[3]:
enemy1.hit()
bullets.pop(bullets.index(bullet))
##Checking for enemy1 hitbox
if bullet.y < h and bullet.y > 0:
bullet.y -= bullet.speed
else:
bullets.pop(bullets.index(bullet))
## Deletes bullet when offscreen
for shot in enemybullets:
if shot.y - shot.radius > player.x and shot.y - shot.radius < shot.y +30:
if shot.x - shot.radius > player.x and shot.x - shot.radius < player.x + 30:
player.hit()
enemybullets.pop(enemybullets.index(shot))
#### if enemy hits player?
if shot.y> 0 and shot.y < h:
shot.y += shot.speed
else:
enemybullets.pop(enemybullets.index(shot))
###shooting
key = pygame.key.get_pressed()
if key[pygame.K_SPACE]:
if len(bullets) < 15: bullets.append(PlayerShots(round(player.x+15),round(player.y - 5),3,(255,255,255)))
if key[pygame.K_a] and player.x > 0:
player.x -= player.vel
if key[pygame.K_d] and player.x <w - 30:
player.x += player.vel
if key[pygame.K_h]:
if len(enemybullets) < 5:
enemybullets.append(EnemyShots(round(enemy1.x), round(enemy1.y + 35),3,(255,0,0)))
enemy1.movement()
redraw()
pygame.quit()
我已经尝试在Enemyshots.draw()方法中将.x值切换为敌人1.x,但这也没有任何改变。 如果您需要更多信息,请与我们联系。谢谢