用pygame做一个战争游戏
时间: 2024-05-08 08:09:40 浏览: 84
好的,下面是一个简单的战争游戏的代码示例,使用了Pygame库:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 设置游戏屏幕大小和标题
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("战争游戏")
# 设置游戏时钟
clock = pygame.time.Clock()
# 加载游戏资源
background_image = pygame.image.load("background.png")
player_image = pygame.image.load("player.png")
enemy_image = pygame.image.load("enemy.png")
bullet_image = pygame.image.load("bullet.png")
# 定义游戏对象类
class GameObject:
def __init__(self, x, y, image):
self.x = x
self.y = y
self.image = image
self.width = image.get_width()
self.height = image.get_height()
def draw(self, screen):
screen.blit(self.image, (self.x, self.y))
def collide(self, other):
return (self.x < other.x + other.width and
self.x + self.width > other.x and
self.y < other.y + other.height and
self.y + self.height > other.y)
# 定义玩家类
class Player(GameObject):
def __init__(self, x, y, image):
super().__init__(x, y, image)
self.speed = 5
self.bullets = []
def shoot(self):
bullet = Bullet(self.x + self.width/2 - bullet_image.get_width()/2, self.y, bullet_image)
self.bullets.append(bullet)
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.x -= self.speed
elif keys[pygame.K_RIGHT]:
self.x += self.speed
if keys[pygame.K_SPACE]:
self.shoot()
for bullet in self.bullets:
bullet.update()
if bullet.y < -bullet.height:
self.bullets.remove(bullet)
def collide(self, other):
for bullet in self.bullets:
if bullet.collide(other):
self.bullets.remove(bullet)
return True
return super().collide(other)
# 定义敌人类
class Enemy(GameObject):
def __init__(self, x, y, image):
super().__init__(x, y, image)
self.speed = 3
def update(self):
self.y += self.speed
# 定义子弹类
class Bullet(GameObject):
def __init__(self, x, y, image):
super().__init__(x, y, image)
self.speed = 10
def update(self):
self.y -= self.speed
def collide(self, other):
return super().collide(other)
# 创建游戏对象
player = Player(screen_width/2 - player_image.get_width()/2, screen_height - player_image.get_height() - 20, player_image)
enemies = []
bullets = []
for i in range(10):
enemy = Enemy(random.randint(0, screen_width - enemy_image.get_width()), -random.randint(0, screen_height), enemy_image)
enemies.append(enemy)
# 游戏循环
running = True
while running:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 更新游戏对象
player.update()
for enemy in enemies:
enemy.update()
if enemy.y > screen_height:
enemies.remove(enemy)
for bullet in player.bullets:
for enemy in enemies:
if bullet.collide(enemy):
enemies.remove(enemy)
if bullet.y < -bullet.height:
player.bullets.remove(bullet)
# 检测碰撞
for enemy in enemies:
if player.collide(enemy):
running = False
# 绘制游戏界面
screen.blit(background_image, (0, 0))
player.draw(screen)
for enemy in enemies:
enemy.draw(screen)
for bullet in player.bullets:
bullet.draw(screen)
pygame.display.update()
# 控制游戏帧率
clock.tick(60)
# 退出游戏
pygame.quit()
```
请注意,这只是一个简单的战争游戏示例,您可以根据自己的需求和想法进行修改和扩展。同时,您还需要自己准备游戏资源,如背景、玩家、敌人和子弹的图片等。
阅读全文