gpt4 book ai didi

python - 闲置3秒后如何再次移动一圈?

转载 作者:行者123 更新时间:2023-12-01 08:04:51 26 4
gpt4 key购买 nike

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

最佳答案

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

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

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

此代码始终运行未完成,因此您始终可以使用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()

关于python - 闲置3秒后如何再次移动一圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55588557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com