添加敌人
class Mob(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((30, 40))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
def update(self):
self.rect.y += self.speedy
if self.rect.top > HEIGHT + 10:
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
player = Player()
all_sprites.add(player)
for i in range(8):
m = Mob()
all_sprites.add(m)
mobs.add(m)
敌人 在 x 方向上 添加一点移动
class Mob(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((30, 40))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
self.speedx = random.randrange(-3, 3)
def update(self):
self.rect.x += self.speedx
self.rect.y += self.speedy
if self.rect.top > HEIGHT + 10 or self.rect.left < -25 or self.rect.right > WIDTH + 20:
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
完整代码:
import pygame
import random
WIDTH = 480 # 屏幕宽度
HEIGHT = 600 # 屏幕高度
FPS = 60 # 帧率
# 定义颜色
WHITE = (255, 255, 255) # 白色
BLACK = (0, 0, 0) # 黑色
RED = (255, 0, 0) # 红色
GREEN = (0, 255, 0) # 绿色
BLUE = (0, 0, 255) # 蓝色
YELLOW = (255, 255, 0) # 黄色
# 初始化pygame并创建窗口
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("太空狙击!")
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 40))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
# 初始飞机位置
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT - 10
self.speedx = 0 # 水平速度
def update(self):
self.speedx = 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 self.rect.right > WIDTH: # 如果玩家精灵的右边缘超出屏幕宽度
self.rect.right = WIDTH
if self.rect.left < 0: # 如果玩家精灵的左边缘小于0(超出屏幕左侧)
self.rect.left = 0
class Mob(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((30, 40))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
self.speedx = random.randrange(-3, 3)
def update(self):
self.rect.x += self.speedx
self.rect.y += self.speedy
if self.rect.top > HEIGHT + 10 or self.rect.left < -25 or self.rect.right > WIDTH + 20:
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
all_sprites = pygame.sprite.Group() # 创建精灵组
mobs = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
m = Mob()
all_sprites.add(m)
mobs.add(m)
# 游戏主循环
running = True
while running:
# 保持循环以正确的速度运行
clock.tick(FPS)
# 处理输入(事件)
for event in pygame.event.get():
# 检查是否关闭窗口
if event.type == pygame.QUIT:
running = False
# 更新
all_sprites.update()
# 绘制/渲染
screen.fill(BLACK) # 填充屏幕为黑色
all_sprites.draw(screen) # 在屏幕上绘制所有角色
# 在绘制完所有内容后,更新显示
pygame.display.flip()
pygame.quit()