等待没有冻结一切

时间:2015-03-27 17:04:49

标签: python pygame

我正在努力争取胜利声明。我希望屏幕快速闪烁蓝色和黄色,但是当满足胜利要求时不要太快,现在左键点击。问题是,如果不根据运行程序的计算机进行游戏更改,我找不到实现此目的的方法。我尝试让它重新运行几次颜色,以便颜色在更改之前持续更长时间,但在更快或更慢的系统上,这种效果最有可能是不同的。

if V == 2:
    if Blue == 1:
        D.fill(BLUE)
        Blue = 2
    elif Blue == 2:
        D.fill(BLUE)
        Blue = 3
    elif Blue == 3:
        D.fill(BLUE)
        Blue = 4
    elif Blue == 4:
        D.fill(BLUE)
        Blue = 5
    elif Blue == 5:
        D.fill(YELLOW)
        Blue = 6
    elif Blue == 6:
        D.fill(YELLOW)
        Blue = 7
    elif Blue == 7:
        D.fill(YELLOW)
        Blue = 8
    elif Blue == 8:
        D.fill(YELLOW)
        Blue = 1

这只是在改变之前重复四次颜色,这是不理想的。我曾尝试使用等待功能,但这会在此期间冻结整个程序,这使得所有其他事情显然都显得无法响应。有没有办法让它闪现没有这个重述的想法,并没有冻结整个程序多长时间?感谢。

在使用你们给出的答案之一后,我已经设法让它起作用了。问题是,现在,只要我使用任何形式的输入(例如箭头键),闪烁就会停止。理想情况下,闪光不会停止,直到V = 0,这将是游戏重置时。

if V == 2:
    if event.type == event_500ms:
        if Blue == 1:
            D.fill(BLUE)
            Blue = 2
        elif Blue == 2:
            D.fill(YELLOW)
            Blue = 1

这是之前显示的代码的新版本。如果重要,这就是整个循环。

    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()

    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            x_speed =- 3
            x2_speed = 3
            y3_speed =- 3
            y4_speed = 3
        elif event.key == pygame.K_RIGHT:
            x_speed = 3
            x2_speed =- 3
            y3_speed = 3
            y4_speed =- 3
        elif event.key == pygame.K_UP:
            y_speed =- 3
            y2_speed = 3
            x3_speed =- 3
            x4_speed = 3
        elif event.key == pygame.K_DOWN:
            y_speed = 3
            y2_speed =- 3
            x3_speed = 3
            x4_speed =- 3
        elif event.key == pygame.K_TAB:
            GMT += 1
            CIP = 2
            if GMT % 2:
                GM = 2
            else:
                GM = 1

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT:
            x_speed=0
            x2_speed=0
            y3_speed=0
            y4_speed=0
        elif event.key == pygame.K_RIGHT:
            x_speed=0
            x2_speed=0
            y3_speed=0
            y4_speed=0
        elif event.key == pygame.K_UP:
            y_speed=0
            y2_speed=0
            x3_speed=0
            x4_speed=0
        elif event.key == pygame.K_DOWN:
            y_speed=0
            y2_speed=0
            x3_speed=0
            x4_speed=0

    elif event.type == MOUSEBUTTONDOWN:
        x_coord = 300
        y_coord = 300
        x2_coord = 300
        y2_coord = 300
        x3_coord = 300
        y3_coord = 300
        x4_coord = 300
        y4_coord = 300
        X5_coord = 30
        Y5_coord = 10
        direction = 'right'
        D.fill(BLACK)
        G = random.randrange(1, 10)
        CO = 2
        V = 2

    elif event.type == MOUSEBUTTONUP:
        X5_coord = 30
        Y5_coord = 10
        direction = 'right'

    if direction == 'right':
        X5_coord += 10
        if X5_coord == 670:
            direction = 'down'
    elif direction == 'down':
        Y5_coord += 10
        if Y5_coord == 460:
            direction = 'left'
    elif direction == 'left':
        X5_coord -= 10
        if X5_coord == 10:
            direction = 'up'
    elif direction == 'up':
        Y5_coord -= 10
        if Y5_coord == 10:
            direction = 'right'





x_coord = x_coord + x_speed
y_coord = y_coord + y_speed

x2_coord = x2_coord + x2_speed
y2_coord = y2_coord + y2_speed

x3_coord = x3_coord + x3_speed
y3_coord = y3_coord + y3_speed

x4_coord = x4_coord + x4_speed
y4_coord = y4_coord + y4_speed

if GM == 2 and CIP == 2:
    D.fill(BLACK)
    CIP = 1

if GM == 1:
    D.fill(MEXON)

if G >= 6:
    B(L6, (80, 150))

if G == 1 and CO == 2:
    x_coord = 366
    y_coord = 201

    x2_coord = 234
    y2_coord = 399

    x3_coord = 201
    y3_coord = 366

    x4_coord = 399
    y4_coord = 234

    CO = 1

elif G == 2 and CO == 2:
    x_coord = 240
    y_coord = 312

    x2_coord = 360
    y2_coord = 288

    x3_coord = 312
    y3_coord = 240

    x4_coord = 288
    y4_coord = 360

    CO = 1

elif G == 3 and CO == 2:
    x_coord = 405
    y_coord = 228

    x2_coord = 195
    y2_coord = 372

    x3_coord = 228
    y3_coord = 405

    x4_coord = 372
    y4_coord = 195

    CO = 1

elif G == 4 and CO == 2:
    x_coord = 179
    y_coord = 309

    x2_coord = 421
    y2_coord = 291

    x3_coord = 309
    y3_coord = 179

    x4_coord = 291
    y4_coord = 421

    CO = 1

elif G == 5 and CO == 2:
    x_coord = 315
    y_coord = 450

    x2_coord = 285
    y2_coord = 150

    x3_coord = 450
    y3_coord = 315

    x4_coord = 150
    y4_coord = 285

    CO = 1

elif G == 6 and CO == 2:
    x_coord = 180
    y_coord = 390

    x2_coord = 420
    y2_coord = 210

    x3_coord = 390
    y3_coord = 180

    x4_coord = 210
    y4_coord = 420

    CO = 1

elif G == 7 and CO == 2:
    x_coord = 267
    y_coord = 204

    x2_coord = 333
    y2_coord = 396

    x3_coord = 204
    y3_coord = 267

    x4_coord = 396
    y4_coord = 333

    CO = 1

elif G == 8 and CO == 2:
    x_coord = 363
    y_coord = 390

    x2_coord = 237
    y2_coord = 210

    x3_coord = 390
    y3_coord = 363

    x4_coord = 210
    y4_coord = 237

    CO = 1

elif G == 9 and CO == 2:
    x_coord = 234
    y_coord = 273

    x2_coord = 366
    y2_coord = 327

    x3_coord = 273
    y3_coord = 234

    x4_coord = 327
    y4_coord = 366

    CO = 1

elif G == 10 and CO == 2:
    x_coord = 102
    y_coord = 267

    x2_coord = 498
    y2_coord = 333

    x3_coord = 267
    y3_coord = 102

    x4_coord = 333
    y4_coord = 498

    CO = 1

if V == 2:
    if event.type == event_500ms:
        if Blue == 1:
            D.fill(BLUE)
            Blue = 2
        elif Blue == 2:
            D.fill(YELLOW)
            Blue = 1



B(L1, (47, 45))
B(L2, (47, 60))
B(L2C, (65, 75))
B(L3, (47, 90))
B(L4, (47, 105))
B(L5, (47, 120))

L(D, PINDLER, (40, 5), (695, 5), 10)
L(D, PINDLER, (3, 42), (3, 495), 10)
L(D, PINDLER, (3, 495), (695, 495), 10)
L(D, PINDLER, (695, 495), (695, 5), 10)

L(D, PINDLER, (3, 41), (658, 42), 10)
L(D, PINDLER, (40, 5), (40, 458), 10)
L(D, PINDLER, (40, 458), (658, 458), 10)
L(D, PINDLER, (658, 458), (658, 42), 10)

FINISH(D, Fx, Fy)

MOLINE1(D, x_coord, y_coord)

MOLINE2(D, x2_coord, y2_coord)

MOLINE3(D, x3_coord, y3_coord)

MOLINE4(D, x4_coord, y4_coord)

MOLINE5(D, X5_coord, Y5_coord)


pygame.draw.line(D, PINDLER, (303, 302), (307, 306), 1)
pygame.draw.line(D, PINDLER, (307, 302), (303, 306), 1)






pygame.display.flip()

clock.tick(60)

我会显示所有代码,但它不喜欢正确缩进以使网站将其识别为代码。有一次,我手动在所有500行中放置四个空格。这不好玩。

根据要求提供更多信息,我冒昧地提供了所有代码。希望这可以帮助。感谢。

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

pygame.init()

BLACK =          (  0,   0,   0)
ABINO =          ( 34,  45, 102)
PINDLER =        (255, 123,  90)
MEXON =          (200, 190, 220)
WHITE =          (255, 255, 255)
YELLOW =         (255, 255,   0)
RED =            (255,   0,   0)
BLUE =           (  0,   0, 255)

size = [700, 500]
D = pygame.display.set_mode(size)
L = pygame.draw.aaline
R = pygame.draw.rect
G = random.randrange(1, 10)
GM = 1
GMT = 2
CIP = 1
CO = 2
V = 0
Blue = 1
event_500ms = pygame.USEREVENT + 1
pygame.time.set_timer(event_500ms, 500)

pygame.display.set_caption("Moline")
myfont = pygame.font.SysFont("monospace", 15)
T = myfont.render
M1S = "RULES:"
M2S = "1. To start, press each arrow key in a random sequence"
M2F = " for roughly one second."
M3S = "2. To win, re-merge the four men on the crossmark."
M4S = "3. Complete before the running man completes a lap."
M5S = "4. Mouse movement is illegal and causes a severe time penalty."
L1 = T(M1S, 1, (255, 255, 0))
L2 = T(M2S, 1, (255, 255, 0))
L2C = T(M2F, 1, (255, 255, 0))
L3 = T(M3S, 1, (255, 255, 0))
L4 = T(M4S, 1, (255, 255, 0))
L5 = T(M5S, 1, (255, 255, 0))
B = D.blit
Fx = 10
Fy = 10
L6 = T("cheeeeeeeeese", 1, (255, 255, 0))

def MOLINE1(D, x, y):
    pygame.draw.ellipse(D, BLACK, [1 + x, y, 10, 10], 0)

    pygame.draw.line(D, BLACK, [5 + x, 17 + y], [10 + x, 27 + y], 2)
    pygame.draw.line(D, BLACK, [5 + x, 17 + y], [x, 27 + y], 2)

    pygame.draw.line(D, PINDLER, [5 + x, 17 + y], [5 + x, 7 + y], 2)

    pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [9 + x, 17 + y], 2)
    pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [1 + x, 17 + y], 2)

def MOLINE2(D, x, y):
    pygame.draw.ellipse(D, BLACK, [1 + x, y, 10, 10], 0)

    pygame.draw.line(D, BLACK, [5 + x, 17 + y], [10 + x, 27 + y], 2)
    pygame.draw.line(D, BLACK, [5 + x, 17 + y], [x, 27 + y], 2)

    pygame.draw.line(D, PINDLER, [5 + x, 17 + y], [5 + x, 7 + y], 2)

    pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [9 + x, 17 + y], 2)
    pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [1 + x, 17 + y], 2)

def MOLINE3(D, x, y):
    pygame.draw.ellipse(D, BLACK, [1 + x, y, 10, 10], 0)

    pygame.draw.line(D, BLACK, [5 + x, 17 + y], [10 + x, 27 + y], 2)
    pygame.draw.line(D, BLACK, [5 + x, 17 + y], [x, 27 + y], 2)

    pygame.draw.line(D, PINDLER, [5 + x, 17 + y], [5 + x, 7 + y], 2)

    pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [9 + x, 17 + y], 2)
    pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [1 + x, 17 + y], 2)

def MOLINE4(D, x, y):
    pygame.draw.ellipse(D, BLACK, [1 + x, y, 10, 10], 0)

    pygame.draw.line(D, BLACK, [5 + x, 17 + y], [10 + x, 27 + y], 2)
    pygame.draw.line(D, BLACK, [5 + x, 17 + y], [x, 27 + y], 2)

    pygame.draw.line(D, PINDLER, [5 + x, 17 + y], [5 + x, 7 + y], 2)

    pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [9 + x, 17 + y], 2)
    pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [1 + x, 17 + y], 2)

def MOLINE5(D, x, y):
    pygame.draw.ellipse(D, BLACK, [1 + x, y, 10, 10], 0)

    pygame.draw.line(D, BLACK, [5 + x, 17 + y], [10 + x, 27 + y], 2)
    pygame.draw.line(D, BLACK, [5 + x, 17 + y], [x, 27 + y], 2)

    pygame.draw.line(D, PINDLER, [5 + x, 17 + y], [5 + x, 7 + y], 2)

    pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [9 + x, 17 + y], 2)
    pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [1 + x, 17 + y], 2)

def FINISH(D, x, y):
    R(D, BLACK, ( 4, 42, 6, 6))
    R(D, WHITE, (10, 42, 6, 6))
    R(D, BLACK, (16, 42, 6, 6))
    R(D, WHITE, (22, 42, 6, 6))
    R(D, BLACK, (28, 42, 6, 6))
    R(D, WHITE, (34, 42, 6, 6))

    R(D, WHITE, ( 4, 48, 6, 6))
    R(D, BLACK, (10, 48, 6, 6))
    R(D, WHITE, (16, 48, 6, 6))
    R(D, BLACK, (22, 48, 6, 6))
    R(D, WHITE, (28, 48, 6, 6))
    R(D, BLACK, (34, 48, 6, 6))

    R(D, BLACK, ( 4, 54, 6, 6))
    R(D, WHITE, (10, 54, 6, 6))
    R(D, BLACK, (16, 54, 6, 6))
    R(D, WHITE, (22, 54, 6, 6))
    R(D, BLACK, (28, 54, 6, 6))
    R(D, WHITE, (34, 54, 6, 6))

x_speed = 0
y_speed = 0

x2_speed = 0
y2_speed = 0

x3_speed = 0
y3_speed = 0

x4_speed = 0
y4_speed = 0

x_coord = 300
y_coord = 300

x2_coord = 300
y2_coord = 300

x3_coord = 300
y3_coord = 300

x4_coord = 300
y4_coord = 300

X5_coord = 30
Y5_coord = 10

direction = 'right'

clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_speed =- 3
                x2_speed = 3
                y3_speed =- 3
                y4_speed = 3
            elif event.key == pygame.K_RIGHT:
                x_speed = 3
                x2_speed =- 3
                y3_speed = 3
                y4_speed =- 3
            elif event.key == pygame.K_UP:
                y_speed =- 3
                y2_speed = 3
                x3_speed =- 3
                x4_speed = 3
            elif event.key == pygame.K_DOWN:
                y_speed = 3
                y2_speed =- 3
                x3_speed = 3
                x4_speed =- 3
            elif event.key == pygame.K_TAB:
                GMT += 1
                CIP = 2
                if GMT % 2:
                    GM = 2
                else:
                    GM = 1

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                x_speed=0
                x2_speed=0
                y3_speed=0
                y4_speed=0
            elif event.key == pygame.K_RIGHT:
                x_speed=0
                x2_speed=0
                y3_speed=0
                y4_speed=0
            elif event.key == pygame.K_UP:
                y_speed=0
                y2_speed=0
                x3_speed=0
                x4_speed=0
            elif event.key == pygame.K_DOWN:
                y_speed=0
                y2_speed=0
                x3_speed=0
                x4_speed=0

        elif event.type == MOUSEBUTTONDOWN:
            x_coord = 300
            y_coord = 300
            x2_coord = 300
            y2_coord = 300
            x3_coord = 300
            y3_coord = 300
            x4_coord = 300
            y4_coord = 300
            X5_coord = 30
            Y5_coord = 10
            direction = 'right'
            D.fill(BLACK)
            G = random.randrange(1, 10)
            CO = 2
            V = 2

        elif event.type == MOUSEBUTTONUP:
            X5_coord = 30
            Y5_coord = 10
            direction = 'right'

        if direction == 'right':
            X5_coord += 10
            if X5_coord == 670:
                direction = 'down'
        elif direction == 'down':
            Y5_coord += 10
            if Y5_coord == 460:
                direction = 'left'
        elif direction == 'left':
            X5_coord -= 10
            if X5_coord == 10:
                direction = 'up'
        elif direction == 'up':
            Y5_coord -= 10
            if Y5_coord == 10:
                direction = 'right'





    x_coord = x_coord + x_speed
    y_coord = y_coord + y_speed

    x2_coord = x2_coord + x2_speed
    y2_coord = y2_coord + y2_speed

    x3_coord = x3_coord + x3_speed
    y3_coord = y3_coord + y3_speed

    x4_coord = x4_coord + x4_speed
    y4_coord = y4_coord + y4_speed

    if GM == 2 and CIP == 2:
        D.fill(BLACK)
        CIP = 1

    if GM == 1:
        D.fill(MEXON)

    if G >= 6:
        B(L6, (80, 150))

    if G == 1 and CO == 2:
        x_coord = 366
        y_coord = 201

        x2_coord = 234
        y2_coord = 399

        x3_coord = 201
        y3_coord = 366

        x4_coord = 399
        y4_coord = 234

        CO = 1

    elif G == 2 and CO == 2:
        x_coord = 240
        y_coord = 312

        x2_coord = 360
        y2_coord = 288

        x3_coord = 312
        y3_coord = 240

        x4_coord = 288
        y4_coord = 360

        CO = 1

    elif G == 3 and CO == 2:
        x_coord = 405
        y_coord = 228

        x2_coord = 195
        y2_coord = 372

        x3_coord = 228
        y3_coord = 405

        x4_coord = 372
        y4_coord = 195

        CO = 1

    elif G == 4 and CO == 2:
        x_coord = 179
        y_coord = 309

        x2_coord = 421
        y2_coord = 291

        x3_coord = 309
        y3_coord = 179

        x4_coord = 291
        y4_coord = 421

        CO = 1

    elif G == 5 and CO == 2:
        x_coord = 315
        y_coord = 450

        x2_coord = 285
        y2_coord = 150

        x3_coord = 450
        y3_coord = 315

        x4_coord = 150
        y4_coord = 285

        CO = 1

    elif G == 6 and CO == 2:
        x_coord = 180
        y_coord = 390

        x2_coord = 420
        y2_coord = 210

        x3_coord = 390
        y3_coord = 180

        x4_coord = 210
        y4_coord = 420

        CO = 1

    elif G == 7 and CO == 2:
        x_coord = 267
        y_coord = 204

        x2_coord = 333
        y2_coord = 396

        x3_coord = 204
        y3_coord = 267

        x4_coord = 396
        y4_coord = 333

        CO = 1

    elif G == 8 and CO == 2:
        x_coord = 363
        y_coord = 390

        x2_coord = 237
        y2_coord = 210

        x3_coord = 390
        y3_coord = 363

        x4_coord = 210
        y4_coord = 237

        CO = 1

    elif G == 9 and CO == 2:
        x_coord = 234
        y_coord = 273

        x2_coord = 366
        y2_coord = 327

        x3_coord = 273
        y3_coord = 234

        x4_coord = 327
        y4_coord = 366

        CO = 1

    elif G == 10 and CO == 2:
        x_coord = 102
        y_coord = 267

        x2_coord = 498
        y2_coord = 333

        x3_coord = 267
        y3_coord = 102

        x4_coord = 333
        y4_coord = 498

        CO = 1

    if V == 2:
        if event.type == event_500ms:
            if Blue == 1:
                D.fill(BLUE)
                Blue = 2
            elif Blue == 2:
                D.fill(YELLOW)
                Blue = 1



    B(L1, (47, 45))
    B(L2, (47, 60))
    B(L2C, (65, 75))
    B(L3, (47, 90))
    B(L4, (47, 105))
    B(L5, (47, 120))

    L(D, PINDLER, (40, 5), (695, 5), 10)
    L(D, PINDLER, (3, 42), (3, 495), 10)
    L(D, PINDLER, (3, 495), (695, 495), 10)
    L(D, PINDLER, (695, 495), (695, 5), 10)

    L(D, PINDLER, (3, 41), (658, 42), 10)
    L(D, PINDLER, (40, 5), (40, 458), 10)
    L(D, PINDLER, (40, 458), (658, 458), 10)
    L(D, PINDLER, (658, 458), (658, 42), 10)

    FINISH(D, Fx, Fy)

    MOLINE1(D, x_coord, y_coord)

    MOLINE2(D, x2_coord, y2_coord)

    MOLINE3(D, x3_coord, y3_coord)

    MOLINE4(D, x4_coord, y4_coord)

    MOLINE5(D, X5_coord, Y5_coord)


    pygame.draw.line(D, PINDLER, (303, 302), (307, 306), 1)
    pygame.draw.line(D, PINDLER, (307, 302), (303, 306), 1)






    pygame.display.flip()

    clock.tick(60)

pygame.quit()

认识到,这不是一个成品,因此在某些方面可能看起来很尴尬,因为这个程序已经发展了很多。因此可能有一些未使用的全局变量或一些不准确的文本。

3 个答案:

答案 0 :(得分:3)

问题是,当您使用PyGame的pygame.time.wait()pygame.time.delay()函数时,无法在游戏暂停的时间内获取任何事件 time模块。

要避免此问题,您可以创建自己的事件,该事件在每个给定的毫秒数的事件队列中出现。您可以使用pygame.time.set_timer()创建活动:

import pygame    
pygame.init()

#color tupels
BLUE = (55,155,255)
YELLOW = (255,255,0)


#create a new event id called event_500ms
event_500ms = pygame.USEREVENT + 1

#set timer event to appear on the event queue every 500 milliseconds
pygame.time.set_timer(event_500ms, 500)

screen = pygame.display.set_mode((800,800))

#global variable V for state-machine
V = 0

while True:
    #get events from the event queue
    for ev in pygame.event.get():
        if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
                pygame.quit()
                exit()
        #left (ev.button == 3) mousbutton down -> set variable V = 1
        if ev.type == pygame.MOUSEBUTTONDOWN and ev.button == 3:
            V = 1

        #check for the event_500ms event    
        if ev.type == event_500ms:
            if V == 1:
                screen.fill(BLUE)
                V = 2
            elif V == 2:
                screen.fill(YELLOW)
                V = 0

            pygame.display.flip() #update whole screen

在这段代码中,我们使用了一种具有3个不同阶段的所谓state-machine

  1. 设置V = 1 ,如果pygame.MOUSEBUTTONDOWN事件有效,(下一状态:2)
  2. 如果变量screen等于V,请填充V蓝色并增加1 (下一个州:3)
  3. 如果变量screen等于V,请填充0黄色,然后将V恢复为2 (下一个州:1)
  4. 我希望这会有所帮助:)

答案 1 :(得分:1)

你有两个解决问题的方法,它代表了你尝试过的两种方法:你要么等待一个特定的时间进行下一次打勾,要么每次打勾都要做一些事情,但是你有问题,打勾取决于计算机的速度是(以及其他因素,如在后台运行的程序)。 (勾选我指的是你可能使用的每个主循环运行。)

当你说'#34;好的时候,我想每隔四个刻度改变一次背景",问题可能是每一个毫秒,或每秒或每当。因此,我们需要不依赖于发生的滴答数量,而是取决于时间量。我们可以使用time.perf_counter()来获取当前时间。这给了我们:

#Initialize old_time somewhere
new_time = time.perf_counter()
if new_time - old_time > 0.05:
    change_background()
    old_time = new_time

通常情况下,你不想为每件依赖时间的事情做这件事。而是在while循环中的第一件事计算增量时间,通常称为dt

while True:
    dt = time.perf_counter() - old_time
    old_time = time.perf_counter()
    change_background(dt)

然后按照我们处理之前的方式处理dt

def change_background(dt):
    cumulative_time += dt
    if cumulative_time > 0.05:
        #do things
        cumulative_time = 0

另一种方法是使用某种sleep。正如您已经看到的那样,这将冻结整个线程 - 但您可以创建多个线程(当然您不需要使用线程,您可以使用进程或greenlet或所有其他花哨的多处理内容)。整个过程有点复杂,所以我不会在这里详细介绍,但通常最好有不同的线程进行输入和处理。相对较新的asyncio模块正好处理这类问题。还有不同的解决方案,例如greenletsthreads。做事asyncronuos一开始可能会令人困惑,第一个解决方案可能会让你相对较远。

编辑: 我的答案独立于pygame,其他答案中使用的事件在后台为您做了这个。理解这个增量时间的想法并使用它是一个好主意,例如在物理计算中。一般依赖于刻度(而不是时间)是一个坏主意,可以给你意想不到的行为。据说原始的太空入侵者确实依赖于物理和图形计算的滴答声。因此,每当你射击足够的敌人时它会加速。这是一个意想不到的结果(然后保留,因为它是一个增加难度的好机制)。模拟器面临同样的问题:通常旧游戏(依赖于刻度而不是按时)会快速运行,因为现代计算机具有更强大的处理能力。模拟器必须考虑到这一点,并且减慢处理速度"。

答案 2 :(得分:-1)

也许你可以试试:

import time
time.sleep(5)