The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

This topic is READ ONLY
ZhangXunqi (8) [Avatar] Offline
#1
Hi! Mr.Sande,or other friends,I need your help.Does my questions are too difficult to work out?Why not say something?0 reply makes me really sad!
Ok,still chapter 17:I let example 17.2 have a little change,because I can't understand it clearly.I try to know every variable's change rule.So you can see I nearly print every variable every loop when the ball runs(To think easily,I only play one ball)I think it can help me know hiw does the code run.Here is the code:
Maybe you can't see the indent.Here please regard '~' as a blank.
from random import *
class MyBallClass(pygame.sprite.Sprite):
~~~~def __init__(self,image_file,location,speed):
~~~~~~~~ pygame.sprite.Sprite.__init__(self)
~~~~~~~~self.image=pygame.image.load(image_file)
~~~~~~~~self.rect=self.image.get_rect()
~~~~~~~~self.rect.left,self.rect.top=location
~~~~~~~~self.speed=speed
~~~~def move(self):
~~~~~~~~print "##",self.rect
~~~~~~~~self.rect=self.rect.move(self.speed) #NOTICE HERE!!
~~~~~~~~print "##",self.rect
~~~~~~~~if self.rect.left<0 or self.rect.right>width:
~~~~~~~~self.speed[0]=-self.speed[0]
~~~~~~~~if self.rect.top<0 or self.rect.bottom>height:
~~~~~~~~self.speed[1]=-self.speed[1]
size=width,height=640,480
screen=pygame.display.set_mode(size)
screen.fill([255,255,255])
location=[10,10]
img_file="beach_ball.png"
speed=[choice([-2,2]),choice([-2,2])]
ball=MyBallClass(img_file,location,speed)
screen.blit(ball.image,location)
pygame.display.flip()

while True:
~~~~for event in pygame.event.get():
~~~~~~~~if event.type==pygame.QUIT:
~~~~~~~~~~sys.exit()
~~~~pygame.time.delay(20)
~~~~screen.fill([255,255,255])
~~~~ball.move()
~~~~screen.blit(ball.image,ball.rect)
~~~~pygame.display.flip()
~~~~print "========================="
~~~~print ball.rect
~~~~print ball.rect.left
~~~~print ball.rect.right
~~~~print ball.rect.bottom
~~~~print ball.rect.top
~~~~print speed[0]
~~~~print speed[1]


Did you see "NOTICE HERE" ---self.rect=self.rect.move(self.speed)?I find self.rect changed from there.How does the code do it?I think this line must be the soul of this code?
Dear Mr. Sanda.Could you please give your opinions?