Pygame做个游戏(2)人物及运动操作(包括长按)

上期我们已经完成了对背景的部署,这期完成人物的部署和移动。

(1)P个图,做一张人物png格式的图片,背景设置成透明的,大小100*100像素(大小自己定啊),切记不要一下就把人物设计好,先拿个火柴人代替一下。后期再换。没时间弄的可以借你这个图用一下(别嫌弃)

(2) 先把它加载到程序中,再渲染,刷新屏幕(基本图片照这个流程来)

renwuimage = image.load("火柴人.png")                    # 人物图片
window.blit(renwuimage, (20, 20))     # 人物图片渲染到坐标(20,20)
display.update()                                          # 刷新屏幕

目前的全部代码 

import pygame
from pygame import *

pygame.init()# 初始化
window = display.set_mode((1000, 600))# 创建画布,大小为1000*600
pygame.display.set_caption("战火")

window.fill((255, 255, 255))
display.flip()

backgroundimage = image.load("background.jpg")
window.blit(backgroundimage, (0, 0))
display.update()

renwuimage = image.load("火柴人.png")                    # 人物图片
window.blit(renwuimage, (20, 20))     # 人物图片渲染
display.update()                                          # 刷新屏幕
# 最小游戏系统
while True:
    for thing in pygame.event.get():    # 事件检测语句
        if thing.type == QUIT:            # 检测到点击退出按钮,关闭游戏
            exit()

(3)接下来实现运动 

人物属性规定

首先规定人物的位置属性,写在window.blit(renwuimage, (20,20))前

renwuimagex = 20                                        # 水平位置
renwuimagey = 20                                        # 垂直位置

将这一句改写

 window.blit(renwuimage, (20, 20))     # 人物图片渲染

改写为

window.blit(renwuimage, (renwuimagex, renwuimagey))     # 人物图片渲染

 操作识别(写在游戏系统内)

        if thing.type == KEYDOWN:             # 按键按下
            print("按键被按下", thing.key, chr(thing.key))           # 反馈按键值
        if thing.type == KEYUP:
            print("按键松开", thing.key, chr(thing.key))             # 反馈按键松开值

然后你键盘随便按字母键

输出栏会显示按下的是哪个键和松开的是哪个键

对应按键事件的设置

规定人物运动属性,写在循环前,为1时就会一直运动,为0就停止运动,这样就能响应长按

renwuimageys = 0    #人物y坐标向上移动,设初值0表示不进行
renwuimageyx = 0    #人物y坐标向下移动,设初值0表示不进行
renwuimagexz = 0    #人物x坐标向左移动,设初值0表示不进行
renwuimagexy = 0    #人物x坐标向右移动,设初值0表示不进行

按下判断中写入

            if chr(thing.key) == 'a':         # 检测按下a键
                renwuimagexz = 1

            if chr(thing.key) == 'd':         # 检测按下d键
                renwuimagexy = 1

            if chr(thing.key) == 'w':         # 检测按下w键
                renwuimageys = 1

            if chr(thing.key) == 's':         # 检测按下s键
                renwuimageyx = 1

松开判断中写入,区别就是运动属性设置的值为0

            if chr(thing.key) == 'a':         # 检测松开a键
                renwuimagexz = 0
            if chr(thing.key) == 'd':         # 检测松开d键
                renwuimagexy = 0
            if chr(thing.key) == 'w':         # 检测松开w键
                renwuimageys = 0
            if chr(thing.key) == 's':         # 检测松开s键
                renwuimageyx = 0

写完后的全部代码,有点长了,会包装成函数的同学可以选择包装起来,简洁一点

import pygame
from pygame import *

pygame.init()# 初始化
window = display.set_mode((1000, 600))# 创建画布,大小为1000*600
pygame.display.set_caption("战火")

window.fill((255, 255, 255))
display.flip()

backgroundimage = image.load("background.jpg")
window.blit(backgroundimage, (0, 0))
display.update()

renwuimagex = 30                                        # 水平位置
renwuimagey = 30                                        # 垂直位置
renwuimage = image.load("火柴人.png")                    # 人物图片
window.blit(renwuimage, (renwuimagex, renwuimagey))     # 人物图片渲染
display.update()                                          # 刷新屏幕
# 最小游戏系统
while True:
    for thing in pygame.event.get():    # 事件检测语句
        if thing.type == QUIT:            # 检测到点击退出按钮,关闭游戏
            exit()
        if thing.type == KEYDOWN:             # 按键按下
            print("按键被按下", thing.key, chr(thing.key))           # 反馈按键值
            if chr(thing.key) == 'a':         # 检测按下a键
                renwuimagexz = 1

            if chr(thing.key) == 'd':         # 检测按下d键
                renwuimagexy = 1

            if chr(thing.key) == 'w':         # 检测按下w键
                renwuimageys = 1

            if chr(thing.key) == 's':         # 检测按下s键
                renwuimageyx = 1
        if thing.type == KEYUP:
            print("按键松开", thing.key, chr(thing.key))             # 反馈按键松开值
            if chr(thing.key) == 'a':         # 检测松开a键
                renwuimagexz = 0
            if chr(thing.key) == 'd':         # 检测松开d键
                renwuimagexy = 0
            if chr(thing.key) == 'w':         # 检测松开w键
                renwuimageys = 0
            if chr(thing.key) == 's':         # 检测松开s键
                renwuimageyx = 0

运动

运动状态规定完成,接下来就是运动的实现了。

在适当位置规定运动速度speed

speed = 20

运动实际是坐标在动,把坐标进行修改,记得一一对应,向上实际是y坐标减小,向下是y坐标变大 

        if renwuimagexz == 1:
            renwuimagex -= speed
        if renwuimagexy == 1:
            renwuimagex += speed
        if renwuimageys == 1:
            renwuimagey -= speed
        if renwuimageyx == 1:
            renwuimagey += speed

        window.blit(renwuimage, (renwuimagex, renwuimagey))        #渲染
        display.update()                                        #刷新

运行,嘿嘿,发现啥了没有。前一张人物图片还留在(类似残影),这就要讲到覆盖的问题了,游戏是一帧一帧的,所以我们后一帧要把前一帧的痕迹进行覆盖。你觉得最好的覆盖用啥?

 当然是背景

所以把上面的代码中添加一句

window.blit(backgroundimage, (0, 0))

目前所有的代码

import pygame
from pygame import *

pygame.init()# 初始化
window = display.set_mode((1000, 600))# 创建画布,大小为1000*600
pygame.display.set_caption("战火")

window.fill((255, 255, 255))
display.flip()

backgroundimage = image.load("background.jpg")
window.blit(backgroundimage, (0, 0))
display.update()

renwuimagex = 30                                        # 水平位置
renwuimagey = 30                                        # 垂直位置
renwuimage = image.load("火柴人.png")                    # 人物图片
window.blit(renwuimage, (renwuimagex, renwuimagey))     # 人物图片渲染
display.update()

renwuimageys = 0
renwuimageyx = 0
renwuimagexz = 0
renwuimagexy = 0
# 刷新屏幕
# 最小游戏系统
while True:
    speed = 20
    for thing in pygame.event.get():    # 事件检测语句
        if thing.type == QUIT:            # 检测到点击退出按钮,关闭游戏
            exit()
        if thing.type == KEYDOWN:             # 按键按下
            print("按键被按下", thing.key, chr(thing.key))           # 反馈按键值
            if chr(thing.key) == 'a':         # 检测按下a键
                renwuimagexz = 1

            if chr(thing.key) == 'd':         # 检测按下d键
                renwuimagexy = 1

            if chr(thing.key) == 'w':         # 检测按下w键
                renwuimageys = 1

            if chr(thing.key) == 's':         # 检测按下s键
                renwuimageyx = 1
        if thing.type == KEYUP:
            print("按键松开", thing.key, chr(thing.key))             # 反馈按键松开值
            if chr(thing.key) == 'a':         # 检测松开a键
                renwuimagexz = 0
            if chr(thing.key) == 'd':         # 检测松开d键
                renwuimagexy = 0
            if chr(thing.key) == 'w':         # 检测松开w键
                renwuimageys = 0
            if chr(thing.key) == 's':         # 检测松开s键
                renwuimageyx = 0

        if renwuimagexz == 1:
            renwuimagex -= speed
        if renwuimagexy == 1:
            renwuimagex += speed
        if renwuimageys == 1:
            renwuimagey -= speed
        if renwuimageyx == 1:
            renwuimagey += speed
        window.blit(backgroundimage, (0, 0))
        window.blit(renwuimage, (renwuimagex, renwuimagey))
        display.update()

运动实现了,但你会发现它会飞出舞台,下期我们将会对它的运动进行限制。

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

予我心安A3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值