Python Hunting: A beginner's guide to programming and game building in Python for teens, tweens and newbies. by Brian Carling

Python Hunting: A beginner's guide to programming and game building in Python for teens, tweens and newbies. by Brian Carling

Author:Brian Carling [Carling, Brian]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2017-04-10T07:00:00+00:00


get_width() solves this problem. It measures the width of score at any point in time and we can use this value to position the score correctly.

We could have used the get_width function in other places. For example, in the space invader game we added a missile to the missiles list with the following line:

missiles.append(Missile(self.x+50))

That (self.x+50) is the position of the middle of the fighter. We happen to know the fighter is 100 pixels wide. But if we couldn't be bothered looking it up, or if we had several fighters of different widths, we could have written:

missiles.append(Missile(self.x+fighter_image.get_width()/2))

Similarly, when setting the y-position of the fighter we wrote:

screen.blit(fighter_image,(self.x,591))

But we had to work out that 591 by looking up the height of the screen and taking away the height of the fighter. We could have let Python do the maths with:

screen.blit(fighter_image,(self.x, screen.get_height()-fighter_image.get_height()))

The line we just wrote to blit the right hand score could look like this:

screen.blit(txt,(screen.get_width()-20-txt.get_width(),20))



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.