python游戏:让子弹飞代码
时间: 2024-06-20 11:03:25 浏览: 10
《让子弹飞》是一款非常有趣的射击游戏,这个游戏也可以通过Python语言来实现。下面是一份简单的代码示例,你可以了解一下:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 设置屏幕大小
screen_width = 480
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置窗口标题
pygame.display.set_caption("让子弹飞")
# 加载背景图像
bg_img = pygame.image.load("background.png")
# 加载玩家飞机图像
player_img = pygame.image.load("player.png")
player_rect = player_img.get_rect()
player_rect.centerx = screen_width // 2
player_rect.bottom = screen_height - 10
# 子弹图像和列表
bullet_img = pygame.image.load("bullet.png")
bullet_list = []
# 敌机图像和列表
enemy_img_list = [
pygame.image.load("enemy1.png"),
pygame.image.load("enemy2.png"),
pygame.image.load("enemy3.png")
]
enemy_list = []
# 控制敌机出现的频率
enemy_frequency = 0
# 分数和字体
score = 0
font = pygame.font.SysFont(None, 32)
# 游戏循环
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_rect.x -= 5
elif event.key == pygame.K_RIGHT:
player_rect.x += 5
elif event.key == pygame.K_SPACE:
bullet_rect = bullet_img.get_rect()
bullet_rect.centerx = player_rect.centerx
bullet_rect.bottom = player_rect.top
bullet_list.append(bullet_rect)
# 更新子弹位置
for bullet_rect in bullet_list:
bullet_rect.y -= 10
if bullet_rect.bottom < 0:
bullet_list.remove(bullet_rect)
# 控制敌机出现的频率,并更新敌机位置
enemy_frequency += 1
if enemy_frequency % 30 == 0:
enemy_img = random.choice(enemy_img_list)
enemy_rect = enemy_img.get_rect()
enemy_rect.centerx = random.randint(0, screen_width)
enemy_rect.bottom = 0
enemy_list.append(enemy_rect)
for enemy_rect in enemy_list:
enemy_rect.y += 5
if enemy_rect.top > screen_height:
enemy_list.remove(enemy_rect)
# 检测子弹是否击中敌机,并更新分数
for bullet_rect in bullet_list:
for enemy_rect in enemy_list:
if bullet_rect.colliderect(enemy_rect):
score += 10
bullet_list.remove(bullet_rect)
enemy_list.remove(enemy_rect)
# 绘制屏幕元素
screen.blit(bg_img, (0, 0))
for bullet_rect in bullet_list:
screen.blit(bullet_img, bullet_rect)
for enemy_rect in enemy_list:
screen.blit(enemy_img, enemy_rect)
screen.blit(player_img, player_rect)
score_text = font.render("Score: {}".format(score), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
# 更新屏幕并控制帧率
pygame.display.update()
pygame.time.Clock().tick(60)
# 退出Pygame
pygame.quit()
```
上述代码实现了一个简单的让子弹飞游戏,通过按键控制玩家飞机移动,发射子弹并打击敌机。你可以根据自己的需求对代码进行修改和优化。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)