用Python写射击小游戏的代码
时间: 2024-01-29 07:03:00 浏览: 149
一款简单射击类游戏代码
5星 · 资源好评率100%
好的,我可以为您提供一个简单的 Python 射击小游戏的代码。请注意,这只是一个基础的示例,您可以根据自己的需要进行修改和改进。
```python
import pygame
import random
pygame.init()
# 设置游戏窗口的大小
screen_width = 800
screen_height = 600
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
# 创建游戏窗口
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('射击小游戏')
# 加载图片
player_img = pygame.image.load('player.png').convert()
player_img.set_colorkey(white)
enemy_img = pygame.image.load('enemy.png').convert()
enemy_img.set_colorkey(white)
# 设置游戏时钟
clock = pygame.time.Clock()
# 定义玩家,敌人和子弹类
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.transform.scale(player_img, (50, 50))
self.rect = self.image.get_rect()
self.rect.x = screen_width // 2
self.rect.y = screen_height - 100
self.speed_x = 0
self.speed_y = 0
def update(self):
self.rect.x += self.speed_x
self.rect.y += self.speed_y
if self.rect.left < 0:
self.rect.left = 0
elif self.rect.right > screen_width:
self.rect.right = screen_width
elif self.rect.top < 0:
self.rect.top = 0
elif self.rect.bottom > screen_height:
self.rect.bottom = screen_height
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.transform.scale(enemy_img, (50, 50))
self.rect = self.image.get_rect()
self.rect.x = random.randrange(screen_width - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speed_x = random.randrange(-3, 3)
self.speed_y = random.randrange(1, 8)
def update(self):
self.rect.x += self.speed_x
self.rect.y += self.speed_y
if self.rect.top > screen_height + 10 or self.rect.left < -25 or self.rect.right > screen_width + 20:
self.rect.x = random.randrange(screen_width - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speed_x = random.randrange(-3, 3)
self.speed_y = random.randrange(1, 8)
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((10, 20))
self.image.fill(black)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed_y = -10
def update(self):
self.rect.y += self.speed_y
if self.rect.bottom < 0:
self.kill()
# 创建游戏角色和精灵组
player = Player()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
# 创建敌人和精灵组
enemies = pygame.sprite.Group()
for i in range(10):
enemy = Enemy()
enemies.add(enemy)
all_sprites.add(enemy)
# 创建子弹和精灵组
bullets = pygame.sprite.Group()
# 游戏循环
running = True
while running:
# 设置帧率
clock.tick(60)
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.speed_x = -5
elif event.key == pygame.K_RIGHT:
player.speed_x = 5
elif event.key == pygame.K_SPACE:
bullet = Bullet(player.rect.centerx, player.rect.top)
all_sprites.add(bullet)
bullets.add(bullet)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and player.speed_x < 0:
player.speed_x = 0
elif event.key == pygame.K_RIGHT and player.speed_x > 0:
player.speed_x = 0
# 更新游戏角色和敌人
all_sprites.update()
# 检查子弹是否击中敌人
hits = pygame.sprite.groupcollide(enemies, bullets, True, True)
for hit in hits:
enemy = Enemy()
enemies.add(enemy)
all_sprites.add(enemy)
# 检查是否有敌人撞到了玩家
hits = pygame.sprite.spritecollide(player, enemies, False)
if hits:
running = False
# 绘制游戏界面
screen.fill(white)
all_sprites.draw(screen)
pygame.display.flip()
# 关闭游戏窗口
pygame.quit()
```
希望这个代码可以帮到您!
阅读全文