Pygame画的不是它应该画的东西

时间:2012-10-14 00:05:15

标签: python drawing pygame

好的,所以我使用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)

所以我运行它,它给了我这个奇怪的混乱图像,随机方向有一堆线。它接触到我,我真的不太了解这个功能。我可以举一个具有可配置关节的可渲染棒图的例子吗?如果没有,有人可以至少告诉我我的致命错误吗?谢谢!

2 个答案:

答案 0 :(得分:1)

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

注意:

  1. 使用向量代替元组可以让您return self.pos+offset vs return (self.pos[0]+offset[0], self.pos[1]+offset[1])
  2. 我使用相对于本地原点的偏移来绘制。
  3. 代码:

    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()
    

答案 1 :(得分: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]

这会让你想要渲染,但我也会采用猴子的建议并整理一下,将重复的代码划分为函数。