pygame事件之——控制物体(飞机)的移动

栏目: Python · 发布时间: 8年前

内容简介:pygame事件之——控制物体(飞机)的移动
# -*- coding: utf-8 -*-

import pygame
from pygame.locals import *
import sys
import time

pygame.init()
screen_width = 640
screen_height = 480
# 设置游戏窗口的大小
screen = pygame.display.set_mode((screen_width,screen_height), 0, 32)
pygame.display.set_caption('飞机大战')
image = 'feiji.png'
mv_image = pygame.image.load(image)

# 获取飞机的长度
mv_image_width = mv_image.get_width()

# 获取飞机的宽度
mv_image_height = mv_image.get_height()

# 飞机的起始位置,应该在下面的正中间
x, y = screen_width/2-mv_image_width/2, screen_height-mv_image_height

# 标记1
# 飞机的移动偏移量,每个方向设置一个
move_a, move_d, move_w, move_s = 0, 0, 0, 0

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_a or event.key == K_LEFT:
                move_a = 1
            elif event.key == K_d or event.key == K_RIGHT:
                move_d = 1
            elif event.key == K_UP or event.key == K_w:
                move_w = 1
            elif event.key == K_DOWN or event.key == K_s:
                move_s = 1
        elif event.type == KEYUP:
            # 某一个方向的键盘被松开,这个方向的偏移量赋为0
            if event.key == K_a or event.key == K_LEFT:
                move_a = 0
            elif event.key == K_d or event.key == K_RIGHT:
                move_d = 0
            elif event.key == K_UP or event.key == K_w:
                move_w = 0
            elif event.key == K_DOWN or event.key == K_s:
                move_s = 0
    
    # 按下键盘时,飞机对应的移动偏移量会变成1,飞机的位置进行相应的加减操作,刷新显示来控制飞机的移动
    # 如果一直按住键盘不动,飞机对应的移动偏移量就会一直为1,飞机的位置就会随着while的不断循环而改变

    # 飞机移动后的位置
    x = x + move_d - move_a
    y = y + move_s - move_w
  # 标记2

    # 控制飞机的范围,不能超出屏幕,还要考虑到子弹应该能够发射到任意一个地方
    # 左右边界飞机应该可以进入一半
    if x > screen_width-mv_image_width/2:
        x = screen_width-mv_image_width/2
    elif x < 0-mv_image_width/2:
        x = 0-mv_image_width/2
    # 下边界的话能够看到飞机头部就行
    if y > screen_height-mv_image_height/5:
        y = screen_height-mv_image_height/5
    # 上边界不允许飞机进入
    elif y < 0:
        y = 0

    # 填充背景颜色以RGB形式
    screen.fill((255, 255, 255))
    # 在新的位置上画图
    screen.blit(mv_image, (x, y))
    pygame.display.update()
    # 减少CPU负担,测试后感觉这个数值最恰当
    time.sleep(0.001)

在这里,飞机的偏移量之所以设置四个而不是两个,是因为如果设置的是两个,即控制x和y轴,那么飞机控制的方向只能是x轴或y轴,

就比如说,当按住 a 键的时候,飞机往左移动,此时不松开a键而继续按住d键,飞机就会停止往左移动转而向右移动,这里明显是不对的,我们目标应该是同时按住a键和d键时飞机停止移动,

紧接着,松开d键,此时飞机却不动了,但我们的a键此时还按着呢,他不是应该向左边移动吗?

所以用四个偏移量控制飞机的移动,各个方向互不影响,才能让飞机顺利的移动。

下面是飞机用两个偏移量时的代码


# 标记1

move_x, move_y = 0, 0
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        if event.type == KEYDOWN:
            if event.key == K_a or event.key == K_LEFT:
                move_x = -1
            elif event.key == K_d or event.key == K_RIGHT:
                move_x = 1
            elif event.key == K_UP or event.key == K_w:
                move_y = -1
            elif event.key == K_DOWN or event.key == K_s:
                move_y = 1
        elif event.type == KEYUP:
            # 如果用户放开了键盘,图就不要动了
            move_x = 0
            move_y = 0
    # 计算出新的坐标
    x += move_x
    y += move_y
# 标记2

需要的图片feiji.png,纯手工绘,不好看莫怪

如果还想做一个更酷的效果(其实也不怎么酷啦),比如游戏开始时飞机不是直接出现在屏幕上,而是缓缓的从下方移动到开始位置。

代码如下:

# 使游戏开始时飞机缓缓到达战场
y1 = screen_height
while True:
    if y1 <= y:
        break
    screen.fill((255, 255, 255))
    screen.blit(mv_image, (x, y1))
    pygame.display.update()
    y1 -= 1
    # 每0.01秒前进一步
    time.sleep(0.01)

这段代码插入到 #标记1 的前面

pygame事件之——控制物体(飞机)的移动


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Bulletproof Web Design

Bulletproof Web Design

Dan Cederholm / New Riders Press / 28 July, 2005 / $39.99

No matter how visually appealing or packed with content a Web site is, it isn't succeeding if it's not reaching the widest possible audience. Designers who get this guide can be assured their Web site......一起来看看 《Bulletproof Web Design》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具