Python 写个飞机大战的代码
时间: 2023-11-05 10:16:00 浏览: 88
抱歉,作为AI语言模型,我无法编写代码。但是,以下是一个简单的飞机大战游戏的代码示例,供您参考:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 设置窗口
WIDTH = 500
HEIGHT = 600
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("飞机大战")
# 加载图片
bg_img = pygame.image.load('background.png')
player_img = pygame.image.load('player.png')
enemy_img = pygame.image.load('enemy.png')
# 设置游戏参数
player_width = player_img.get_width()
player_height = player_img.get_height()
enemy_width = enemy_img.get_width()
enemy_height = enemy_img.get_height()
player_x = WIDTH / 2 - player_width / 2
player_y = HEIGHT - player_height - 10
player_speed = 5
enemy_speed = 3
enemy_spawn_rate = 30
enemies = []
# 设置字体
font = pygame.font.SysFont('comicsans', 30)
# 定义函数
def draw_window():
win.blit(bg_img, (0, 0))
win.blit(player_img, (player_x, player_y))
for enemy in enemies:
win.blit(enemy_img, (enemy['x'], enemy['y']))
score_text = font.render("Score: " + str(score), 1, (255, 255, 255))
win.blit(score_text, (WIDTH - score_text.get_width() - 10, 10))
pygame.display.update()
def spawn_enemy():
x = random.randint(0, WIDTH - enemy_width)
y = -enemy_height
enemies.append({'x': x, 'y': y})
# 开始游戏循环
score = 0
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# 移动玩家
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < WIDTH - player_width:
player_x += player_speed
# 生成敌人
if random.randint(0, enemy_spawn_rate) == 0:
spawn_enemy()
# 移动敌人
for enemy in enemies:
enemy['y'] += enemy_speed
# 检测碰撞
for enemy in enemies:
if player_x < enemy['x'] + enemy_width and player_x + player_width > enemy['x'] and player_y < enemy['y'] + enemy_height and player_y + player_height > enemy['y']:
run = False
# 删除飞出屏幕的敌人
enemies = [enemy for enemy in enemies if enemy['y'] < HEIGHT]
# 更新分数
score += len(enemies)
# 绘制游戏界面
draw_window()
# 结束游戏
pygame.quit()
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)