【问题标题】:How to move a circle again after being idle for 3 seconds?闲置3秒后如何再次移动一圈?
【发布时间】:2019-04-09 08:44:18
【问题描述】:

我目前正在创建一个程序,其中一个圆圈将向左移动,停留 3 秒,然后移动到屏幕右侧。但是,当尝试在那里实施此解决方案时:In PyGame, how to move an image every 3 seconds without using the sleep function?,它不起作用。如果有人能告诉我我在做什么,我将不胜感激。

这是我的代码:

import pygame, sys, time, random
from pygame.locals import *

currentPosition = [300, 368]

def moveLeft():
    currentPosition[0] -= 1

def moveRight():
    currentPosition[0] += 1


pygame.init()

windowCalibration = pygame.display.set_mode((0,0))

WHITE = (255, 255, 255)



windowCalibration.fill(WHITE)

pygame.display.set_caption("Eye calibration")
pygame.draw.circle(windowCalibration, (0,0,0), currentPosition, 10)
done = False

circleIsIdle = True

clock = pygame.time.Clock()

time_counter = 0

def stopCircle():
    circleIsIdle = True
    while circleIsIdle:
        time_counter = clock.tick()
        if time_counter > 3000:
            time_counter = 0
            circleIsIdle = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                done = True
    while currentPosition[0] >= 10:
        moveLeft()
        windowCalibration.fill(WHITE)
        pygame.draw.circle(windowCalibration, (0,0,0), currentPosition, 10)
        pygame.display.update()
    stopCircle()

    while currentPosition[0] <= 1350:
        moveRight()
        windowCalibration.fill(WHITE)
        pygame.draw.circle(windowCalibration, (0,0,0), currentPosition, 10)
        pygame.display.update()
    stopCircle()
    done = True        

【问题讨论】:

  • 我想你忘记了time_counter = clock.tick()中的+=
  • while not done 中,您应该只使用一次pygame.display.update() - 在循环结束时。这样它应该运行更顺畅。但问题是while circleIsIdle: 停止while not done 并且它无法检查您是否按下ESC。在游戏中你不应该使用其他需要较长时间的while
  • 感谢@Pygasm。这解决了我的问题。

标签: python pygame


【解决方案1】:

while not done 内部,您不应该使用其他while,这需要更长的时间。它会停止while not done 并且无法检查您是否按下了ESC 等。

您应该使用pygame.time.get_ticks() 来获取当前时间并使用它来控制哪个元素移动或绘制。

我还使用state 来查看我是向左还是向右移动,或者在向左或向右移动之前等待。这样我可以做不同的事情——我可以移动或不移动,我可以绘制或不绘制(即,如果我有“暂停”按钮,我可以使用state_pause 来绘制或不绘制这个按钮)。

此代码始终运行while not done,因此您始终可以使用ESC 退出。即使第一个圆圈正在等待,您也可以移动第二个圆圈。

import pygame

# --- constants ---

WHITE = (255, 255, 255)
BLACK = (  0,   0,   0)

# --- classes ---

# empty

# --- functions ---

def moveLeft():
    currentPosition[0] -= 1

def moveRight():
    currentPosition[0] += 1
# --- main ---

pygame.init()

windowCalibration = pygame.display.set_mode((0,0))
pygame.display.set_caption("Eye calibration")

currentPosition = [300, 368]
state = 'move_left'
wait_to = 0

done = False
while not done:

    # --- events ---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                done = True

    # --- moves ---

    if state == 'move_left':
        if currentPosition[0] >= 10:
            moveLeft()
        else:
            state = 'wait_before_move_right'
            wait_to = pygame.time.get_ticks() + 3000

    elif state == 'move_right':
        if currentPosition[0] <= 1350:
            moveRight()
        else:
            state = 'wait_before_move_left'
            wait_to = pygame.time.get_ticks() + 3000

    elif state == 'wait_before_move_right':
        current_time = pygame.time.get_ticks()
        if current_time > wait_to:
            state = 'move_right'

    elif state == 'wait_before_move_left':
        current_time = pygame.time.get_ticks()
        if current_time > wait_to:
            state = 'move_left'

    # --- draws ----

    windowCalibration.fill(WHITE)
    pygame.draw.circle(windowCalibration, BLACK, currentPosition, 10)
    pygame.display.update()

# --- end ---
pygame.quit()

编辑:此代码同时移动 3 个圆圈,它们在改变方向之前等待并且不会停止其他圆圈,您可以随时使用ESC

import pygame

# --- constants ---

WHITE = (255, 255, 255)
BLACK = (  0,   0,   0)
RED   = (255,   0,   0)
GREEN = (  0, 255,   0)
BLUE  = (  0,   0, 255)

# --- classes ---

# empty

# --- functions ---

# empty

# --- main ---

pygame.init()

windowCalibration = pygame.display.set_mode((0,0))
pygame.display.set_caption("Eye calibration")

circles = [
    {'pos': [300, 368], 'speed': 1, 'state': 'move_left', 'wait_to': 0, 'color': RED},
    {'pos': [300, 268], 'speed': 10, 'state': 'move_right', 'wait_to': 0, 'color': GREEN},
    {'pos': [300, 168], 'speed': 30, 'state': 'move_right', 'wait_to': 0, 'color': BLUE},
]

done = False
while not done:

    # --- events ---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                done = True

    # --- moves ---

    current_time = pygame.time.get_ticks()

    for circle in circles:
        if circle['state'] == 'move_left':
            if circle['pos'][0] >= 10:
                circle['pos'][0] -= circle['speed']
            else:
                circle['pos'][0] = 10
                circle['state'] = 'wait_before_move_right'
                circle['wait_to'] = pygame.time.get_ticks() + 3000

        elif circle['state'] == 'move_right':
            if circle['pos'][0] <= 1350:
                circle['pos'][0] += circle['speed']
            else:
                circle['pos'][0] = 1350
                circle['state'] = 'wait_before_move_left'
                circle['wait_to'] = pygame.time.get_ticks() + 3000

        elif circle['state'] == 'wait_before_move_right':
            if current_time > circle['wait_to']:
                circle['state'] = 'move_right'

        elif circle['state'] == 'wait_before_move_left':
            if current_time > circle['wait_to']:
                circle['state'] = 'move_left'

    # --- draws ----

    windowCalibration.fill(WHITE)

    for circle in circles:
        pygame.draw.circle(windowCalibration, circle['color'], circle['pos'], 10)

    pygame.display.update()

# --- end ---

pygame.quit()

【讨论】:

  • 非常感谢您提供的详细代码。我想让圆圈自动移动,但无论如何我认为你的代码会帮助我改进我的程序。