用Python写一个飞机大战的游戏
时间: 2024-04-10 16:20:56 浏览: 38
Python编写飞机大战游戏
好的,下面是用Python实现的简单飞机大战游戏。
首先,我们需要引入pygame库,它可以帮助我们创建游戏窗口、加载图片、播放音乐等。
```python
import pygame
from pygame.locals import *
import sys
import random
```
接下来,定义一些常量,如窗口大小、背景音乐等。
```python
WINDOW_WIDTH = 512
WINDOW_HEIGHT = 768
FPS = 60
BACKGROUND_IMAGE = 'images/background.png'
GAME_MUSIC = 'sounds/game_music.ogg'
BULLET_SOUND = 'sounds/bullet.wav'
ENEMY_DOWN_SOUND = 'sounds/enemy_down.wav'
GAME_OVER_SOUND = 'sounds/game_over.wav'
```
然后,创建游戏窗口,加载背景图片和音乐。
```python
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(GAME_MUSIC)
pygame.mixer.music.play(-1)
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Plane Game')
background = pygame.image.load(BACKGROUND_IMAGE)
```
接下来,定义一个函数,用于加载并返回图片。
```python
def load_image(image_file):
image = pygame.image.load(image_file)
return image
```
然后,定义飞机和子弹的类。
```python
class Plane(pygame.sprite.Sprite):
def __init__(self, image_file, x, y):
super().__init__()
self.image = load_image(image_file)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = 5
self.bullets = pygame.sprite.Group()
def move_left(self):
self.rect.x -= self.speed
def move_right(self):
self.rect.x += self.speed
def move_up(self):
self.rect.y -= self.speed
def move_down(self):
self.rect.y += self.speed
def fire(self):
bullet = Bullet('images/bullet.png', self.rect.centerx, self.rect.top)
self.bullets.add(bullet)
def update(self):
self.bullets.update()
for bullet in self.bullets.sprites():
if bullet.rect.bottom < 0:
self.bullets.remove(bullet)
def draw(self, surface):
surface.blit(self.image, self.rect)
self.bullets.draw(surface)
class Bullet(pygame.sprite.Sprite):
def __init__(self, image_file, x, y):
super().__init__()
self.image = load_image(image_file)
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speed = 10
def update(self):
self.rect.y -= self.speed
```
接下来,定义敌机的类。
```python
class Enemy(pygame.sprite.Sprite):
def __init__(self, image_file, x, y):
super().__init__()
self.image = load_image(image_file)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = random.randint(2, 5)
def update(self):
self.rect.y += self.speed
if self.rect.top > WINDOW_HEIGHT:
self.kill()
```
然后,定义游戏结束的函数。
```python
def game_over():
pygame.mixer.music.stop()
game_over_sound = pygame.mixer.Sound(GAME_OVER_SOUND)
game_over_sound.play()
game_over_font = pygame.font.SysFont(None, 48)
game_over_text = game_over_font.render('Game Over', True, (255, 255, 255))
game_over_rect = game_over_text.get_rect()
game_over_rect.centerx = window.get_rect().centerx
game_over_rect.centery = window.get_rect().centery
window.blit(game_over_text, game_over_rect)
pygame.display.update()
pygame.time.delay(2000)
sys.exit()
```
最后,我们可以在游戏主循环中实现游戏的逻辑。
```python
def main():
pygame.time.set_timer(USEREVENT + 1, 1000)
pygame.time.set_timer(USEREVENT + 2, 1000)
pygame.time.set_timer(USEREVENT + 3, 100)
player = Plane('images/player.png', WINDOW_WIDTH // 2, WINDOW_HEIGHT - 120)
player_group = pygame.sprite.Group(player)
enemy_group = pygame.sprite.Group()
score = 0
score_font = pygame.font.SysFont(None, 32)
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == USEREVENT + 1:
enemy = Enemy('images/enemy.png', random.randint(0, WINDOW_WIDTH - 50), -50)
enemy_group.add(enemy)
elif event.type == USEREVENT + 2:
enemy = Enemy('images/enemy2.png', random.randint(0, WINDOW_WIDTH - 80), -80)
enemy_group.add(enemy)
elif event.type == USEREVENT + 3:
pygame.sprite.groupcollide(enemy_group, player.bullets, True, True)
enemies = pygame.sprite.spritecollide(player, enemy_group, False)
if enemies:
game_over()
window.blit(background, (0, 0))
player_group.update()
player_group.draw(window)
enemy_group.update()
enemy_group.draw(window)
score_text = score_font.render('Score: {}'.format(score), True, (255, 255, 255))
window.blit(score_text, (10, 10))
pygame.display.update()
pressed_keys = pygame.key.get_pressed()
if pressed_keys[K_LEFT]:
player.move_left()
elif pressed_keys[K_RIGHT]:
player.move_right()
elif pressed_keys[K_UP]:
player.move_up()
elif pressed_keys[K_DOWN]:
player.move_down()
elif pressed_keys[K_SPACE]:
player.fire()
score += len(pygame.sprite.groupcollide(player.bullets, enemy_group, True, True))
if len(enemy_group) > 5:
game_over()
pygame.time.Clock().tick(FPS)
if __name__ == '__main__':
main()
```
这样,一个简单的飞机大战游戏就完成了。你可以根据自己的喜好和需求对其进行修改和扩展。
阅读全文