用python写一个小飞机的程序
时间: 2023-06-03 12:02:44 浏览: 220
好的,关于这个问题,我可以给你一些指导和建议。首先,你需要使用一个图形化界面库,比如Pygame,来创建游戏窗口和显示飞机等元素。然后,你需要定义飞机的属性,比如位置、速度、生命等,在程序中用变量来保存并随时更新。接着,你可以使用键盘输入事件来控制飞机的移动和发射子弹等行为。最后,你需要编写游戏逻辑,包括碰撞检测、分数计算、游戏结束等功能。希望这些提示能对你有所帮助。
相关问题
用Python写一个小飞机游戏程序
好的,以下是一个使用Python和Pygame库编写的小飞机游戏程序示例:
```python
import pygame
import random
# 初始化 Pygame 库
pygame.init()
# 定义游戏窗口的大小
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 800
SCREEN_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)
# 设置窗口标题
pygame.display.set_caption("小飞机游戏")
# 创建游戏窗口
screen = pygame.display.set_mode(SCREEN_SIZE)
# 加载游戏图片资源
bg_img = pygame.image.load("images/background.png")
plane_img = pygame.image.load("images/plane.png")
bullet_img = pygame.image.load("images/bullet.png")
enemy_img = pygame.image.load("images/enemy.png")
# 定义游戏变量
score = 0
font = pygame.font.SysFont(None, 32)
# 定义游戏角色
class Plane(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = plane_img
self.rect = self.image.get_rect()
self.rect.centerx = SCREEN_WIDTH // 2
self.rect.bottom = SCREEN_HEIGHT - 20
self.speed = 5
self.bullets = pygame.sprite.Group()
def update(self):
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_LEFT]:
self.rect.x -= self.speed
if key_pressed[pygame.K_RIGHT]:
self.rect.x += self.speed
if key_pressed[pygame.K_SPACE]:
bullet = Bullet()
bullet.rect.centerx = self.rect.centerx
bullet.rect.top = self.rect.top
self.bullets.add(bullet)
# 控制飞机不飞出屏幕
if self.rect.left < 0:
self.rect.left = 0
elif self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
# 更新子弹
self.bullets.update()
def draw(self, surface):
surface.blit(self.image, self.rect)
self.bullets.draw(surface)
class Bullet(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = bullet_img
self.rect = self.image.get_rect()
self.speed = -10
def update(self):
self.rect.y += self.speed
if self.rect.bottom < 0:
self.kill()
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = enemy_img
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
self.rect.y = random.randint(-SCREEN_HEIGHT, -self.rect.height)
self.speed = random.randint(1, 5)
def update(self):
self.rect.y += self.speed
if self.rect.top > SCREEN_HEIGHT:
self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
self.rect.y = random.randint(-SCREEN_HEIGHT, -self.rect.height)
self.speed = random.randint(1, 5)
# 创建游戏角色
player = Plane()
enemies = pygame.sprite.Group()
for i in range(10):
enemy = Enemy()
enemies.add(enemy)
# 游戏主循环
while True:
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 更新游戏角色
player.update
使用python写一个飞机小游戏实例
好的,以下是一个简单的飞机小游戏实例的 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()
```
请注意,此程序的图像和声音文件必须存在于程序目录中。
阅读全文