python小游戏有积分和关卡代码
时间: 2023-09-03 15:11:38 浏览: 148
好的,我可以给你提供一个简单的Python打飞机游戏代码,其中包括了积分和关卡的实现。代码如下:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 定义屏幕大小和帧率
screen_width = 480
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("打飞机")
clock = pygame.time.Clock()
FPS = 60
# 加载游戏资源
background = pygame.image.load("images/background.png").convert()
player_img = pygame.image.load("images/player.png").convert_alpha()
player_rect = player_img.get_rect()
enemy_img = pygame.image.load("images/enemy.png").convert_alpha()
bullet_img = pygame.image.load("images/bullet.png").convert_alpha()
# 定义玩家、敌机、子弹和得分
player_speed = 5
player_rect.left = (screen_width - player_rect.width) // 2
player_rect.top = screen_height - player_rect.height - 50
player_score = 0
enemies = []
bullets = []
max_enemy_speed = 5
max_enemies = 10
current_wave = 1
# 定义文字
font = pygame.font.SysFont("arial", 24)
score_text = font.render("得分:%d" % player_score, True, (255, 255, 255))
wave_text = font.render("波数:%d" % current_wave, True, (255, 255, 255))
# 定义游戏状态
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_ESCAPE:
running = False
elif event.key == pygame.K_SPACE:
bullet_rect = bullet_img.get_rect()
bullet_rect.left = player_rect.left + player_rect.width // 2 - bullet_rect.width // 2
bullet_rect.top = player_rect.top - bullet_rect.height
bullets.append(bullet_rect)
# 处理玩家移动
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_rect.left > 0:
player_rect.left -= player_speed
elif keys[pygame.K_RIGHT] and player_rect.right < screen_width:
player_rect.left += player_speed
elif keys[pygame.K_UP] and player_rect.top > 0:
player_rect.top -= player_speed
elif keys[pygame.K_DOWN] and player_rect.bottom < screen_height:
player_rect.top += player_speed
# 处理敌机生成和移动
if len(enemies) < max_enemies:
enemy_rect = enemy_img.get_rect()
enemy_rect.left = random.randint(0, screen_width - enemy_rect.width)
enemy_rect.top = -enemy_rect.height
enemy_speed = random.randint(1, max_enemy_speed)
enemies.append((enemy_rect, enemy_speed))
for i, (enemy_rect, enemy_speed) in enumerate(enemies):
enemy_rect.top += enemy_speed
if enemy_rect.top > screen_height:
enemies.pop(i)
# 处理子弹移动和碰撞检测
for i, bullet_rect in enumerate(bullets):
bullet_rect.top -= 10
if bullet_rect.top < 0:
bullets.pop(i)
continue
for j, (enemy_rect, enemy_speed) in enumerate(enemies):
if bullet_rect.colliderect(enemy_rect):
bullets.pop(i)
enemies.pop(j)
player_score += 10
break
# 处理关卡升级
if player_score >= current_wave * 100:
current_wave += 1
max_enemy_speed += 1
max_enemies += 2
wave_text = font.render("波数:%d" % current_wave, True, (255, 255, 255))
# 绘制游戏界面
screen.blit(background, (0, 0))
screen.blit(player_img, player_rect)
for enemy_rect, _ in enemies:
screen.blit(enemy_img, enemy_rect)
for bullet_rect in bullets:
screen.blit(bullet_img, bullet_rect)
score_text = font.render("得分:%d" % player_score, True, (255, 255, 255))
screen.blit(score_text, (10, 10))
screen.blit(wave_text, (10, 40))
pygame.display.update()
# 控制游戏帧率
clock.tick(FPS)
# 退出游戏
pygame.quit()
```
在这个代码中,我们定义了一个`player_score`变量来存储玩家得分,每次击落敌机得分加10。在游戏循环中,我们使用`current_wave`变量来表示当前关卡,每过100分就升级一次关卡,同时增加敌机的数量和速度,提高游戏难度。我们还使用了`score_text`和`wave_text`两个变量来显示得分和关卡信息。
阅读全文