如何在pygame中使用列表同时绘制多条线?

2024-05-14 10:08:53 发布

您现在位置:Python中文网/ 问答频道 /正文

最近,我尝试在屏幕上同时绘制多行,使用一个列表来创建每个对象,并将这些数据存储在列表中。但是,每当我运行代码时,都会收到一条错误消息:

TypeError: an integer is required (got type tuple)

有谁能帮我解决这个问题吗? 非常感谢你。在

import pygame
import random
import sys

Height = 800
Width = 800
running = True

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

class Wire(object):

    def __init__(self, wire_pos_start, wire_pos_end, wire_width):
        self.wire_pos_start = wire_pos_start
        self.wire_pos_end = wire_pos_end
        self.wire_width = wire_width

    def draw(self):
        pygame.draw.circle(display, WHITE, self.wire_pos_start, self.wire_pos_end, self.wire_width)

    def update(self):
        pass


wires = []

for i in range(10):
    x = random.randint(0, 800)
    y = random.randint(0, 800)
    wire = Wire((400, 400), (x, y), 1)
    wires.append(wire)



pygame.init()
display = pygame.display.set_mode((Height, Width))
clock = pygame.time.Clock()
pygame.display.set_caption("Game")

while running:
    clock.tick(60)
    display.fill(BLACK)

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

    # Update
    #for wire in wires:
    #    wire.draw()
    wire.draw()
    # Draw

    pygame.display.update()

pygame.quit()

Tags: posimportselffordefdisplayrandomwidth

热门问题