Python射击游戏代码
时间: 2024-01-21 07:13:35 浏览: 68
一款简单射击类游戏代码
5星 · 资源好评率100%
以下是一个简单的Python射击游戏代码,其中包括玩家、敌人和子弹类:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# 设置屏幕大小
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 加载图片
player_img = pygame.image.load("player.png").convert()
enemy_img = pygame.image.load("enemy.png").convert()
missile_img = pygame.image.load("missile.png").convert()
# 定义玩家类
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = player_img
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.centerx = SCREEN_WIDTH / 2
self.rect.bottom = SCREEN_HEIGHT - 10
self.speedx = 0
def update(self):
self.speedx = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.speedx = -5
if keystate[pygame.K_RIGHT]:
self.speedx = 5
self.rect.x += self.speedx
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
if self.rect.left < 0:
self.rect.left = 0
def shoot(self):
missile = Missile(self.rect.centerx, self.rect.top)
all_sprites.add(missile)
missiles.add(missile)
# 定义敌人类
class Enemy(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = enemy_img
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(SCREEN_WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
def update(self):
self.rect.y += self.speedy
if self.rect.top > SCREEN_HEIGHT + 10:
self.rect.x = random.randrange(SCREEN_WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
# 定义子弹类
class Missile(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = missile_img
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.bottom = y
self.rect.centerx = x
self.speedy = -10
def update(self):
self.rect.y += self.speedy
if self.rect.bottom < 0:
self.kill()
# 创建精灵组
all_sprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()
missiles = pygame.sprite.Group()
# 创建玩家
player = Player()
all_sprites.add(player)
# 创建敌人
for i in range(8):
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
# 设置游戏循环
clock = pygame.time.Clock()
score = 0
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_SPACE:
player.shoot()
# 更新精灵组
all_sprites.update()
# 检测子弹是否击中敌人
hits = pygame.sprite.groupcollide(enemies, missiles, True, True)
for hit in hits:
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
score += 10
# 检测敌人是否撞到玩家
hits = pygame.sprite.spritecollide(player, enemies, False)
if hits:
running = False
# 绘制屏幕
screen.fill(WHITE)
all_sprites.draw(screen)
pygame.display.flip()
# 显示得分
font = pygame.font.Font(None, 36)
text = font.render("Score: " + str(score), True, RED)
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.centery = screen.get_rect().centery + 24
screen.blit(text, text_rect)
pygame.display.flip()
# 延迟关闭窗口
pygame.time.delay(2000)
# 退出pygame
pygame.quit()
```
阅读全文