python飞机大战,要求可以发射子弹,飞机可以自由移动,玩家飞机有生命值,玩家飞机碰到敌方飞机或者被敌方飞机子弹击中则扣除生命值,生命值为0时结束游戏,并显示分数。
时间: 2024-06-08 12:07:12 浏览: 68
利用Python实现飞机大战,设置有子弹类,飞机类,敌机类,函数类,移动类,侦测类.rar
好的,这个可以使用Python的pygame模块来实现。我给你提供一下思路:
1. 导入pygame模块并初始化
```python
import pygame
pygame.init()
```
2. 设置窗口大小和游戏标题
```python
screen = pygame.display.set_mode((480, 700))
pygame.display.set_caption("飞机大战")
```
3. 加载背景图片
```python
bg = pygame.image.load("background.png").convert()
```
4. 加载玩家飞机和敌机
```python
player_plane = pygame.image.load("player.png").convert_alpha()
enemy_plane = pygame.image.load("enemy.png").convert_alpha()
```
5. 定义玩家飞机和敌机的初始位置和速度
```python
player_rect = player_plane.get_rect()
player_rect.left = 190
player_rect.top = 500
player_speed = 5
enemy_rect = enemy_plane.get_rect()
enemy_rect.left = 100
enemy_rect.top = 0
enemy_speed = 2
```
6. 定义子弹类和敌机类
```python
class Bullet(pygame.sprite.Sprite):
def __init__(self, bullet_img, init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = bullet_img
self.rect = self.image.get_rect()
self.rect.midbottom = init_pos
self.speed = 10
def update(self):
self.rect.top -= self.speed
if self.rect.top < 0:
self.kill()
class Enemy(pygame.sprite.Sprite):
def __init__(self, enemy_img, init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = enemy_img
self.rect = self.image.get_rect()
self.rect.topleft = init_pos
self.speed = enemy_speed
def update(self):
self.rect.top += self.speed
if self.rect.top > 700:
self.kill()
```
7. 定义玩家飞机和敌机的碰撞检测函数
```python
def collide_check(player_rect, enemy_list):
for enemy in enemy_list:
if pygame.sprite.collide_rect(player_rect, enemy):
return True
return False
```
8. 定义游戏主循环
```python
bullet_list = pygame.sprite.Group()
enemy_list = pygame.sprite.Group()
score = 0
player_life = 3
font = pygame.font.SysFont(None, 36)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bullet = Bullet(bullet_plane, player_rect.midtop)
bullet_list.add(bullet)
screen.blit(bg, (0, 0))
screen.blit(player_plane, player_rect)
# 移动玩家飞机
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_LEFT]:
player_rect.left -= player_speed
if key_pressed[pygame.K_RIGHT]:
player_rect.left += player_speed
if key_pressed[pygame.K_UP]:
player_rect.top -= player_speed
if key_pressed[pygame.K_DOWN]:
player_rect.top += player_speed
# 碰撞检测
if collide_check(player_rect, enemy_list):
player_life -= 1
if player_life == 0:
game_over = True
else:
player_rect.left = 190
player_rect.top = 500
# 移动敌机和发射子弹
for enemy in enemy_list:
enemy.update()
screen.blit(enemy_plane, enemy.rect)
bullet_hit_list = pygame.sprite.spritecollide(enemy, bullet_list, True)
if bullet_hit_list:
enemy_list.remove(enemy)
score += 10
for bullet in bullet_list:
bullet.update()
screen.blit(bullet_plane, bullet.rect)
# 生成敌机
if random.randint(1, 50) == 1:
enemy = Enemy(enemy_plane, [random.randint(0, 400), 0])
enemy_list.add(enemy)
# 显示分数和生命值
score_text = font.render("Score: "+str(score), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
life_text = font.render("Life: "+str(player_life), True, (255, 255, 255))
screen.blit(life_text, (400, 10))
pygame.display.update()
```
这样就可以实现一个简单的飞机大战游戏了。
阅读全文