[Python] 纯文本查看 复制代码 import pygame
# 导入游戏环境
from game import *
import sys
# 初始化
pygame.init() # pygame 初始化
screen = pygame.display.set_mode((800, 600)) # 创建屏幕
clock = pygame.time.Clock() # 生成游戏时钟
# 初始化炮台里的能量球
ball = spaceship.new_ball()
# 反应仓列表
ball_list = []
# 加载背景图片
background = pygame.image.load('image/星海背景.png')
# 添加警报器
alarm = pygame.image.load('image/警报器.png')
alarm_rect = alarm.get_rect()
alarm_rect.left = 300
alarm_rect.top = 450
# 添加反应仓及获取反应仓的矩形区域
storage = pygame.image.load('image/反应仓.png')
storage_rect = storage.get_rect()
print(storage_rect)
storage_rect.left = 12
storage_rect.top = 115
print(storage_rect)
# 添加弹出按钮及获取弹出按钮的矩形区域
push = pygame.image.load('image/弹出按钮.png')
push_rect = push.get_rect()
push_rect.left = -4
push_rect.top = 388
# 主循环
while True:
# 事件检测
for event in pygame.event.get():
# 退出事件
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 你需要在这里修改 [鼠标点击事件] 的代码
# 鼠标点击事件
if event.type == pygame.MOUSEBUTTONDOWN:
# 获取鼠标坐标
mouse_position = pygame.mouse.get_pos()
if storage_rect.collidepoint(mouse_position)and len(ball_list)<4:
ball_list.append(ball)
ball = spaceship.new_ball()
else:
# 【在这里编写代码,完成弹出反应仓的代码】
spaceship.fire_ball(ball ,mouse_position)
ball = spacechip.new_ball()
# 鼠标移动事件
if event.type == pygame.MOUSEMOTION:
# 让炮台跟着鼠标转动
spaceship.rotate_to_mouse()
# 绘制背景
screen.blit(background, (0, 0))
# 绘制飞船
spaceship.draw(screen)
# 绘制反应仓
screen.blit(storage, storage_rect)
# 绘制弹出按钮
screen.blit(push, push_rect)
# 绘制警报器
screen.blit(alarm, (300, 450))
ball_rect = get_firstball()
center = ball_rect.center
if alarm_rect.collidepoint(center):
ring_alarm()
# 检测终极武器有没有触发
spaceship.master_stroke(screen, ball_list)
# 更新屏幕
pygame.display.update()
# 设置游戏速度
clock.tick(12) |