使用python写一个飞机小游戏实例
时间: 2024-05-13 19:14:12 浏览: 139
好的,以下是一个简单的飞机小游戏实例的 Python 代码:
``` python
import pygame
import random
pygame.init()
# Set up the display
screen_width = 480
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Aircraft War')
# Load images
background_img = pygame.image.load('images/background.png')
aircraft_img = pygame.image.load('images/aircraft.png')
bullet_img = pygame.image.load('images/bullet.png')
enemy_small_img = pygame.image.load('images/enemy_small.png')
enemy_middle_img = pygame.image.load('images/enemy_middle.png')
enemy_big_img = pygame.image.load('images/enemy_big.png')
# Load sounds
bullet_sound = pygame.mixer.Sound('sounds/bullet.wav')
enemy_destroyed_sound = pygame.mixer.Sound('sounds/enemy_destroyed.wav')
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# Define the aircraft
class Aircraft(pygame.sprite.Sprite):
def __init__(self):
super(Aircraft, self).__init__()
self.image = aircraft_img
self.rect = self.image.get_rect()
self.rect.centerx = screen_width/2
self.rect.bottom = screen_height - 10
self.speed = 5
def move_left(self):
self.rect.x -= self.speed
if self.rect.x < 0:
self.rect.x = 0
def move_right(self):
self.rect.x += self.speed
if self.rect.right > screen_width:
self.rect.right = screen_width
def move_up(self):
self.rect.y -= self.speed
if self.rect.y < 0:
self.rect.y = 0
def move_down(self):
self.rect.y += self.speed
if self.rect.bottom > screen_height:
self.rect.bottom = screen_height
def fire_bullet(self):
bullet = Bullet(self.rect.centerx, self.rect.top)
all_sprites_list.add(bullet)
bullet_list.add(bullet)
bullet_sound.play()
# Define the bullet
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super(Bullet, self).__init__()
self.image = bullet_img
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
if self.rect.bottom < 0:
self.kill()
# Define the enemy
class Enemy(pygame.sprite.Sprite):
def __init__(self, img, speed):
super(Enemy, self).__init__()
self.image = img
self.rect = self.image.get_rect()
self.rect.x = random.randrange(0, screen_width - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speed = speed
def update(self):
self.rect.y += self.speed
if self.rect.top > screen_height:
self.kill()
# Set up groups
all_sprites_list = pygame.sprite.Group()
enemy_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()
# Set up the aircraft
aircraft = Aircraft()
all_sprites_list.add(aircraft)
# Set up the enemies
for i in range(8):
enemy = Enemy(enemy_small_img, random.randrange(1, 4))
all_sprites_list.add(enemy)
enemy_list.add(enemy)
# Set up the clock
clock = pygame.time.Clock()
# Set up the score
font = pygame.font.SysFont(None, 25)
score = 0
# Game loop
game_over = False
while not game_over:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
aircraft.move_left()
elif event.key == pygame.K_RIGHT:
aircraft.move_right()
elif event.key == pygame.K_UP:
aircraft.move_up()
elif event.key == pygame.K_DOWN:
aircraft.move_down()
elif event.key == pygame.K_SPACE:
aircraft.fire_bullet()
# Update the game
all_sprites_list.update()
# Check for collisions between the aircraft and enemies
enemy_hit_list = pygame.sprite.spritecollide(aircraft, enemy_list, True)
for enemy in enemy_hit_list:
enemy_destroyed_sound.play()
# Check for collisions between the bullets and enemies
for bullet in bullet_list:
enemy_hit_list = pygame.sprite.spritecollide(bullet, enemy_list, True)
for enemy in enemy_hit_list:
enemy_destroyed_sound.play()
score += 1
bullet.kill()
# Add new enemies
while len(enemy_list) < 8:
enemy = Enemy(random.choice([enemy_small_img, enemy_middle_img, enemy_big_img]), random.randrange(1, 4))
all_sprites_list.add(enemy)
enemy_list.add(enemy)
# Draw the game
screen.blit(background_img, (0, 0))
all_sprites_list.draw(screen)
score_text = font.render('Score: {}'.format(score), True, WHITE)
screen.blit(score_text, (10, 10))
# Update the display
pygame.display.update()
# Set the frame rate
clock.tick(60)
# Quit the game
pygame.quit()
```
请注意,此程序的图像和声音文件必须存在于程序目录中。
阅读全文