简体   繁体   English

Pygame没有画

[英]Pygame doesn't draw

I need help with a program I'm making. 我需要有关正在制作的程序的帮助。 It's a version of Conway's Game of Life. 这是Conway的《人生游戏》的一个版本。 This game is right now made out of 3 files: main.py, cellBoard.py, cell.py 该游戏现在由3个文件组成:main.py,cellBoard.py,cell.py

main.py takes care to instance cellboard and make it update its data, give it mouse input, and tell it to draw itself (an instance of the pygame surface is given to it, which handles it to the cells which are the actual ones that draw themselves) main.py会小心地实例化单元板,并使其更新其数据,向其提供鼠标输入,并告诉其自行绘制(已为其提供pygame表面的实例,该实例将其处理为实际的单元格)画自己)

cellboard.py creates a list of cells based off their size and the screen's size, to fill it properly. cellboard.py根据单元格的大小和屏幕的大小创建一个单元格列表,以正确填充它。 It's a 2D list. 这是2D列表。 When it creates the cells it sets their state (alive currently) and handles them an instance of its instance of the original surface instance. 创建单元时,它会设置其状态(当前处于活动状态)并为它们处理原始表面实例的实例。

cell.py contains all the things a cell can do: die, live, be toggled, be drawn. cell.py包含单元格可以执行的所有操作:死亡,存活,切换,绘制。

In fact, when I need to draw the whole board I just call cellBoard's own draw() and it should take care of calling each cell's draw. 实际上,当我需要绘制整个电路板时,我只调用cellBoard自己的draw(),它应该注意调用每个单元格的绘制。 And it does. 确实如此。 The execution gets to the point where the cell should be drawn (checked with prints) and the pixel filling function is executed (using a for loop to cover an area). 执行到应该绘制单元格的位置(用打印检查)并执行像素填充功能(使用for循环覆盖区域)。 But nothing is actually drawn to the screen, or at least nothing is visible. 但是实际上什么都没有画到屏幕上,或者至少看不到任何东西。

I have no idea what is causing this. 我不知道是什么原因造成的。 I checked the code multiple times, I've even rewritten the whole program from scratch to make it more tidy (and I had the same problem as now) 我多次检查了代码,甚至从头开始重写了整个程序,以使其更简洁(而且我现在遇到了同样的问题)

What is causing this? 是什么原因造成的? My idea would be that somehow the instance of surface Cell gets is not good anymore to work because something happened to it (it goes through cellboard before getting to each cell, could that be the problem?) 我的想法是,表面Cell实例以某种方式变得不好了,因为它发生了某些事情(它到达了每个单元之前都要经过单元板,这可能是问题吗?)

Here's the source code (all 3 files, they are very short and barebones so they should be easy to read) http://dl.dropbox.com/u/2951174/src.zip 这是源代码(全部3个文件,它们都很短且准系统,因此应该易于阅读) http://dl.dropbox.com/u/2951174/src.zip

Thanks in advance to anyone who feels like helping. 在此先感谢任何想要帮助的人。 I need to complete this project very fast so your help would be greatly appreciated. 我需要非常快地完成这个项目,因此将非常感谢您的帮助。

First of a quick suggestion: 首先快速建议:
People are much more likely to help you if they don't have to download a zip file, next time just post the code parts you suspect not to work. 如果人们不必下载zip文件,则人们更有可能为您提供帮助,下一次,只需发布​​您怀疑不起作用的代码部分。

Anyways, problem seems to be in your main loop: 无论如何,问题似乎出在您的主循环中:

#Keyboard events
events = pygame.event.get()
for event in events:
    if event.type == pygame.QUIT:
        running = 0

#Mouse events
#todo

#Grid update  <------- here you update the grid and the cells are being drawn
cb.draw()

#Graphical output    <------------ here you're filling the WHOLE screen with white
screen.fill(THECOLORS["white"])    

pygame.display.flip()

You need to move your screen.fill call above cb.draw so you don't paint over the cells. 您需要将您的screen.fill以上调用cb.draw让你不用油漆过的细胞。

Also in cell.py your drawing code is A) Broken and B) bad. 同样在cell.py您的绘制代码是A)损坏和B)损坏。

Instead of setting every pixel on its own, which is slow and in it's current state doesn't draw the cells correctly, you can just as well draw rectangle: 与其单独设置每个像素(这很慢并且在当前状态下不能正确绘制单元格),您还可以绘制矩形:

pygame.draw.rect(self.surface, (100, 10, 10), (self.pos[0], self.pos[1], self.size, self.size))

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

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