pygame小游戏代码
时间: 2023-07-23 15:49:47 浏览: 85
Python小游戏代码
以下是一个简单的 Pygame 小游戏代码示例,它是一个基于键盘控制的飞机射击游戏:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置窗口大小和标题
screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("飞机射击小游戏")
# 加载背景音乐
pygame.mixer.music.load("bg_music.mp3")
pygame.mixer.music.play(-1)
# 加载图片和音效
player_img = pygame.image.load("player.png")
player_bullet_img = pygame.image.load("player_bullet.png")
enemy_img = pygame.image.load("enemy.png")
enemy_bullet_img = pygame.image.load("enemy_bullet.png")
explosion_sound = pygame.mixer.Sound("explosion.wav")
shoot_sound = pygame.mixer.Sound("shoot.wav")
# 设置游戏变量
player_x = 300
player_y = 400
player_speed = 5
player_bullet_speed = 10
player_bullets = []
enemy_speed = 3
enemies = []
enemy_bullet_speed = 5
enemy_bullets = []
score = 0
font = pygame.font.Font(None, 30)
# 游戏循环
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:
# 发射子弹
player_bullets.append([player_x + 25, player_y])
shoot_sound.play()
# 移动玩家
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 < screen_width - 50:
player_x += player_speed
# 移动玩家子弹
for bullet in player_bullets:
bullet[1] -= player_bullet_speed
if bullet[1] < -20:
player_bullets.remove(bullet)
# 移动敌人
for enemy in enemies:
enemy[1] += enemy_speed
if enemy[1] > screen_height + 50:
enemies.remove(enemy)
# 移动敌人子弹
for bullet in enemy_bullets:
bullet[1] += enemy_bullet_speed
if bullet[1] > screen_height + 20:
enemy_bullets.remove(bullet)
# 检测碰撞
for bullet in player_bullets:
for enemy in enemies:
if (bullet[0] > enemy[0] and bullet[0] < enemy[0] + 50 and
bullet[1] > enemy[1] and bullet[1] < enemy[1] + 50):
player_bullets.remove(bullet)
enemies.remove(enemy)
explosion_sound.play()
score += 10
for bullet in enemy_bullets:
if (bullet[0] > player_x and bullet[0] < player_x + 50 and
bullet[1] > player_y and bullet[1] < player_y + 50):
enemy_bullets.remove(bullet)
explosion_sound.play()
running = False
# 生成敌人
if len(enemies) < 10 and random.random() < 0.1:
enemies.append([random.randint(0, screen_width - 50), -50])
# 发射敌人子弹
for enemy in enemies:
if random.random() < 0.01:
enemy_bullets.append([enemy[0] + 25, enemy[1] + 50])
# 绘制屏幕
screen.fill((0, 0, 0))
screen.blit(player_img, (player_x, player_y))
for bullet in player_bullets:
screen.blit(player_bullet_img, (bullet[0], bullet[1]))
for enemy in enemies:
screen.blit(enemy_img, (enemy[0], enemy[1]))
for bullet in enemy_bullets:
screen.blit(enemy_bullet_img, (bullet[0], bullet[1]))
score_text = font.render("得分:" + str(score), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
pygame.display.update()
# 游戏结束,显示最终得分
game_over_text = font.render("游戏结束,得分:" + str(score), True, (255, 255, 255))
screen.blit(game_over_text, (screen_width // 2 - 150, screen_height // 2))
pygame.display.update()
# 延迟 3 秒后退出游戏
pygame.time.delay(3000)
pygame.quit()
```
这只是一个简单的示例,你可以根据自己的需求和想象力进行修改和扩展。
阅读全文