2012-10-14 62 views
0

好了,所以我用pygame.draw使叫做實體類簡筆畫:pygame的平局不拉絲它應該

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) 

於是我運行這個,這讓我這個奇怪搞砸圖像隨機附帶一束線。它接近我,我不太瞭解它的功能。我可以請一個具有可配置關節的渲染棒圖的例子嗎?如果沒有,請問有人能告訴我我致命的錯誤嗎?謝謝!

回答

1

我開始寫一個基於你的代碼的例子。現在它只是平2支腳和他的脊椎:

注:

  1. 使用的載體,而不是元組將讓你做return self.pos+offset VS return (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() 
+0

你可以給我身體其他部分的例子嗎? –

0

我看到的主要問題是,您使用的是實體上的線點之一的y值大小的x值:

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] 

Ť帽子會讓你想要渲染,但我也會採取猴子的建議,並組織一點,並將重複的代碼劃分成函數。

+0

'self.size [0]/2'可以簡化爲'self.width/2',這取決於你是否要考慮self.topleft,還有很多其他的**虛擬屬性**要使用:(見段落後的頁首)http://www.pygame.org/docs/ref/rect.html – ninMonkey

+0

@monkey他的類不是從Surface派生出來的,所以它沒有width或topleft屬性。在他的類定義下,需要self.size [1]/2。 – kevintodisco