我创建了一个带有pygame.rect的敌方职业,我试图将其在屏幕上从右移至左,然后向右移,并在循环中重复进行。到目前为止,在敌人更新中,我只能从右向左移动,然后矩形停止。这是因为它不断更新导致代码无法正常工作吗?我不确定如何实现双向发展。
# Pygame template - skeleton for a new pygame project
import pygame
import random
import os
# set up asset folders
os.environ['SDL_VIDEO_CENTERED'] = '1'
game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, 'img')
# Screen resolution and frames
WIDTH = 480
HEIGHT = 600
FPS = 60
# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# initialize pygame and create window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shmup!")
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((100, 40))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT - 10
self.speedx = 0
self.speedy = 0
def update(self):
self.speedx = 0
self.speedy = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.speedx = -8
if keystate[pygame.K_RIGHT]:
self.speedx = 8
self.rect.x += self.speedx
if keystate[pygame.K_UP]:
self.speedy = -8
if keystate[pygame.K_DOWN]:
self.speedy = 8
self.rect.y += self.speedy
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.left < 0:
self.rect.left = 0
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
if self.rect.top < 100:
self.rect.top = 100
class Enemies(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 40))
self.image.fill(BLACK)
self.rect = self.image.get_rect()
self.rect.x = WIDTH - 10
self.rect.bottom = HEIGHT / 2
self.speedx = 0
def update(self):
self.speedx = 0
if self.rect.right > 0:
self.speedx = 2
self.speedx = -self.speedx
if self.rect.left < 0:
self.speedx *= -1
self.speedx = self.speedx
self.rect.x += self.speedx
all_sprites = pygame.sprite.Group()
player = Player()
enemies = Enemies()
all_sprites.add(player)
all_sprites.add(enemies)
# Game Loop
running = True
while running:
# keep loop running at the right speed
clock.tick(FPS)
# Process input (events)
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
running = False
# Update
all_sprites.update()
#Draw / render
screen.fill(BLUE)
all_sprites.draw(screen)
# *after* drawing everything
pygame.display.flip()
pygame.quit()
quit()
答案 0 :(得分:1)
不要把事情复杂化。在构造函数中初始化恒定速度:
class Enemies(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
# [...]
self.speedx = 2
在2种情况下,您必须更改速度。
如果敌人在左边(self.rect.left <= 0
),则必须将移动方向更改为右边,这意味着self.speedx
必须为正。使用abs(x)
使速度为正(例如self.speedx = abs(self.speedx)
)。
如果敌人在右边(self.rect.right >= WIDTH
),则必须将直接方向更改为左边。因此速度必须为负数(例如self.speedx = -abs(self.speedx)
)。
以下代码即使敌人不在窗外,也总是将敌人朝正确的方向移动:
class Enemies(pygame.sprite.Sprite):
# [...]
def update(self):
if self.rect.left <= 0:
self.speedx = abs(self.speedx)
elif self.rect.right >= WIDTH:
self.speedx = -abs(self.speedx)
self.rect.x += self.speedx