简体   繁体   English

Pygame 在特定阈值后不会在屏幕上绘制矩形

[英]Pygame doesn't draw rectangles on the screen after a specific threshold

I'm trying to make a visualisation for various sorting algorithms and I was playing around with Pygame to try and draw the rectangles that I need.我正在尝试为各种排序算法进行可视化,并且我正在使用Pygame尝试绘制我需要的矩形。

In the below code, the user is given multiples inputs: the lowest value of the list to be sorted, the highest value , and the number of elements the list is going to have.在下面的代码中,为用户提供了多个输入:要排序的列表的最低值最高值以及列表将要包含的元素数 The elements are going to be randomly generated.元素将随机生成。

Then I'm getting the user's screen size so that I can have an appropriate window for the visualisation.然后我得到用户的屏幕尺寸,以便我可以有一个合适的窗口进行可视化。 Based on the visualisation window and the user's input, I'm setting up the width and height of the rectangles, so that each rectangle has the same width and that they are scaled based on the highest value .根据可视化窗口和用户的输入,我正在设置矩形的宽度高度,以便每个矩形具有相同的宽度,并根据最大值进行缩放。

Almost everything is nice and fine with this approach, but there's one thing that I can't figure out.这种方法几乎一切都很好,但有一件事我想不通。 It seems that setting the number of elements ( n , in the code below) too high, the rectangles are not being drawn.似乎将元素数量n ,在下面的代码中)设置得太高,矩形没有被绘制。

My asumption is that after a specific threshold, RECT_W , which is the width of the rectangles, becomes to small for Pygame to draw it.我的假设是,在特定阈值之后,矩形的宽度RECT_W变得很小, Pygame无法绘制它。

What options do I have to solve it, except of having the number of elements smaller than a specific value?除了元素数量小于特定值之外,我有什么选择来解决它?

import random
import pygame
import color_constants as colors
import ctypes
import copy
from pygame.locals import *


# Read data based on user's input
def readData():
    listOfNumbers = []
    data = dict()

    print("Lowest Value: ")
    numLow = int(input())

    print("Highest Value: ")
    numHigh = int(input())

    print("Length of list: ")
    n = int(input())

    for i in range(0, n):
        listOfNumbers.append(random.randint(numLow, numHigh))

    origLst = copy.copy(listOfNumbers)
    data.update({'lst': origLst})
    data.update({'numLow': numLow})
    data.update({'numHigh': numHigh})
    data.update({'n': n})
    data.update({'sorted': listOfNumbers})

    return data

if __name__ == "__main__":
    data = readData()

    # Getting the user's screen size
    user32 = ctypes.windll.user32
    SCREENSIZE = user32.GetSystemMetrics(0)-100, user32.GetSystemMetrics(1)-100
    SCREEN_W = SCREENSIZE[0]
    SCREEN_H = SCREENSIZE[1]

    # Setting and scaling the size of rectangle based on the number of elements (n)
    # and the highest number (numHigh)
    RECT_W = SCREEN_W // data['n']
    RECT_H = SCREEN_H / (data['numHigh'])

    # Setting up the color literals
    RED = (255, 255, 255)
    GRAY = (0, 0, 0)

    pygame.init()
    screen = pygame.display.set_mode(SCREENSIZE)

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
        screen.fill(GRAY)

        for i in range(data['n']):
            rect = Rect(i*RECT_W, 0, RECT_W, RECT_H * data['lst'][i])
            rect.bottom = SCREEN_H
            pygame.draw.rect(screen, RED, rect)
        pygame.display.flip()

If data['n'] is greater than SCREEN_W , RECT_W is 0. A coordinate is truncated when drawing.如果data['n']大于SCREEN_W ,则RECT_W为 0。绘制时坐标被截断。 You cannot draw a fraction of a pixel.您不能绘制像素的一小部分。 The size of the rectangle can only be integral (0, 1, 2 ...).矩形的大小只能是整数 (0, 1, 2 ...)。 Hence, you cannot draw a rectangle with a size less than 1.因此,您不能绘制尺寸小于 1 的矩形。
You can draw the rectangles on a large surface and scale down the surface.您可以在大表面上绘制矩形并缩小表面。 However, the rectangles get blurred.但是,矩形变得模糊。 So, this is no good option.所以,这不是一个好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM