飞机大战游戏的Python代码

本文介绍了一个简单的飞机大战游戏的Python代码,使用pygame库创建了飞机和敌机的Sprite类,包括移动、碰撞检测等基本功能。
摘要由CSDN通过智能技术生成

以下是一个简单的飞机大战游戏的Python代码示例:

import pygame  
import random  
  
# 初始化pygame  
pygame.init()  
  
# 设置屏幕大小和标题  
screen_width = 800  
screen_height = 600  
screen = pygame.display.set_mode((screen_width, screen_height))  
pygame.display.set_caption("飞机大战")  
  
# 设置颜色  
WHITE = (255, 255, 255)  
RED = (255, 0, 0)  
GREEN = (0, 255, 0)  
  
# 定义飞机类  
class Plane(pygame.sprite.Sprite):  
    def __init__(self):  
        super().__init__()  
        self.image = pygame.Surface([50, 30])  
        self.image.fill(GREEN)  
        self.rect = self.image.get_rect()  
        self.rect.x = screen_width // 2  
        self.rect.y = screen_height - 40  
        self.change_x = 0  
  
    def update(self):  
        self.rect.x += self.change_x  
        if self.rect.x < 0:  
            self.rect.x = 0  
        if self.rect.x > screen_width - 50:  
            self.rect.x = screen_width - 50  
  
    def go_left(self):  
        self.change_x = -5  
  
    def go_right(self):  
        self.change_x = 5  
  
    def stop(self):  
        self.change_x = 0  
  
# 定义敌人类  
class Enemy(pygame.sprite.Sprite):  
    def __init__(self):  
        super().__init__()  
        self.image = pygame.Surface([40, 25])  
        self.image.fill(RED)  
        self.rect = self.image.get_rect()  
        self.rect.x = random.randint(0, screen_width - 40)  
        self.rect.y = -40  
        self.speed = random.randint(1, 3)  
  
    def update(self):  
        self.rect.y += self.speed  
        if self.rect.y > screen_height:  
            self.rect.y = -40  
            self.rect.x = random.randint(0, screen_width - 40)  
            self.speed = random.randint(1, 3)  
  
# 初始化敌机精灵组和飞机精灵组  
all_sprites = pygame.sprite.Group()  
plane = Plane()  
all_sprites.add(plane)  
enemies = pygame.sprite.Group()  
for i in range(5):  
    enemy = Enemy()  
    all_sprites.add(enemy)  
    enemies.add(enemy)  
  
# 游戏主循环  
running = True  
while running:  
    for event in pygame.event.get():  
        if event.type == pygame.QUIT:  
            running = False  
        elif event.type == pygame.KEYDOWN:  
            if event.key == pygame.K_LEFT:  
                plane.go_left()  
            elif event.key == pygame.K_RIGHT:  
                plane.go_right()  
        elif event.type == pygame.KEYUP:  
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:  
                plane.stop()  
            elif event.key == pygame.K_SPACE:  # 发射子弹,这里需要自己实现子弹类和逻辑,这里省略了子弹类的定义和逻辑实现。

以下是一个简单的飞机大战游戏Python 代码示例: ```python import pygame import random # 初始化 Pygame pygame.init() # 颜色定义 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) # 设置屏幕大小 SCREEN_WIDTH = 700 SCREEN_HEIGHT = 500 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) # 设置游戏标题 pygame.display.set_caption("飞机大战") # 加载图片 player_img = pygame.image.load("player.png").convert() enemy_img = pygame.image.load("enemy.png").convert() # 设置字体 font = pygame.font.Font(None, 36) # 游戏结束标志 game_over = False # 玩家 player_x = 300 player_y = 400 player_speed = 5 # 敌人 enemies = [] enemy_speed = 3 # 得分 score = 0 # 生成敌人 def create_enemy(): enemy_x = random.randint(0, SCREEN_WIDTH - enemy_img.get_width()) enemy_y = -enemy_img.get_height() enemies.append([enemy_x, enemy_y]) # 更新敌人位置 def update_enemies(): for i, enemy in enumerate(enemies): enemy[1] += enemy_speed if enemy[1] > SCREEN_HEIGHT: del enemies[i] # 检查碰撞 def check_collision(): global game_over, score for i, enemy in enumerate(enemies): if (player_x < enemy[0] + enemy_img.get_width() and player_x + player_img.get_width() > enemy[0] and player_y < enemy[1] + enemy_img.get_height() and player_y + player_img.get_height() > enemy[1]): game_over = True score_text = font.render("得分: {}".format(score), True, WHITE) screen.blit(score_text, [SCREEN_WIDTH // 2 - score_text.get_width() // 2, SCREEN_HEIGHT // 2 - score_text.get_height() // 2]) pygame.display.flip() pygame.time.wait(3000) break # 游戏循环 while not game_over: # 处理事件 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: player_x -= player_speed elif event.key == pygame.K_RIGHT: player_x += player_speed # 更新玩家位置 if player_x < 0: player_x = 0 elif player_x > SCREEN_WIDTH - player_img.get_width(): player_x = SCREEN_WIDTH - player_img.get_width() # 更新敌人位置 if len(enemies) < 10: create_enemy() update_enemies() # 检查碰撞 check_collision() # 绘制背景 screen.fill(BLACK) # 绘制玩家 screen.blit(player_img, [player_x, player_y]) # 绘制敌人 for enemy in enemies: screen.blit(enemy_img, enemy) # 显示得分 score += len(enemies) score_text = font.render("得分: {}".format(score), True, WHITE) screen.blit(score_text, [10, 10]) # 更新屏幕 pygame.display.flip() # 退出 Pygame pygame.quit() ``` 这个示例中,玩家可以使用左右键来移动飞机,敌人会从顶部随机位置出现并向下移动,玩家需要躲避敌人并尽可能地击败他们来获得更高的分数。当玩家与敌人发生碰撞时,游戏将结束,并显示最终得分。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值