设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【Pygame 第5课】 游戏中的运动

[复制链接]

0

主题

0

好友

4

积分

新手上路

Rank: 1

楼主
发表于 2016-8-23 16:09:39 |显示全部楼层
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()
    #刷新一下图片
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-4 10:19 , Processed in 0.026082 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部