【问题标题】:pygame display doesn't update after initial array drawingpygame 显示在初始数组绘制后不更新
【发布时间】:2020-08-29 01:17:35
【问题描述】:

我作为一个副项目开始研究排序算法可视化工具。 我不知道这里上传多个文件的规则,如果我违反了规则,请提前道歉。

项目布局:

目录 - 几何:point.py、line.py

main.py

文件如下:

point.py:

class Point:
    def __init__(self,x, y):
        self.x = x
        self.y = y

    def get_x(self):
        return self.x

    def get_y(self):
        return self.y

    def set_x(self, new_x):
        self.x = new_x

    def set_y(self, new_y):
        self.y = new_y

接下来是line.py:

from geometry.point import Point
import pygame as pg

class Line:

    def __init__(self, start_point, end_point, color, width):
        self.start = start_point
        self.end = end_point
        self.size = self.start.get_y() - self.end.get_y()
        self.color = color
        self.width = width

    def get_start(self):
        return self.start

    def get_end(self):
        return self.end

    def get_size(self):
        return self.size

    def get_color(self):
        return self.color

    def draw_on_board(self, window):
        pg.draw.line(window, self.color, (self.start.get_x(), self.start.get_y()), (self.end.get_x(), self.end.get_y()), self.width)

    def set_start(self, new_start):
        self.start = new_start
        self.size = self.start.get_y() - self.end.get_y()

    def set_end(self, new_end):
        self.end = new_end
        self.size = self.start.get_y() - self.end.get_y()

最后,main.py:

import sys
from geometry.point import Point
from geometry.line import Line
import random
import time
import os
import pygame

pygame.init()
WIN_WIDTH = 1280
WIN_HEIGHT = 720
window_size = (WIN_WIDTH, WIN_HEIGHT)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
LINE_COLOR = (153, 255, 51)
gap = 10
start = 10
end = 500
line_width = 5
window = pygame.display.set_mode(window_size)
pygame.display.set_caption("Sorting Algorithms Visualization")


def draw_background(win):
    win.fill(WHITE)

def create_arr_and_lines():
    x_start = 250
    y_start = WIN_HEIGHT - gap * 5
    numbers = list(range(gap, end + gap * 5, gap))
    shuffle_array(numbers)
    lines = []
    for i in range(len(numbers)):
        start_point = Point(x_start + gap * i, y_start)
        end_point = Point(x_start + gap * i, y_start - numbers[i])
        line = Line(start_point, end_point, VALUE_COLOR, line_width)
        lines.append(line)
    return lines


def print_arr(lines, win):
    draw_background(win)
    for line in lines:
        line.draw_on_board(win)
    pygame.display.flip()


def bubble_sort(lines, win):
    n = len(lines)
    for i in range(n):
        for j in range(n - i - 1):
            if lines[j].get_end().get_y() < lines[j+1].get_end().get_y():
                lines[j], lines[j+1] = lines[j+1], lines[j]
                print_arr(lines, win)
            pygame.display.flip()
            time.sleep(0.005)


def shuffle_array(arr):
    random.shuffle(arr)


def main():
    
    lines = create_arr_and_lines()
    running = True
    done_sorting = False
    while running:
        if done_sorting:
            print_arr(lines, window)
            pygame.display.update()
            time.sleep(2)
            running = False
        print_arr(lines, window)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if not done_sorting:
                    bubble_sort(lines, window)
                    done_sorting = True

            else:
                continue

        pygame.display.update()


main()

再次为长篇道歉,有谁知道为什么排序开始后显示不更新? (意思是在我按下鼠标按钮之后) 提前致谢!

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    冒泡排序工作正常,行在列表中移动。问题是线条的 x 值永远不会改变。

    试试这个代码:

    def bubble_sort(lines, win):
        n = len(lines)
        for i in range(n):
            for event in pygame.event.get():
               if event.type == pygame.QUIT: return
            for j in range(n - i - 1):
                if lines[j].get_end().get_y() < lines[j+1].get_end().get_y():
                    lines[j], lines[j+1] = lines[j+1], lines[j]
                    for i,ln in enumerate(lines):  # update x coordinates
                       ln.start.x = 250 + gap * i
                       ln.end.x = 250 + gap * i
                    print_arr(lines, win)
                time.sleep(0.01)
    

    【讨论】:

    • 你是救生员!我在发布帖子 20 分钟后发现了它,但在正确更新 x 坐标时遇到了一些问题。再次感谢!
    • 我实际上最终得到了一些更有效率的东西。而不是添加另一个 for 循环,我只更新了在恒定时间内切换的两条线的 x-cords 值,再次感谢
    【解决方案2】:

    我认为这是因为您在 print_arr() 和冒泡排序中都使用了 display.flip() 两次。所以 screen 在 if 子句中更新一次,一旦它离开 if 子句。因此最后的更新消失了。也许尝试在冒泡排序 for 循环中删除 display.flip()。

    def bubble_sort(lines, win):
        n = len(lines)
        for i in range(n):
            for j in range(n - i - 1):
                if lines[j].get_end().get_y() < lines[j+1].get_end().get_y():
                    lines[j], lines[j+1] = lines[j+1], lines[j]
                    print_arr(lines, win) #flip() gets called at end of it
                pygame.display.flip()  #remove it
                time.sleep(0.005)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多