用python写一个外星人入侵游戏
时间: 2023-05-22 11:05:25 浏览: 96
好的,我可以为您提供一个简单的外星人入侵游戏的 Python 代码示例。以下是代码:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 设置游戏窗口的大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置游戏的标题
pygame.display.set_caption("外星人入侵游戏")
# 加载游戏背景
background = pygame.image.load('bg.jpg')
# 加载玩家的飞船
player = pygame.image.load('player.png')
player_width = player.get_width()
player_height = player.get_height()
player_x = (screen_width - player_width) / 2
player_y = screen_height - player_height - 20
# 加载敌人的飞船
enemy = pygame.image.load('enemy.png')
enemy_width = enemy.get_width()
enemy_height = enemy.get_height()
enemy_x = random.randint(0, screen_width - enemy_width)
enemy_y = - enemy_height
enemy_speed = 3
# 加载子弹
bullet = pygame.image.load('bullet.png')
bullet_width = bullet.get_width()
bullet_height = bullet.get_height()
bullet_x = 0
bullet_y = -1
bullet_speed = 5
bullet_state = "ready"
# 设置游戏循环
running = True
while running:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x -= 5
if event.key == pygame.K_RIGHT:
player_x += 5
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bullet_x = player_x + player_width / 2 - bullet_width / 2
bullet_y = player_y
bullet_state = "firing"
# 移动敌人的飞船
enemy_y += enemy_speed
if enemy_y > screen_height:
enemy_x = random.randint(0, screen_width - enemy_width)
enemy_y = -enemy_height
# 移动子弹
if bullet_state == "firing":
bullet_y -= bullet_speed
if bullet_y < 0:
bullet_state = "ready"
# 检测子弹是否击中敌人
if bullet_state == "firing":
if bullet_x + bullet_width > enemy_x and bullet_x < enemy_x + enemy_width and bullet_y < enemy_y + enemy_height:
bullet_state = "ready"
enemy_x = random.randint(0, screen_width - enemy_width)
enemy_y = -enemy_height
# 将游戏元素绘制到屏幕上
screen.blit(background, (0, 0))
screen.blit(player, (player_x, player_y))
screen.blit(enemy, (enemy_x, enemy_y))
if bullet_state == "firing":
screen.blit(bullet, (bullet_x, bullet_y))
# 更新屏幕显示
pygame.display.update()
# 结束游戏
pygame.quit()
```
这是一个非常简单的游戏,玩家需要控制飞船躲避敌人的攻击并且射杀敌人。游戏代码中有一些注释,以帮助您理解每个部分的作用。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)