Rect.move() 不移动矩形 [英] Rect.move() Does not move the rect

查看:62
本文介绍了Rect.move() 不移动矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    '''
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) 应该可以工作,但由于某些奇怪的原因它不起作用.我不知道为什么.我相当确定其他一切都运行良好,在来到本网站寻求帮助之前,我已经在互联网上搜索了几个小时.

The block.move(xPos,yPos) should work, but it does not for some odd reason. I have no idea why. I am fairly certain that everything else is working just fine, I have searched the interwebs for hours before coming on this site to ask for help.

推荐答案

从文档看来,draw.rect 在其构造函数中采用了 Rect,而不是元组:

From the documentation it seems draw.rect takes a Rect in its constructor, not a tuple:

block = pygame.draw.rect(screen, white, Rect(0, 0, width, unitSize), 0)

移动返回的 Rect 不会再次神奇地绘制块.要再次绘制块,您需要再次绘制块:

Moving the returned Rect doesn't magically draw the block again. To draw the block again, you will need to draw the block again:

block.move(xPos,yPos)
block = pygame.draw.rect(screen, white, block, 0)

当然,您现在屏幕上有两个块,因为您已经绘制了两次.既然你无论如何都想移动方块,那为什么首先要在旧位置绘制它呢?为什么不直接指定您希望它开始的位置?

Of course you now have two blocks on your screen, because you have drawn twice. Since you want to move the block anyway, why draw it at the old location in the first place? Why not just specify the location you want it at to begin with?

block = pygame.draw.rect(screen, white, Rect(xPos, yPos, width, unitSize), 0)

有了更多关于您要做什么的信息,或许可以构建更好的答案.

With more information on what you're trying to do, perhaps a better answer can be constructed.

这篇关于Rect.move() 不移动矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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