简体   繁体   English

pygame平局没有画什么

[英]Pygame draw not drawing what it should

Ok, so I am using pygame.draw to make a stick figure in the class called Entity: 好的,所以我正在使用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)

So I run this and it gives me this weird messed up image with a bunch of lines in random directions. 所以我运行了它,它给了我这个奇怪的,混乱的图像,在随机方向上有一堆线。 It seams to me I do not really understand the function well. 对我来说,它并不是一个很好的功能。 Could I please have a example of a render able stick figure with configurable joints? 我可以举一个带有可配置关节的可渲染棒图的示例吗? If not, could someone please at least tell me my fatal error? 如果不是,请问至少有人告诉我我的致命错误吗? Thanks! 谢谢!

I started writing an example based off your code. 我开始根据您的代码编写示例。 For now it just draws 2 legs and his spine: 现在,它只画两条腿和他的脊椎:

Note: 注意:

  1. Using vectors instead of tuples would let you do return self.pos+offset vs return (self.pos[0]+offset[0], self.pos[1]+offset[1]) 使用向量代替元组可以让您return self.pos+offsetreturn (self.pos[0]+offset[0], self.pos[1]+offset[1])
  2. I use offsets relative a local origin to draw. 我使用相对于本地原点的偏移量进行绘制。

code: 码:

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

The main problem I see is that you're using the x-value of your entity's size for the y-value of one of the points on your lines: 我看到的主要问题是,您将实体大小的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)

The second value in the third argument should be: 第三个参数中的第二个值应该是:

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

That will get you want you want rendered, but I would also takes monkey's advice and organize a bit and compartmentalize repeated code into functions. 那会让您想要渲染,但是我也会听取猴子的建议并进行一些整理,并将重复的代码划分为功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM