用pygame 做精灵组飞机大战
时间: 2023-09-05 10:11:50 浏览: 104
基于pygame实现飞机大战的游戏
好的,我可以为你提供一些基本的代码框架和思路来实现一个简单的精灵组飞机大战游戏。
首先,你需要安装Pygame模块,然后导入它和其他必要的模块:
```python
import pygame
import random
import os
```
接下来,你需要初始化Pygame并设置游戏窗口的大小和标题:
```python
pygame.init()
WIDTH = 480
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("飞机大战")
```
然后你需要定义几个常量,如颜色、速度和图片等:
```python
# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# 速度
FPS = 60
PLAYER_SPEED = 5
ENEMY_SPEED = 3
BULLET_SPEED = 7
# 图片
background_img = pygame.image.load(os.path.join("img", "background.png"))
player_img = pygame.image.load(os.path.join("img", "player.png"))
enemy_img = pygame.image.load(os.path.join("img", "enemy.png"))
bullet_img = pygame.image.load(os.path.join("img", "bullet.png"))
```
接下来,你需要创建玩家和敌人的类:
```python
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = player_img
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 = -PLAYER_SPEED
if keystate[pygame.K_RIGHT]:
self.speedx = PLAYER_SPEED
self.rect.x += self.speedx
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.left < 0:
self.rect.left = 0
def shoot(self):
bullet = Bullet(self.rect.centerx, self.rect.top)
all_sprites.add(bullet)
bullets.add(bullet)
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = enemy_img
self.rect = self.image.get_rect()
self.rect.x = random.randrange(0, WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = ENEMY_SPEED
def update(self):
self.rect.y += self.speedy
if self.rect.top > HEIGHT + 10:
self.rect.x = random.randrange(0, WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = ENEMY_SPEED + random.randrange(0, 3)
```
然后你需要创建子弹的类:
```python
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = bullet_img
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speedy = -BULLET_SPEED
def update(self):
self.rect.y += self.speedy
if self.rect.bottom < 0:
self.kill()
```
接下来,你需要创建一些精灵组:
```python
all_sprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
```
然后你需要创建一些敌人并添加到精灵组中:
```python
for i in range(8):
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
```
接下来,你需要创建一个游戏循环并处理事件:
```python
running = True
clock = pygame.time.Clock()
while running:
clock.tick(FPS)
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.shoot()
# 更新
all_sprites.update()
# 碰撞检测
hits = pygame.sprite.groupcollide(enemies, bullets, True, True)
for hit in hits:
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
hits = pygame.sprite.spritecollide(player, enemies, False)
if hits:
running = False
# 绘制
screen.blit(background_img, (0, 0))
all_sprites.draw(screen)
# 更新屏幕
pygame.display.flip()
pygame.quit()
```
最后,你需要记得在程序结束时调用`pygame.quit()`来退出Pygame。
这只是一个简单的游戏框架,你可以根据自己的需要进行修改和扩展。
阅读全文