所谓人物的移动,其实就是让我们原绘制的图片不再显示,再要移动的地方绘制一个新的图片。这里我们用这只小猫的图片来代替人物角色。再选取人物角色图片时,建议大家采取正方形大小的尺寸,方便后面的坐标计算,背景最好是纯黑或者纯白,方便背景透明化处理。

python上下左右键控制 pygame的上下左右_开发语言

先把程序主干写出来

import time,sys
import pygame
from pygame.locals import *
import math
pygame.init()#游戏初始化
screen = pygame.display.set_mode(size=(1400,800))#设置屏幕大小
pygame.display.set_caption('小游戏')#标题名
cat = pygame.image.load('cat.png')#加载小猫图片
cat_ = pygame.transform.scale(cat,(100,100)).convert()#这步是把小猫图片大小转化了,并让它可以自适应这个大小。这里是弄成了100*100大小的正方形
cat_x = 550#小猫的横坐标
cat_y = 350#小猫的竖坐标
WHITE = [255, 255, 255]#表示白色后续使用
BLACK = [0,0,0]#表示黑色
while True :#主循环
    for event in pygame.event.get() :#键盘事件
        if event.type == QUIT :
            pygame.quit()
            sys.exit()

 我们先创建一个白色的画布,把我们的小猫在上面绘制出来。需要在主循环中增加 :

while True :
    screen.fill(WHITE)#把我们创建的屏幕全涂成白色的
    screen.blit(cat_,(cat_x,cat_y))#在此坐标出绘制我们的小猫,加载的小猫是已经转化过格式的
    for event in pygame.event.get() :
        if event.type == QUIT :
            pygame.quit()
            sys.exit()
    pygame.display.update()#这里一定要更新屏幕,不更新肯定没显示

此时我们运行下程序,可以看到我们画出的小猫:

python上下左右键控制 pygame的上下左右_python_02

 这里我们很快发现了问题,小猫周围背景是黑的,而画布背景是白的,这样看着很别扭,我们想要的知识图片中里面红色的小猫。此时我们可以用set_colorkey函数来把这个黑色背景透明化。

WHITE = [255, 255, 255]#表示白色后续使用
BLACK = [0,0,0]#表示黑色
cat_.set_colorkey((BLACK))#把小猫的黑色背景透明化

我们再看下效果:

python上下左右键控制 pygame的上下左右_pygame_03

 效果很好,小猫周围的黑色背景都没了。

接下来我们编写让小猫移动的部分,在这之前,我们想先再之前建立的白色画布上加上横线竖线以方便我们对其坐标的观察

for event in pygame.event.get() :#键盘事件
        if event.type == QUIT :
            pygame.quit()
            sys.exit()
    for i in range(0,1401,100) :
        pygame.draw.line(screen,BLACK,(i,0),(i,800),3)#画线方法
    for i in range(0,801,100) :
        pygame.draw.line(screen,BLACK,(0,i),(1400,i),3)

 pygame.draw.line(screen,BLACK,(i,0),(i,800),3)#画线方法,参数一表示花在画布上,第二个参数表示线条颜色,两个坐标分别为线段的起始以及终止坐标,最后的数字表示的线条粗细

补充说明:

pygame.draw.lines

绘制多个连续的直线段

pygame.draw.aaline

绘制一条消除锯齿的直线

pygame.draw.aalines

绘制多个连续的直抗锯齿线段

 接下来我们运行下程序,看看画完线条后的效果

python上下左右键控制 pygame的上下左右_python上下左右键控制_04

 可以看到线条都出来了,我们的猫也正好坐落在焦点上(如果不是,自己可以修改下前面的小猫坐标)

我们前面画的线条间隔是100,所以我们让我们的小猫每次也移动100的距离,这样小猫每次移动完正好坐落在焦点上。

if event.type == QUIT :
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                cat_x -= 100
            if event.key == pygame.K_RIGHT:
                cat_x += 100
            if event.key == pygame.K_UP:
                cat_y -= 100
            if event.key == pygame.K_DOWN:
                cat_y += 100

 运行程序后我们发现,小猫可以通过按上下左右键移动了。

但是,如果我们一直按左或者按右什么的想没想过小猫会跑出画布去不再显示。所以我们要做一个越界处理,

if cat_x>1400 :
        cat_x = 1350
    if cat_x<0 :
        cat_x = -50
    if cat_y<0 :
        cat_y = -50
    if cat_y>800 :
        cat_y = 750

 这样不管我们怎样移动,小猫都会在屏幕中,不会消失了。

不过我们又发现了问题,小猫在向右移动时,它的脸依然是朝左的,这不科学,我们要改成向右走脸是朝右的。这就需要翻转图片

cat_180 = pygame.transform.flip(cat_,True,False)#脸朝右
cat_90 = pygame.transform.rotate(cat_,-90)#脸朝上
cat_270 = pygame.transform.rotate(cat_,90)#脸朝下

pygame.transform.flip()中第一个参数为需要翻转的图像,第二个为是否左右镜像翻转,第三个为是否上下翻转。

他们的效果图为:

python上下左右键控制 pygame的上下左右_青少年编程_05

python上下左右键控制 pygame的上下左右_python上下左右键控制_06

  

python上下左右键控制 pygame的上下左右_python_07

 这下我们的效果图都准备好了

接下来我们把他们应用到我们的程序当中即可

整个程序代码如下:

import time,sys
import pygame
from pygame.locals import *
import math
pygame.init()#游戏初始化
screen = pygame.display.set_mode(size=(1400,800))#设置屏幕大小
pygame.display.set_caption('小游戏')#标题名
cat = pygame.image.load('cat.png')
cat_ = pygame.transform.scale(cat,(100,100)).convert()
WHITE = [255, 255, 255]#表示白色后续使用
BLACK = [0,0,0]#表示黑色
cat_.set_colorkey((BLACK))
cat_180 = pygame.transform.flip(cat_,True,False)
cat_90 = pygame.transform.rotate(cat_,-90)
cat_270 = pygame.transform.rotate(cat_,90)
cat_x = 550
cat_y = 350
flag = 0
while True :#主循环
    screen.fill(WHITE)#把我们创建的屏幕全涂成白色的
    if flag == 0:
        screen.blit(cat_, (cat_x, cat_y))
    if flag-1 == 0 :
        screen.blit(cat_, (cat_x, cat_y))
    if flag-2 == 0 :
        screen.blit(cat_180,(cat_x, cat_y))
    if flag-3 == 0 :
        screen.blit(cat_90,(cat_x, cat_y))
    if flag-4 == 0 :
        screen.blit(cat_270, (cat_x, cat_y))
    for event in pygame.event.get() :#键盘事件
        if event.type == QUIT :
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                cat_x -= 100
                flag = 1
            if event.key == pygame.K_RIGHT:
                cat_x += 100
                flag = 2
            if event.key == pygame.K_UP:
                cat_y -= 100
                flag = 3
            if event.key == pygame.K_DOWN:
                cat_y += 100
                flag = 4
    for i in range(0,1401,100) :
        pygame.draw.line(screen,BLACK,(i,0),(i,800),3)
    for i in range(0,801,100) :
        pygame.draw.line(screen,BLACK,(0,i),(1400,i),3)
    if cat_x > 1400:
        cat_x = 1350
    if cat_x < 0:
        cat_x = -50
    if cat_y < 0:
        cat_y = -50
    if cat_y > 800:
        cat_y = 750
    pygame.display.update()

 感谢各位小伙伴的支持与观看。