首页 > 解决方案 > 敌人的 x 坐标不变(pygame)

问题描述

我目前正在用 pygame 制作潜艇游戏。我想让敌人左右移动。但是,尽管我添加了更改 x 坐标的代码,但它们不会改变。当我在游戏中再添加一个敌人时,错误实际上开始了。在只有一个敌人之前,这个问题没有发生。我试图自己寻找问题,但没有成功。这是代码:

import pygame
pygame.init()
run = True
screen = pygame.display.set_mode((600, 500), pygame.FULLSCREEN)  
pygame.display.set_caption('Podmornca')
background = pygame.image.load('background1.png')
bottom = pygame.image.load('bottom.png')
bottomx = 100
shoot = pygame.mixer.Sound('torpedo-5.wav')
clock = pygame.time.Clock()
enemyL = pygame.image.load('enemyL.png')
enemyD = pygame.image.load('enemyD.png')
torpL = pygame.image.load('torpL.png')
torpD = pygame.image.load('torpD.png')
hit = pygame.mixer.Sound('torpedo-fire.wav')
explosion = pygame.mixer.Sound('explosion.wav')
ekspl = pygame.image.load('explosion.png')
font = pygame.font.SysFont('bauhaus93', 40)
win = font.render('YOU WON!', 1, (255, 0, 0))
lose = font.render('GAME OVER!', 1, (255, 0, 0))
class enemy1():
    def __init__(self, x, y, v):
        self.x = x
        self.y = y
        self.v = v
        self.start = 1300
        self.end = 1800
        self.left = True
        self.right = False
        self.way = -1
        self.projectilex = x
        self.projectiley = y
        self.shot = False
        self.frame = (self.x, self.y, 200, 92)
        self.life = 300 
def shoot(self):
    if self.shot:
        shoot.play()
        if self.left:
            screen.blit(torpL, (self.projectilex, self.projectiley + 40))
            self.projectilex += 10 * self.way
        if self.right:
            screen.blit(torpD, (self.projectilex, self.projectiley + 40))
            self.projectilex += 10 * self.way 
        if self.projectilex > 600 or self.projectilex < 0:
            self.shot = False
            self.projectilex = self.x
            self.projectiley = self.y 
def move(self):
    if self.life > 0:
        if self.left:
            self.frame = (self.x + 10, self.y, 177, 92)
            pygame.draw.rect(screen, (255,0,0), (self.x + 60, self.y - 5, self.life * 0.25, 4 ), 0)
            self.way = -1
            if  self.x > self.end:
                screen.blit(enemyL, (self.x, self.y))
                self.x -= self.v
            if self.x == self.end: 
                self.left = False
                self.right = True
        if self.right:
            self.way = 1
            self.frame = (self.x + 10, self.y, 177, 92)
            if self.x < self.start:
                pygame.draw.rect(screen, (255,0,0), (self.x + 60, self.y - 5, self.life * 0.25, 4 ), 0) 
                screen.blit(enemyD, (self.x, self.y))
                self.x += self.v
            if self.x == self.start:
                self.left = True
                self.right = False
        if self.x - player.x <= 300:
            self.shoot()
            self.shot = True
    else:
        explosion.play()
class enemy2():
    def __init__(self, x, y, v):
        self.x = x
        self.y = y
        self.v = v
        self.start = 1900
        self.end = 2400
        self.left = True
        self.right = False
        self.way = -1
        self.projectilex = x
        self.projectiley = y
        self.shot = False
        self.frame = (self.x, self.y, 200, 92)
        self.frame = 300 
    def shoot(self):
       if self.shot:
           shoot.play()
            if self.left:
                screen.blit(torpL, (self.projectilex, self.projectiley + 40))
                self.projectilex += 10 * self.way
            if self.right:
                screen.blit(torpD, (self.projectilex, self.projectiley + 40))
                self.projectilex += 10 * self.way 
            if self.projectilex > 600 or self.projectilex < 0:
                self.shot = False
                self.projectilex = self.x
                self.projectiley = self.y
def move(self):
    if self.life > 0:
        if self.left:
            self.frame = (self.x + 10, self.y, 177, 92)
            pygame.draw.rect(screen, (255,0,0), (self.x + 60, self.y - 5, self.life * 0.25, 4 ), 0)
            self.way = -1
            if  self.x > self.end:
                screen.blit(enemyL, (self.x, self.y))
                self.x -= self.v
            if self.x == self.end: 
                self.left = False
                self.right = True
        if self.right:
            self.way = 1
            self.frame = (self.x + 10, self.y, 177, 92)
            if self.x < self.start:
                pygame.draw.rect(screen, (255,0,0), (self.x + 60, self.y - 5, self.life * 0.25, 4), 0) 
                screen.blit(enemyD, (self.x, self.y))
                self.x += self.v
            if self.x == self.start:
                self.left = True
                self.right = False
    else:
        explosion.play()
submarine1 = enemy1(1300, 250, 5)
submarine2 = enemy2(1900, 250, 5)
right = True
left = False 
def graphics():
    global bottomx
    clock.tick(60)
    screen.blit(background, (0,0))
    screen.blit(bottom, (bottomx, 250))
    if bottomx < -594:
        bottomx = 1194
    submarine1.move()
    podmornca2.premik()
    pygame.display.flip()
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            run = False
    if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
        bottomx += 5
        submarine1.start += 5
        submarine2.start += 5
        submarine1.end += 5
        submarine2.end += 5
    if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
        bottomx -= 5
        submarine1.start -= 5
        submarine2.start -= 5
        submarine1.end -= 5
        submarine2.end -= 5
    if submarine1.life < 0 and submarine2.life < 0:
        screen.blit(ekspl, (submarine1.x, submarine1.y))
        screen.blit(ekspl, (submarine1.x + 5, submarine1.y))
        screen.blit(ekspl, (submarine2.x, submarine2.y))
        screen.blit(ekspl, (submarine2.x + 5, submarine2.y))
        screen.blit(end, (200, 250)) 
    graphics()
pygame.quit()

标签: pythonpygame

解决方案


有几件事可以帮助...

  1. 如上所述,只需使用一个敌方等级并从中派生两艘潜艇,例如:
sub1 = enemy(...)

sub2 = enemy(...)
  1. 您向右移动的主要问题是您的起点和终点颠倒了。您向右移动到起点,因此start必须大于end你让他们翻转。

  2. 在几个地方,您应该使用if-elseorif-elif-else代替一堆if语句来清理逻辑并确保其中一个或另一个执行。

祝你好运!


推荐阅读