这个循环似乎不是恒定的,但我不知道为什么
This loop appears to not be constant, but I don't know why
我正在尝试为我一直在开发的测试游戏测试随机数生成器。它目前设置为始终执行操作,无论生成的数字如何,只需在屏幕上说 "cheeeeeeeese",但我的问题是它会在通过鼠标或箭头键输入时闪烁该词,但不会坚持。我不明白为什么它只在我输入时闪烁。
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)
size = [700, 500]
D = pygame.display.set_mode(size)
L = pygame.draw.aaline
R = pygame.draw.rect
G = random.randrange(1, 10)
pygame.display.set_caption("Tester of Mester")
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 leaves the screen."
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.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'
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'
if G >= 0:
B(L6, (80, 150))
pygame.display.update()
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
D.fill(MEXON)
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()
您将文本 "cheeeeeeeese"(程序中的变量 L6
)blit 到显示表面 D
,更新它(调用 pygame.display.update()
),然后紧接着,您通过调用 D.fill(MEXON)
.
填充 整个 表面
结果:每次一个事件发生时文本会短暂闪烁,因为你"clear"屏幕通过调用pygamesfill()
函数在绘制后立即正文。
您可以更改哪些内容来解决问题:
删除事件循环中的 pygame.display.update()
行。
您不需要这样做,因为您在主游戏循环 (pygame.display.flip()
) 中经常翻转(更新)显示。
写代码行
if G >= 0:
B(L6, (80, 150))
在你调用了D.fill(MEXON)
方法之后。
这应该可以解决您的闪烁问题。
我正在尝试为我一直在开发的测试游戏测试随机数生成器。它目前设置为始终执行操作,无论生成的数字如何,只需在屏幕上说 "cheeeeeeeese",但我的问题是它会在通过鼠标或箭头键输入时闪烁该词,但不会坚持。我不明白为什么它只在我输入时闪烁。
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)
size = [700, 500]
D = pygame.display.set_mode(size)
L = pygame.draw.aaline
R = pygame.draw.rect
G = random.randrange(1, 10)
pygame.display.set_caption("Tester of Mester")
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 leaves the screen."
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.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'
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'
if G >= 0:
B(L6, (80, 150))
pygame.display.update()
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
D.fill(MEXON)
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()
您将文本 "cheeeeeeeese"(程序中的变量 L6
)blit 到显示表面 D
,更新它(调用 pygame.display.update()
),然后紧接着,您通过调用 D.fill(MEXON)
.
结果:每次一个事件发生时文本会短暂闪烁,因为你"clear"屏幕通过调用pygamesfill()
函数在绘制后立即正文。
您可以更改哪些内容来解决问题:
删除事件循环中的
pygame.display.update()
行。
您不需要这样做,因为您在主游戏循环 (pygame.display.flip()
) 中经常翻转(更新)显示。写代码行
if G >= 0: B(L6, (80, 150))
在你调用了
D.fill(MEXON)
方法之后。
这应该可以解决您的闪烁问题。