pygame drawing polygons issue,1名玩家vs 100名玩家

时间:2017-10-22 17:35:47

标签: python pygame

所以我有一个问题,pygame没有绘制Player对象的多边形。

当我有一个单独的播放器,如p = Player(...),或者player.append(播放器(...))只有一次。会吸引玩家。但是当我尝试在列表中有2个或更多玩家时,屏幕上没有画画。我知道他们现在的设置方式都是重叠的,因此看起来只有1个玩家,但我看不到:

LIB / Player.py

class player:
    center_pos = [0, 0]
    points = [[-3, 3], [3, 3], [0, -10]]
    screen = pygame = color = None

    def __init__(self, start, screen, pygame, color):
        self.center_pos = start

        self.points[0] = [self.points[0][0] + start[0], self.points[0][1] + start[1]]
        self.points[1] = [self.points[1][0] + start[0], self.points[1][1] + start[1]]
        self.points[2] = [self.points[2][0] + start[0], self.points[2][1] + start[1]]

        self.screen = screen
        self.pygame = pygame
        self.color = color

    def update(self):
        self.pygame.draw.polygon(self.screen, self.color, self.points, 0)

main.py

import sys, pygame, math

pygame.init()

FPS = 60

clock = pygame.time.Clock()

screen = pygame.display.set_mode((640, 640))

from lib.Player import Player

players = []

for i in range(0, 100):
    players.append(Player([width/2, height-24], screen, pygame, BLUE))

while 1:
    clock.tick(FPS)
    screen.fill((0, 0, 0))
    for player in players:
        player.update()
    pygame.display.flip()

1 个答案:

答案 0 :(得分:1)

当你创建一个类时,所有变量都不是self.共享的所有实例... 包括 points(你为每个类增加,因此你&#39 ;如果有最多8个左右的物体被绘制,它们会逐渐移动(取​​决于你的宽度和高度值,我每个都使用100个)。

class player:
    center_pos = [0, 0] # Shared!
    points = [[-3, 3], [3, 3], [0, -10]] # Shared!
    screen = pygame = color = None

    def __init__(self, start, screen, pygame, color):
        self.center_pos = start

        self.points[0] = [self.points[0][0] + start[0], self.points[0][1] + start[1]]
        self.points[1] = [self.points[1][0] + start[0], self.points[1][1] + start[1]]
        self.points[2] = [self.points[2][0] + start[0], self.points[2][1] + start[1]]
        ...

试试这个:

class player:
    screen = pygame = None

    def __init__(self, start, screen, pygame, color):
        self.center_pos = start
        self.points = [[-3, 3], [3, 3], [0, -10]]

        self.points[0] = [self.points[0][0] + start[0], self.points[0][1] + start[1]]
        self.points[1] = [self.points[1][0] + start[0], self.points[1][1] + start[1]]
        self.points[2] = [self.points[2][0] + start[0], self.points[2][1] + start[1]]

        self.screen = screen
        self.pygame = pygame
        self.color = color

    def update(self):
        self.pygame.draw.polygon(self.screen, self.color, self.points, 0)