- 帖子
- 1
- 精华
- 0
- 积分
- 4
- 阅读权限
- 10
- 注册时间
- 2016-8-23
- 最后登录
- 2016-8-23
|
import pygame
#导入pygame库
from sys import exit
#向sys模块接一个exit函数用来退出程序
pygame.init()
#初始化pygame,为使用硬件做准备
screen = pygame.display.set_mode( (450, 800), 0, 32)
#创建一个窗口,窗口大小和背景图片一样大小
pygame.display.set_caption('Hello, World')
#设置窗口标题
background = pygame.image.load('background.jpg').convert()
#加载病转换图片
plane = pygame.image.load('plane.png').convert()
#加载飞机图片
bullet = pygame.image.load('bullet.png').convert_alpha()
#加载子弹图片
bullet_x = 0
bullet_y = 0
#初始化子弹的位置
while True:
#游戏主循环
for event in pygame.event.get():
if event.type == pygame.QUIT:
#接受到退出事件就退出程序
pygame.quit()
exit()
screen.blit(background, (0, 0))
#将背景图画上去
x,y = pygame.mouse.get_pos()
#获取鼠标位置
if bullet_y < 0:
#如果子弹超出屏幕范围
bullet_x = x #- bullet.get_width() 这里这样改也可以?!!
bullet_y = y #- bullet.get_height()
#把子弹的中心位置设为鼠标坐标
else:
bullet_y -= 5
#子弹位置往上移
screen.blit(bullet, (bullet_x, bullet_y) )
x -= plane.get_width() / 2
y -= plane.get_height() / 2
#计算飞机左上角位置
screen.blit(plane, (x, y))
#把飞机画到屏幕上
pygame.display.update()
#刷新一下图片
|
|