这个循环似乎不是常数,但我不知道为什么

时间:2015-03-09 16:39:49

标签: python pygame

我正在尝试为我的测试游戏测试一个随机数生成器,我一直在努力。目前设置为始终执行动作而不管生成的数字,并简单地说" 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()

1 个答案:

答案 0 :(得分:1)

你快乐你的文字" cheeeeeeeese" (程序中的变量L6)到显示表面D,更新它(调用pygame.display.update()),然后在此之后填充整个表面致电D.fill(MEXON)

结果:每次一个事件发生时,文字会短暂闪烁,因为你"清除"在绘制文本后立即调用pygames fill()函数来显示屏幕。

您可以更改以解决问题:

  1. 删除事件循环中的pygame.display.update()行。
    您不需要这样做,因为您在主游戏循环(pygame.display.flip())中翻转(更新)显示器。

  2. 编写代码行

    if G >= 0:
        B(L6, (80, 150))
    
    之后

    调用D.fill(MEXON)方法。
    这可以解决你的闪烁问题。