pygame 探索_斜着运动的方法

from pygame.locals import *
import pygame
from sys import exit

bg_img = './sources/background.png'
obj = './sources/bullet.png'




pygame.init()

screen = pygame.display.set_mode((480,852),0,32)
background = pygame.image.load(bg_img).convert()
one_obj = pygame.image.load(obj).convert_alpha()

pygame.key.set_repeat(3, 50)

x = 0
y = 0

def move_right_down():
    global x 
    global y
    x += 4
    y += 4
def move_right_up():
    global x 
    global y
    x +=4
    y -=4
def move_down():
    global y        
    y +=4
def move_up():
    global y   
    y -=4
def move_left():
    global x
    x -=4
def move_right():
    global x 
    x +=4
def move_left_down():
    global x
    global y
    x -=4
    y +=4
def move_left_up():
    global x
    global y
    x +=4
    y -=4




while True:
    keys = pygame.key.get_pressed()
    screen.blit(background,(0,0))
    screen.blit(one_obj,(x,y))

    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        elif keys[pygame.K_RIGHT]:
            move_right()
        elif keys[pygame.K_UP]:
            move_up()
        elif keys[pygame.K_DOWN]:
            move_down()
        elif keys[pygame.K_LEFT]:
            move_left()
        elif keys[pygame.K_RIGHT] and keys[pygame.K_DOWN]:
            move_right_down()
        elif keys[pygame.K_RIGHT] and keys[pygame.K_UP]:
            move_right_up()
        elif keys[pygame.K_LEFT] and keys[pygame.K_UP]:
            move_left_up()
        elif keys[pygame.K_LEFT] and keys[pygame.K_DOWN]:
            move_left_down()

        
    
    pygame.display.update()


'''
这样写,假如我只是斜着运动,还能用,但是我加入了单个方向的时间,就不行了。我很难同时按下两个方向按键,造成不能识别到。


'''

这是第一次尝试,失败了。


第二次:

import pygame
from pygame.locals import *
from sys import exit

bg_img = './sources/background.png'
obj = './sources/bullet.png'



'''

    keys = pygame.key.get_pressed()
    print(keys)
    keys是一个元组,布尔值。运行后同时按下键盘多个按键,发现确实有多个1说明,Pygame是可以同时获取多个按键的。

'''
pygame.init()

screen = pygame.display.set_mode((480,852),0,32)
background = pygame.image.load(bg_img).convert()
one_obj = pygame.image.load(obj).convert_alpha()

pygame.key.set_repeat(3, 430)
'''
    哈哈。利用pygame.key.get_pressed()返回的元组,而不是循环判断键盘事件。这样就可以解决运动问题了。
    不过我一直有个想法就是运动函数根本不用判断,不知道有什么办法可以根据参数来决定移动,而不是这种写很多个if ...elif...
    根据按键的不同,传递的参数不同。
    
'''
def move_all(keys_code):
    # print(keys_code[pygame.K_DOWN])
    if keys_code[pygame.K_RIGHT] and keys_code[pygame.K_DOWN]:
        print('right and down')
    elif keys_code[pygame.K_LEFT] and keys_code[pygame.K_DOWN]:
        print('left and down')
    elif keys_code[pygame.K_LEFT] and keys_code[pygame.K_UP]:
        print('left and up')
    elif keys_code[pygame.K_RIGHT] and keys_code[pygame.K_UP]:
        print('right and up')
    elif keys_code[pygame.K_RIGHT]:
        print('right')
    elif keys_code[pygame.K_LEFT]:
        print('left')
    elif keys_code[pygame.K_UP]:
        print('up')
    elif keys_code[pygame.K_DOWN]:
        print('down')

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()


    keys = pygame.key.get_pressed()
    move_all(keys)
    screen.blit(background,(0,0))
    screen.blit(one_obj,(1,1))


    pygame.display.update()

成功能同时检测到多个按键。下一个文件我决定写出来。写到这里就算成功了。

最后版本:

import pygame
from pygame.locals import *
from sys import exit

bg_img = './sources/background.png'
obj = './sources/bullet.png'
'''
    大功告成,,   这样写,就不需要pygame.key.set_repeat()来让pygame帮我们持续按按键了。他每次循环都检测一遍键盘的按键状况。所以,不需要我们来设置这个
嘿嘿。就是按键速度。不知道怎么调节。

'''



pygame.init()

screen = pygame.display.set_mode((480,852),0,32)
background = pygame.image.load(bg_img).convert()
one_obj = pygame.image.load(obj).convert_alpha()

# pygame.key.set_repeat(3, 430)

x = 0
y = 0

def move_all(keys_code):
    global x,y
    # print(keys_code[pygame.K_DOWN])
    if keys_code[pygame.K_RIGHT] and keys_code[pygame.K_DOWN]:
        print('right and down')
        x +=1
        y +=1
    elif keys_code[pygame.K_LEFT] and keys_code[pygame.K_DOWN]:
        print('left and down')
        x -=1
        y +=1
    elif keys_code[pygame.K_LEFT] and keys_code[pygame.K_UP]:
        print('left and up')
        x -=1
        y -=1
    elif keys_code[pygame.K_RIGHT] and keys_code[pygame.K_UP]:
        print('right and up')
        x +=1
        y -=1
    elif keys_code[pygame.K_RIGHT]:
        print('right')
        x +=1
    elif keys_code[pygame.K_LEFT]:
        print('left')
        x -=1
    elif keys_code[pygame.K_UP]:
        print('up')
        y -=1
    elif keys_code[pygame.K_DOWN]:
        print('down')
        y +=1

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()


    keys = pygame.key.get_pressed()
    move_all(keys)
    screen.blit(background,(0,0))
    screen.blit(one_obj,(x,y))


    pygame.display.update()

有个问题就是除了调整数字,我不知道别的办法改变移动速度。这样写的话,运动速度就直接和while循环次数有关了。。循环越快,检测越快,跑的越快,这样写,相当于每次循环keys都在改变。而不是pygame给我们的event的键盘时间。

    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        elif event.type == KEYDOWN:   #注意大小写K
            if event.key == K_a or event.key == K_LEFT:
                hero_one.move_left()
            elif event.key == K_d or event.key == K_RIGHT:
                hero_one.move_right()
            elif event.key == K_UP:
                hero_one.move_up()
            elif event.key == K_DOWN:
                hero_one.move_down()
            elif event.key == K_SPACE:
                hero_one.fire()

我看的教程是这样写, 他这样,每次循环都只能判断一个按键。

你可能感兴趣的:(pygame探索)