如何在pygame中将一行添加为sprite?

编程入门 行业动态 更新时间:2024-10-28 20:17:25
本文介绍了如何在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?

本文发布于:2023-11-15 17:40:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596583.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   如何在   pygame   sprite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!