只有按住按钮并且鼠标移动时,字符才会连续移动

时间:2014-07-13 12:08:31

标签: python pygame mouse

我试图让代码接受用户持有密钥并不断更新。但是,它只进行一次更新,然后执行其余代码,忽略用户的输入。

import pygame, sys
from pygame.locals import *
import math

pygame.init()
black = (0,0,0)
Window = pygame.display.set_mode((800,500),0,32)
pygame.display.set_caption('Game')


FPS = 60
fpsClock = pygame.time.Clock()

background = pygame.Rect(0,0,800,500)
player = pygame.Rect(725,425, 25,25)
playerx = player.x
playery = player.y
jumping = False
onground = True
while True:
    onground = True
    jumping = False
    Window.fill(black)
    pygame.draw.rect(Window, pygame.color.Color('Royal Blue'), player, 0)
    for e in pygame.event.get():
        if e.type == QUIT:
            pygame.quit()
            sys.exit()
        keys = pygame.key.get_pressed()
        if keys[K_w]:
            jumping = True
            onground = False
        elif keys[K_a]:
            player.move_ip(-1,0)
        elif keys[K_d]:
            player.move_ip(2,0)

    if jumping == True and onground == False:
        player.move_ip(0,-1)
        if keys[K_a]:
            player.move_ip(-2,-1)
        elif keys[K_d]:
            player.move_ip(2,-1)
    elif (jumping == False and onground == False) or playery > 412:
        player.move_ip(0,1)
        jumping = False

    print player.copy()

    player.clamp_ip(background)
    pygame.display.flip()
fpsClock.tick(FPS)

1 个答案:

答案 0 :(得分:0)

你有

    keys = pygame.key.get_pressed()
    if keys[K_w]:
        jumping = True
        onground = False
    elif keys[K_a]:
        player.move_ip(-1,0)
    elif keys[K_d]:
        player.move_ip(2,0)

里面

for e in pygame.event.get():

所以只有在按下鼠标移动或按键等事件时才会执行。

键盘事件仅在您按/按键时创建 - 而不是在您按下时创建 因此,只有在移动鼠标或按键时,此for循环才能工作并执行任何内容。

我的代码版本:

import pygame, sys
from pygame.locals import *

#import math

# CONSTANTS - uppercase

BLACK = (0,0,0)
FPS = 60
BLUE = pygame.color.Color('Royal Blue')

# GAME

pygame.init()
# window - variables - always lowercase
screen = pygame.display.set_mode((800,500),0,32)
pygame.display.set_caption('Game')

background = pygame.Rect(0,0,800,500)
player = pygame.Rect(725,425, 25,25)
playerx = player.x
playery = player.y

# mainloop

fps_clock = pygame.time.Clock()

jumping = False
onground = True

gaming = True
paused = False

while gaming:
    onground = True
    jumping = False

    # --- (all) events ---

    for e in pygame.event.get():
        if e.type == QUIT:
            gaming = False
        elif e.type == KEYDOWN:
            if e.key == K_ESCAPE:
                gaming = False
            elif e.key == K_SPACE:
                paused = not paused

    # --- (events and) moves ---

    if not paused:
        # not in `for e in event`
        keys = pygame.key.get_pressed()

        if keys[K_w]:
            jumping = True
            onground = False
        elif keys[K_a]:
            player.move_ip(-1,0)
        elif keys[K_d]:
            player.move_ip(2,0)

        if jumping == True and onground == False:
            player.move_ip(0,-1)
            if keys[K_a]:
                player.move_ip(-2,-1)
            elif keys[K_d]:
                player.move_ip(2,-1)
        elif (jumping == False and onground == False) or playery > 412:
            player.move_ip(0,1)
            jumping = False

    print player #.copy() # why copy ???

    #player.clamp_ip(background) # I don't need it

    # --- (all) draws ---

    screen.fill(BLACK)
    pygame.draw.rect(screen, BLUE, player, 0)
    pygame.display.flip()

    # --- FPS ---

    fps_clock.tick(FPS)

# the end

pygame.quit()
sys.exit()