python - 敌人的 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()
解决方案
有几件事可以帮助...
- 如上所述,只需使用一个敌方等级并从中派生两艘潜艇,例如:
sub1 = enemy(...) sub2 = enemy(...)
您向右移动的主要问题是您的起点和终点颠倒了。您向右移动到起点,因此
start
必须大于。end
你让他们翻转。在几个地方,您应该使用
if-else
orif-elif-else
代替一堆if
语句来清理逻辑并确保其中一个或另一个执行。
祝你好运!
推荐阅读
- pdo - 在 PHP 中遇到 PDO 问题
- openstreetmap - 如何使用 OSMFilter 仅从 DB 中提取方式标记而无需其他实体
- php - Laravel Http 客户端使用 PUT 方法接收 419 响应 200 使用 GET
- flutter - 是否有任何键盘快捷键可以覆盖 Visual Studio Flutter 中的超类方法
- react-native - 反应原生纸张标题 Appbar.Content 问题
- ubuntu - 为什么 POI(excel 文件)在不同的 ubuntu 系统上创建不同
- javascript - 是否可以使用 discordjs 机器人命令设置自定义不和谐存在/活动/状态?
- c# - 是否可以更改 WinFormCore DataGridViewCell 中的 cell.value
- azure-data-factory - 自动创建的表的 Azure 数据工厂复制活动自定义架构名称
- python - 使用神经网络根据体重和身高预测年龄