Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python3中创建矩形选择工具?_Python_Python 3.x_Pygame - Fatal编程技术网

如何在Python3中创建矩形选择工具?

如何在Python3中创建矩形选择工具?,python,python-3.x,pygame,Python,Python 3.x,Pygame,我正在尝试制作一个简单的游戏,用户可以在屏幕上使用RTS风格的方框选择(例如星际争霸、魔兽争霸)来选择单位。在我从谷歌和其他人那里找到了很多东西之后,我仍然不知道它是怎么做到的 环顾四周,我发现: import pygame, sys, random from pygame.locals import * # set up pygame pygame.init() mainClock = pygame.time.Clock() # set up the window WINDOWWIDT

我正在尝试制作一个简单的游戏,用户可以在屏幕上使用RTS风格的方框选择(例如星际争霸、魔兽争霸)来选择单位。在我从谷歌和其他人那里找到了很多东西之后,我仍然不知道它是怎么做到的

环顾四周,我发现:

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, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 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, 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, unit, 2)
     

    # draw the windowSurface onto the screen
    pygame.display.update()
    mainClock.tick(60)
问题是,当用户试图让盒子往任何方向移动时,除了向下和向左,它要么没有显示盒子,要么显示一个非常大的盒子

这是我在这里的第一个问题,因此如果我需要澄清,我将尽我所能。

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

def创建框(p1、p2):
x1,y1=min(p1[0],p2[0]),min(p1[1],p2[1])
x2,y2=max(p1[0],p2[0]),max(p1[1],p2[1])
return pygame.Rect(x1,y1,x2-x1,y2-y1)
或计算点之间距离的绝对值(
abs
):

def创建框(p1、p2):
return pygame.Rect(min(p1[0],p2[0]),min(p1[1],p2[1]),
abs(p2[0]-p1[0]),abs(p2[1]-p1[1]))
使用应用程序循环中的函数:

game\u loop=True
在游戏循环中:
# [...]
当前鼠标位置=pygame.mouse.get\u pos()
如果绘制新选择框:
选择\框=创建\框(左键单击\下键\位置,当前\鼠标\位置)
elif选择已完成:
已完成的\u选择\u框=创建\u框(左键单击\u向下\u位置,
左键单击(向上单击位置)
# [...]

此外,如果高度小于轮廓宽度的两倍,则无法正确绘制矩形。为此,您必须实施一种特殊情况:

game\u loop=True
在游戏循环中:
# [...]
如果绘制新选择框:
如果选择框宽度小于4或选择框高度小于4:
rect=pygame.rect(*选择框左上角,
最大值(2,选择框宽度),
最大值(2,选择框高度))
pygame.draw.rect(窗口表面,红色,rect)
其他:
pygame.draw.rect(窗口表面,红色,选择框,2)