为什么我的代码无法在 pygame 中打开 window?
Why will my code not open a window in pygame?
到目前为止应该只能打开window,但在测试过程中,它不会打开window,尽管没有显示任何错误。
我已经尝试使用“pygame.disply.flip()”而不是更新,但没有任何改变。
我也在终端中得到这个:
pygame2.1.2(SDL 2.0.18,Python3.10.1)
来自 pygame 社区的问候。 https://www.pygame.org/contribute.html
就是这样。
import pygame
import random
pygame.init()
class DrawInformation:
BLACK = 0, 0, 0
WHITE = 255, 255, 255
GREEN = 0, 255, 0
RED = 255, 0, 0
GREY = 128, 128, 128
BACKGROUND_COLOR = WHITE
SIDE_PAD = 100
TOP_PAD = 150
def __init__(self, width, height, lst):
self.width = width
self.height = height
self.window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Sorting Algorithm Visualization")
self.set_list(lst)
def set_list(self, lst):
self.list = lst
self.min_val = min(lst)
self.max_val = max(lst)
self.block_width = round((self.width - self.SIDE_PAD) / len(lst))
self.block_height = round((self.height - self.TOP_PAD) / (self.max_val - self.min_val))
self.start_x = self.SIDE_PAD // 2
def generate_starting_list(n, min_val, max_val):
lst = []
for _ in range(n):
val = random.randint(min_val, max_val)
lst.append(val)
return lst
def main():
run = True
clock = pygame.time.Clock()
n = 50
min_val = 0
max_val = 100
lst = generate_starting_list(n, min_val, max_val)
draw_info = DrawInformation(800, 600, lst)
while run:
clock.tick(60)
pygame.display.update()
for event in pygame.event.get():
if event == pygame.QUIT:
run = False
pygame.quit()
if __name__ == "__main__":
main()
这是我目前所知道的...它还说“draw_info”没有被 pylance 和 idk 访问,为什么??
行
if __name__ == "__main__":
main()
的目的是 def main()
所以程序启动的时候不是运行
这是更正后的代码。
import random
pygame.init()
class DrawInformation:
BLACK = 0, 0, 0
WHITE = 255, 255, 255
GREEN = 0, 255, 0
RED = 255, 0, 0
GREY = 128, 128, 128
BACKGROUND_COLOR = WHITE
SIDE_PAD = 100
TOP_PAD = 150
def __init__(self, width, height, lst):
self.width = width
self.height = height
self.window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Sorting Algorithm Visualization")
self.set_list(lst)
def set_list(self, lst):
self.list = lst
self.min_val = min(lst)
self.max_val = max(lst)
self.block_width = round((self.width - self.SIDE_PAD) / len(lst))
self.block_height = round((self.height - self.TOP_PAD) / (self.max_val - self.min_val))
self.start_x = self.SIDE_PAD // 2
def generate_starting_list(n, min_val, max_val):
lst = []
for _ in range(n):
val = random.randint(min_val, max_val)
lst.append(val)
return lst
def main():
run = True
clock = pygame.time.Clock()
n = 50
min_val = 0
max_val = 100
lst = generate_starting_list(n, min_val, max_val)
draw_info = DrawInformation(800, 600, lst)
while run:
clock.tick(60)
pygame.display.update()
for event in pygame.event.get():
if event == pygame.QUIT:
run = False
pygame.quit()
if __name__ == "__main__":
main()
到目前为止应该只能打开window,但在测试过程中,它不会打开window,尽管没有显示任何错误。
我已经尝试使用“pygame.disply.flip()”而不是更新,但没有任何改变。 我也在终端中得到这个: pygame2.1.2(SDL 2.0.18,Python3.10.1) 来自 pygame 社区的问候。 https://www.pygame.org/contribute.html 就是这样。
import pygame
import random
pygame.init()
class DrawInformation:
BLACK = 0, 0, 0
WHITE = 255, 255, 255
GREEN = 0, 255, 0
RED = 255, 0, 0
GREY = 128, 128, 128
BACKGROUND_COLOR = WHITE
SIDE_PAD = 100
TOP_PAD = 150
def __init__(self, width, height, lst):
self.width = width
self.height = height
self.window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Sorting Algorithm Visualization")
self.set_list(lst)
def set_list(self, lst):
self.list = lst
self.min_val = min(lst)
self.max_val = max(lst)
self.block_width = round((self.width - self.SIDE_PAD) / len(lst))
self.block_height = round((self.height - self.TOP_PAD) / (self.max_val - self.min_val))
self.start_x = self.SIDE_PAD // 2
def generate_starting_list(n, min_val, max_val):
lst = []
for _ in range(n):
val = random.randint(min_val, max_val)
lst.append(val)
return lst
def main():
run = True
clock = pygame.time.Clock()
n = 50
min_val = 0
max_val = 100
lst = generate_starting_list(n, min_val, max_val)
draw_info = DrawInformation(800, 600, lst)
while run:
clock.tick(60)
pygame.display.update()
for event in pygame.event.get():
if event == pygame.QUIT:
run = False
pygame.quit()
if __name__ == "__main__":
main()
这是我目前所知道的...它还说“draw_info”没有被 pylance 和 idk 访问,为什么??
行
if __name__ == "__main__":
main()
的目的是 def main()
所以程序启动的时候不是运行
这是更正后的代码。
import random
pygame.init()
class DrawInformation:
BLACK = 0, 0, 0
WHITE = 255, 255, 255
GREEN = 0, 255, 0
RED = 255, 0, 0
GREY = 128, 128, 128
BACKGROUND_COLOR = WHITE
SIDE_PAD = 100
TOP_PAD = 150
def __init__(self, width, height, lst):
self.width = width
self.height = height
self.window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Sorting Algorithm Visualization")
self.set_list(lst)
def set_list(self, lst):
self.list = lst
self.min_val = min(lst)
self.max_val = max(lst)
self.block_width = round((self.width - self.SIDE_PAD) / len(lst))
self.block_height = round((self.height - self.TOP_PAD) / (self.max_val - self.min_val))
self.start_x = self.SIDE_PAD // 2
def generate_starting_list(n, min_val, max_val):
lst = []
for _ in range(n):
val = random.randint(min_val, max_val)
lst.append(val)
return lst
def main():
run = True
clock = pygame.time.Clock()
n = 50
min_val = 0
max_val = 100
lst = generate_starting_list(n, min_val, max_val)
draw_info = DrawInformation(800, 600, lst)
while run:
clock.tick(60)
pygame.display.update()
for event in pygame.event.get():
if event == pygame.QUIT:
run = False
pygame.quit()
if __name__ == "__main__":
main()