如何在pygame中将一行添加为sprite? [英] How do I add a line as a sprite in pygame?

查看:188
本文介绍了如何在pygame中将一行添加为sprite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的关卡中添加一个与地形有关的栅格,而不是与屏幕有关的栅格.我想到的方法是将所有形成网格的线添加为sprites并随地形移动它们,但是我不知道如何将线表示为图像.

I want to add a grid to my level that stays with the terrain and not the screen. The way I thought of doing it is to add all the lines that form the grid as sprites and move them with the terrain, but I can't figure out how to represent the line as an image.

我试图自己做,但是没有成功.

I tried to do this myself, but had no success.

这是我尝试过的

    class Grid():
    def __init__(self):
        self.grid = pygame.Surface(size)
        self.grid.set_colorkey((0,0,0))

    def draw(self):
        # DRAW TILE LINES ----------------------------------------------------------
        grid_x = 0
        grid_y = 0
        for i in range(total_level_width // TILE_SIZE):
            pygame.draw.aaline(self.grid,BLACK,[grid_x,0],[grid_x,total_level_height])
            pygame.draw.aaline(self.grid,BLACK,[0,grid_x],[total_level_width,grid_y])
            grid_x += TILE_SIZE
            grid_y += TILE_SIZE
        # tile test
        pygame.draw.rect(screen,BLACK,(49*TILE_SIZE,34*TILE_SIZE,TILE_SIZE,TILE_SIZE))
        screen.blit(self.grid,(0,0))

创建对象:

grid = Grid()

调用类:(在主程序循环中)

Calling class: (in main program loop)

grid.draw()

推荐答案

在尝试做一个项目时,我遇到了类似的问题.我使用以下代码在表面上划了一条线,然后将其涂抹到屏幕上.希望整个功能对您有所帮助.

I had a similar problem while i was trying to do a project. I used the following code to get a line onto a surface and then bliting it onto my screen. I hope this entire function might help you.

def blitBoundary(self):
    """ helper function to blit boundary on screen """

    # create a surface
    self.boundSurf=pygame.Surface((1024,768))
    self.boundSurf.set_colorkey((0,0,0))

            """
    if not self.boundary.closePoly:
        (x,y)=pygame.mouse.get_pos()
        pointList=self.boundary.pointList +[[x,y]]
    else:
        pointList=self.boundary.pointList"""

    if len(pointList)>1:
        pygame.draw.aalines(self.boundSurf, (255,255,255), 
            self.boundary.closePoly , pointList, 1)

    self.screen.blit(self.boundSurf,(0,0))

我正在尝试绘制多边形.注释掉了if语句,这可能对您没有用. 我所有的线都在一个多边形类对象中. 您可能需要研究pygame.draw.aalines函数.

I was trying to draw a polygon. The commented out the if statement that would be most probably not useful for you. All my lines were in a polygon class object. You might want to look into pygame.draw.aalines function.

这篇关于如何在pygame中将一行添加为sprite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆