【问题标题】:Pygame making a selection like in stategy gamesPygame 在策略游戏中做出选择
【发布时间】:2018-03-16 12:19:26
【问题描述】:

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

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完成。

谢谢。

【问题讨论】:

    标签: pygame


    【解决方案1】:

    每帧应该只有一个 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()
    

    【讨论】:

    • 啊,是的,如果我先填满,我只需要翻转一次,谢谢 :D
    猜你喜欢
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 2012-03-17
    相关资源
    最近更新 更多