'''
Created on 21. sep. 2013
Page 136 in ze almighty python book, 4.3
@author: Christian
'''
import sys,pygame,time
pygame.init()
numLevels = 15 # Number of levels
unitSize = 25 # Height of one level
white = (255,255,255) # RGB Value of White
black = (0,0,0) # RGB Value of Black
size = unitSize * (numLevels + 1)
xPos = size /2.0 # Constant position of X value
screenSize = size,size # Screen size to accomodate pygame
screen = pygame.display.set_mode(screenSize)
for level in range(numLevels):
yPos = (level + 1) * unitSize
width = (level +1) * unitSize
block = pygame.draw.rect(screen,white,(0,0,width,unitSize),0)
block.move(xPos,yPos)
pygame.time.wait(100)
pygame.display.flip()block.move(xPos,yPos)应该能工作,但由于一些奇怪的原因,它不起作用。我不知道为什么。我相当肯定,其他一切都很好,我已经搜索了几个小时后,才来到这个网站寻求帮助。
发布于 2013-10-14 21:11:40
从文档中可以看出,draw.rect在构造函数中使用Rect,而不是元组:
block = pygame.draw.rect(screen, white, Rect(0, 0, width, unitSize), 0)移动返回的Rect不会神奇地再次绘制块。要再次绘制该块,您需要再次绘制该块:
block.move(xPos,yPos)
block = pygame.draw.rect(screen, white, block, 0)当然,您现在的屏幕上有两个街区,因为您已经绘制了两次。既然你想移动这个街区,为什么要在原来的位置画它呢?为什么不直接指定您想要的位置呢?
block = pygame.draw.rect(screen, white, Rect(xPos, yPos, width, unitSize), 0)有了更多关于你想要做的事情的信息,也许可以构造出一个更好的答案。
发布于 2013-10-14 23:14:43
我不清楚您的代码试图完成什么(而且我也不重新理解图书引用),所以这只是猜测。它首先构造一个Rect对象,然后在每次循环迭代之前对其进行增量重新定位和大小调整(充气)。
注意move_ip()和inflate_ip()的使用会改变Rect对象的“就位”,这意味着它们修改了Rect对象的特性,而不是返回一个新的特征,但是不要(重新)绘制它(也不要返回任何内容)。这比为每次迭代创建一个新的Rect所使用的资源更少。
import sys, pygame, time
pygame.init()
numLevels = 15 # Number of levels
unitSize = 25 # Height of one level
white = (255, 255, 255) # RGB Value of White
black = (0, 0, 0) # RGB Value of Black
size = unitSize * (numLevels+1)
xPos = size / 2.0 # Constant position of X value
screenSize = size, size # Screen size to accomodate pygame
screen = pygame.display.set_mode(screenSize)
block = pygame.Rect(0, 0, unitSize, unitSize)
for level in range(numLevels):
block.move_ip(0, unitSize)
block.inflate_ip(0, unitSize)
pygame.draw.rect(screen, white, block, 0)
pygame.time.wait(100)
pygame.display.flip()https://stackoverflow.com/questions/19369112
复制相似问题