手写代码,飞机大战小游戏
时间: 2023-10-06 18:12:16 浏览: 36
好的,以下是一个简单的飞机大战小游戏的手写代码(使用Python和Pygame库):
```
import pygame
import random
# 初始化Pygame
pygame.init()
# 游戏窗口的宽度和高度
WIDTH = 600
HEIGHT = 800
# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# 设置窗口标题
pygame.display.set_caption("飞机大战")
# 加载背景图片
background_image = pygame.image.load("background.jpg")
# 加载玩家飞机图片
player_image = pygame.image.load("player.png")
# 加载敌机图片
enemy_image = pygame.image.load("enemy.png")
# 玩家飞机的初始位置
player_x = WIDTH / 2
player_y = HEIGHT - 100
# 创建玩家飞机
player_rect = player_image.get_rect()
player_rect.x = player_x
player_rect.y = player_y
# 子弹列表
bullet_list = []
# 敌机列表
enemy_list = []
# 敌机的速度
enemy_speed = 5
# 得分
score = 0
# 游戏循环
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_SPACE:
# 发射子弹
bullet_list.append([player_x + 45, player_y - 20])
# 移动玩家飞机
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= 5
elif keys[pygame.K_RIGHT] and player_x < WIDTH - player_rect.width:
player_x += 5
# 清空屏幕
screen.fill(WHITE)
# 绘制背景
screen.blit(background_image, [0, 0])
# 绘制玩家飞机
player_rect.x = player_x
screen.blit(player_image, player_rect)
# 绘制子弹
for bullet in bullet_list:
pygame.draw.rect(screen, BLACK, [bullet[0], bullet[1], 5, 10])
bullet[1] -= 10
if bullet[1] < 0:
bullet_list.remove(bullet)
# 创建敌机
if random.randint(1, 50) == 1:
enemy_x = random.randint(0, WIDTH - enemy_image.get_width())
enemy_rect = enemy_image.get_rect()
enemy_rect.x = enemy_x
enemy_rect.y = -enemy_image.get_height()
enemy_list.append(enemy_rect)
# 绘制敌机
for enemy in enemy_list:
screen.blit(enemy_image, enemy)
enemy.y += enemy_speed
if enemy.y > HEIGHT:
enemy_list.remove(enemy)
# 碰撞检测
for enemy in enemy_list:
if player_rect.colliderect(enemy):
running = False
for bullet in bullet_list:
if bullet[1] < enemy.y + enemy_image.get_height() and \
bullet[1] + 10 > enemy.y and \
bullet[0] + 5 > enemy.x and \
bullet[0] < enemy.x + enemy_image.get_width():
bullet_list.remove(bullet)
enemy_list.remove(enemy)
score += 10
# 显示得分
font = pygame.font.Font(None, 36)
text = font.render("得分: " + str(score), True, BLACK)
screen.blit(text, [10, 10])
# 更新屏幕
pygame.display.update()
# 退出Pygame
pygame.quit()
```
请注意,这只是一个简单的实现,您可以根据自己的需求进行修改和扩展。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)