微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在python 3中制作矩形选择工具?

如何解决如何在python 3中制作矩形选择工具?

我正在尝试制作一个简单的游戏,其中用户可以使用RTS样式的框选择(例如《星际争霸》,《魔兽争霸》)在屏幕上选择单位。在浏览了我在Google以及其他人身上发现的很多内容之后,我仍然不知道它是如何完成的。

环顾四周时,我发现了这一点:

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


# set up pygame
pygame.init()
mainClock = pygame.time.Clock()


# set up the window
WINDOWWIDTH = 700
WINDOWHEIGHT = 700
CAPTION = 'RTS SELCTION Box TEST'
windowSurface = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
pygame.display.set_caption(CAPTION)


# set constants
# set up the colors
BLACK = (0,0)
RED = (255,0)
GREEN = (0,255,0)
BLUE = (0,255)
WHITE = (255,255)
# color of background
BACKGROUNDCOLOR = GREEN
# set these values to avoid errors on startup
draw_new_selection_Box = False
selection_completed = False
# test units (Boxes)
units = [1,2]
units[0] = pygame.Rect((100,200),(40,40))
units[1] = pygame.Rect((300,360),40))
selected_units = []


# game loop
game_loop = True
while game_loop:
    # check for events and change variables based on events
    for event in pygame.event.get():
        # check if user has quit
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()
  
        check_which_mouse_button = pygame.mouse.get_pressed()
        if event.type == MOUSEBUTTONDOWN:
            if check_which_mouse_button[0]:
                leftclick_down_location = pygame.mouse.get_pos()
                draw_new_selection_Box = True
        if event.type == MOUSEBUTTONUP:
            if not check_which_mouse_button[0]:
                leftclick_up_location = pygame.mouse.get_pos()
                draw_new_selection_Box = False
                selection_completed = True

    
    # use event variables to change variables
    current_mouse_location = pygame.mouse.get_pos()
    if draw_new_selection_Box:
        selection_Box_x = current_mouse_location[0] - leftclick_down_location[0]
        selection_Box_y = current_mouse_location[1] - leftclick_down_location[1]
        selection_Box = pygame.Rect((leftclick_down_location),(selection_Box_x,selection_Box_y))
    elif selection_completed:
        completed_selection_Box_x = leftclick_up_location[0] - leftclick_down_location[0]
        completed_selection_Box_y = leftclick_up_location[1] - leftclick_down_location[1]
        completed_selection_Box = pygame.Rect((leftclick_down_location),(completed_selection_Box_x,completed_selection_Box_y))

                    
    # draw the background onto surface
    windowSurface.fill(BACKGROUNDCOLOR)


    # draw selection Box and units onto the Windowsurface
    for unit in units:
        pygame.draw.rect(windowSurface,WHITE,unit,2)
    if draw_new_selection_Box:
        pygame.draw.rect(windowSurface,RED,selection_Box,2)
        for unit in units:
            if selection_Box.colliderect(unit):
                pygame.draw.rect(windowSurface,BLUE,2)
     

    # draw the windowSurface onto the screen
    pygame.display.update()
    mainClock.tick(60)

问题是,当用户尝试使盒子上下移动(除了上下移动)时,它要么不显示任何盒子,要么显示一个很大的盒子。

这是我的第一个问题,因此,如果需要澄清,我会尽力而为。

解决方法

从左上角的位置和宽度和高度创建一个pygame.Rect。宽度和高度必须为正。您需要找到两个点的最小和最大x和y坐标。编写一个从两点创建pygame.Rect对象的函数:

def create_box(p1,p2):
    x1,y1 = min(p1[0],p2[0]),min(p1[1],p2[1])
    x2,y2 = max(p1[0],max(p1[1],p2[1])
    return pygame.Rect(x1,y1,x2-x1,y2-y1)

或计算点之间距离的绝对值(abs):

def create_box(p1,p2):
    return pygame.Rect(min(p1[0],p2[1]),abs(p2[0]-p1[0]),abs(p2[1]-p1[1]))

在应用程序循环中使用该函数:

game_loop = True
while game_loop:
    # [...]

    current_mouse_location = pygame.mouse.get_pos()   
    if draw_new_selection_box:
        selection_box = create_box(leftclick_down_location,current_mouse_location)
    elif selection_completed:
        completed_selection_box = create_box(leftclick_down_location,leftclick_up_location)

    # [...]

此外,如果pygame.draw.rect()的高度小于或等于轮廓线宽度的两倍,则other question无法正确绘制矩形。为此,您必须实现一个特殊情况:

game_loop = True
while game_loop:
    # [...]

    if draw_new_selection_box:
        if selection_box.width < 4 or selection_box.height < 4:
            rect = pygame.Rect(*selection_box.topleft,max(2,selection_box.width),selection_box.height))
            pygame.draw.rect(windowSurface,RED,rect)
        else:
            pygame.draw.rect(windowSurface,selection_box,2)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。