Pygame在战略游戏中做出选择

时间:2018-03-16 12:19:27

标签: pygame

所以我这样做是为了选择战略游戏中的区域 屏幕一直闪烁,有没有办法解决这个问题?

import pygame
from pygame.locals import *

WHITE = (255,255,255)
BLUE = (0,0,255)

pygame.init()
window = pygame.display.set_mode((640, 480))
window.fill(WHITE)
pygame.display.flip()

LEFT_CLIC = 1
mouse_tracking = False
draw_area = False
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            continuer = 0

        if event.type == MOUSEBUTTONDOWN:
            if event.button == LEFT_CLIC:
                x_start, y_start = event.pos
                x_end, y_end = event.pos
                mouse_tracking = True
                draw_area = True

        if event.type == MOUSEMOTION and mouse_tracking:
            x_end, y_end = event.pos

        if event.type == MOUSEBUTTONUP:
            if event.button == LEFT_CLIC:
                x_end, y_end = event.pos
                mouse_tracking = True
                draw_area = False

    if draw_area:
        width = x_end-x_start
        height = y_end-y_start
        pygame.draw.rect(window, BLUE, (x_start, y_start, width, height))
    pygame.display.flip()
    window.fill(WHITE)
    pygame.display.flip()

所以它非常简单,当有clic时记录坐标,然后按照鼠标直到clic完成。

感谢。

1 个答案:

答案 0 :(得分:2)

每帧只能进行一次pygame.display.flip()次呼叫,否则会出现这种闪烁,请删除其中一个。另外,在绘制矩形之前填充屏幕。

window.fill(WHITE)

if draw_area:
    width = x_end-x_start
    height = y_end-y_start
    pygame.draw.rect(window, BLUE, (x_start, y_start, width, height))

pygame.display.flip()