0

好的,所以我正在使用 pygame.draw 在名为 Entity 的类中制作一个简笔画:

class Entity: #Used with default arguments blited on a 600 by 600 pixel screen
    def __init__(self, pos=[300, 300]):
        self.pos = pos
        self.legR = [10, 25]
        self.legL = [-10, -25]
        self.armR = [0, 0]
        self.armL = [0, 0]
        self.body = [30, 5]
        self.head = [0, 0, 5]
        self.size = [60, 110]
        self.color = [0, 0, 0]
        self.image = pygame.surface.Surface(self.size)
        self.image.fill([255, 255, 255])
    def render(self, screen, frame):
        self.image = pygame.surface.Surface(self.size)
        self.image.fill([255, 255, 255])
        pygame.draw.line(self.image, self.color, [self.size[0]/2, self.size[1]/2],
                         [self.size[0]/2+self.legR[0], self.size[0]/2+self.legR[1]], 5)
        pygame.draw.line(self.image, self.color, [self.size[0]/2, self.size[1]/2],
                         [self.size[0]/2+self.legL[0], self.size[0]/2+self.legL[1]], 5)
        pygame.draw.line(self.image, self.color, [self.size[0]/2, self.size[1]/2],
                         [self.size[0]/2+self.body[0], self.size[0]/2+self.body[1]], self.body[1])
        pygame.draw.circle(self.image, self.color,
                           [self.size[0]/2+self.body[0]+self.head[0], self.size[1]/2+self.body[1]+self.head[1]],
                           self.head[2])
        #pygame.draw.line(self.image, self.color, [self.size/2
        screen.blit(self.image, self.pos)

所以我运行它,它给了我这个奇怪的混乱图像,带有一堆随机方向的线条。在我看来,我不太了解这个功能。我可以举一个带有可配置关节的可渲染简笔画的例子吗?如果没有,有人可以请至少告诉我我的致命错误吗?谢谢!

4

2 回答 2

1

我开始根据您的代码编写一个示例。现在它只画了两条腿和他的脊椎:

笔记:

  1. 使用向量而不是元组可以让你做return self.pos+offsetvsreturn (self.pos[0]+offset[0], self.pos[1]+offset[1])
  2. 我使用相对于本地原点的偏移量来绘制。

代码:

import pygame
from pygame.locals import *

pygame.init()
# not normally all global, but simplified demo
color_bg = Color("gray20")    
color_fg = Color("gray80")
clock = pygame.time.Clock()   

screen = pygame.display.set_mode((600,400))

class Entity(): 
    def __init__(self, pos=(300, 300)):
        self.pos = pos
        self.armR = (10, 10)
        self.armL = (-10, 10)
        self.body = (0, -20)
        self.head_offset = self.offset(self.body)

    def offset(self, offset):
        # get offset to draw, relative stickman's hips
        return (self.pos[0]+offset[0], self.pos[1]+offset[1])

    def render(self):        
        b = self.pos
        #pygame.draw.line( screen, color_fg, (10,10), (20,30) )

        o = self.offset( self.armL )
        pygame.draw.line( screen, color_fg, b, o )

        o = self.offset( self.armR )
        pygame.draw.line( screen, color_fg, b, o )

        o = self.offset( self.body )
        pygame.draw.line( screen, Color("red"), b, o )

class Game():
    def __init__(self):
        self.e = Entity()

    def draw(self):                
            screen.fill( color_bg )

            self.e.render()            

            pygame.display.flip()            
            clock.tick(80)

    def loop(self):
        done=False

        while not done:
            events = pygame.event.get()

            for event in events:
                if event.type == pygame.QUIT: done = True
                # event: keydown
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE or event.key == K_SPACE: done = True
                elif event.type == MOUSEMOTION:
                    self.mouse_loc = event.pos

            self.draw()

g = Game()
g.loop()
于 2012-10-14T06:17:07.710 回答
0

我看到的主要问题是,您将实体大小的 x 值用作线上某个点的 y 值:

pygame.draw.line(self.image, self.color, [self.size[0]/2, self.size[1]/2],
                     [self.size[0]/2+self.legR[0], self.size[0]/2+self.legR[1]], 5)

第三个参数中的第二个值应该是:

self.size[1]/2+self.legR[1]

这会让你想要你想要的渲染,但我也会接受猴子的建议并组织一点并将重复的代码划分为函数。

于 2012-10-14T06:36:13.040 回答