闲置3秒后如何再次移动圆圈?

时间:2019-04-09 08:44:19

标签: python pygame

我当前正在创建一个程序,其中一个圆圈将向左移动,停留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        

1 个答案:

答案 0 :(得分:1)

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

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

我还使用state来查看我是向左还是向右移动,还是等到向左或向右移动。这样,我可以做不同的事情-我可以移动或不移动,我可以绘制或不绘制(即,如果我有按钮“ Pause”,我可以使用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()