pygame 中的平滑运动 [英] smooth movement in pygame

查看:82
本文介绍了pygame 中的平滑运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始尝试学习 pygame,在尝试在屏幕上移动矩形时遇到了一些麻烦.我已将其设置为当我按下每个箭头键时,矩形将向上、向下、向左和向右移动.但是,当我按住按键时,它不会继续移动.我必须多次按键才能移动到任何地方.

I have just started trying to learn about pygame, and I've run into some trouble when trying to move my rectangle around the screen. I have it set so that when I press each of the arrow keys, the rectangle will move up, down, left, and right. However, when I hold down the keys, it will not keep moving. I have to press the keys multiple times for it to move anywhere.

我尝试使用 pygame.key.get_pressed() 方法,如 Python - Pygame 中的平滑键盘移动,但没有任何效果.

I have tried using the pygame.key.get_pressed() method as answered in the post Python - Smooth Keyboard Movement in Pygame but nothing is working.

我注意到,每隔一段时间,如果我按住箭头键一段时间,矩形会继续移动,但只有大约一秒钟,然后停止.

I noticed that every once in a while if I hold down the arrow keys for a while, the rectangle will keep moving but only for about a second, then it stops.

这个问题以前可能有人回答过,但我一直没能找到答案.

This question has probably been answered before, but I haven't been able to come across an answer.

代码如下:

import pygame
import os
import sys

_image_library = {}
def get_image(path):
    global _image_library
    image = _image_library.get(path)
    if image == None:
            canonicalized_path = path.replace('/', os.sep).replace('\\', os.sep)
            image = pygame.image.load(canonicalized_path)
            _image_library[path] = image
    return image

def detect_collision(x,y):
    if x > 340:
       x -= 1
    if y > 240:
       y -= 1
    if y < 0:
       y += 1
    if x < 0:
       x += 1
    return x,y

pygame.init()
screen = pygame.display.set_mode((800, 550))
done = False
clock = pygame.time.Clock()

x = 30
y = 30

pygame.mixer.music.load("song.mp3")
pygame.mixer.music.play()

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

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]:
        y -= 1
        x,y = detect_collision(x, y)
    if pressed[pygame.K_DOWN]:
        y += 1
        x,y = detect_collision(x, y)
    if pressed[pygame.K_LEFT]:
        x -= 1
        x,y = detect_collision(x, y)
    if pressed[pygame.K_RIGHT]:
        x += 1
        x,y = detect_collision(x, y)

    screen.fill((255, 255, 255))

    pygame.draw.rect(screen, (0, 128, 0), pygame.Rect(x, y, 60, 60))

    pygame.display.flip()
    clock.tick(60)

推荐答案

我遇到了类似的问题,所以我没有使用 get_pressed(),而是使用 dict 并在按下某个键时更新它:

I had a similar problem, so instead of using get_pressed(), I use a dict and update it whenever a key is pressed:

pressed = {}

while True:
    for event in pygame.event.get():
        if event.type == KEYUP:
            pressed[event.key] = False
        elif event.type == KEYDOWN:
            pressed[event.key] = True

然后测试是否按下了一个键(例如向上箭头),只需使用

Then to test if a key (e.g. the up arrow) was pressed just use

if pressed.get(K_UP):
    # Do something

在主事件循环内(即 while True).

inside the main event loop (i.e. the while True).

这篇关于pygame 中的平滑运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆