Example: bankruptcy

Pygame Zero Documentation - Read the Docs

Pygame zero DocumentationRelease PopeJan 03, 2022 Contents1 to Pygame zero .. from Scratch ..72 Hooks .. Objects ..223 User Pygame zero .. Pygame zero in IDLE and other IDEs .. and Resources .. libraries like Pygame zero ..574 Stickers Stickers ..595 Improving Pygame .. of Pygame zero .. to Pygame zero ..63 Index67iiiPygame zero Documentation , Release zero is for creating games without is intended for use in education, so that teachers can teach basic programming without needing to explain the PygameAPI or write an event zero Documentation , Release Introduction to Pygame Creating a windowFirst, create an empty file that this runs and creates a blank window by runningpgzrun in Pygame zero is optional; a blank file is a valid Pygame zero script!

Pygame Zero Documentation, Release 1.2 Your project should now look like this:. images/ alien.png alien_hurt.png sounds/ eep.wav intro.py sounds/is the standard directory that Pygame Zero will look in to find your sound files.

Tags:

  Zero, Pygame, Pygame zero

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Pygame Zero Documentation - Read the Docs

1 Pygame zero DocumentationRelease PopeJan 03, 2022 Contents1 to Pygame zero .. from Scratch ..72 Hooks .. Objects ..223 User Pygame zero .. Pygame zero in IDLE and other IDEs .. and Resources .. libraries like Pygame zero ..574 Stickers Stickers ..595 Improving Pygame .. of Pygame zero .. to Pygame zero ..63 Index67iiiPygame zero Documentation , Release zero is for creating games without is intended for use in education, so that teachers can teach basic programming without needing to explain the PygameAPI or write an event zero Documentation , Release Introduction to Pygame Creating a windowFirst, create an empty file that this runs and creates a blank window by runningpgzrun in Pygame zero is optional; a blank file is a valid Pygame zero script!

2 You can quit the game by clicking on the window s close button or by pressingCtrl-Q(-Qon Mac). If the gamestops responding for any reason, you may need to terminate it by pressingCtrl-Cin your Terminal Drawing a backgroundNext, let s add adraw()function and set window dimensions. Pygame zero will call this function whenever it needsto paint the , add the following:1 WIDTH = 3002 HEIGHT = 30034defdraw() ((128, 0, 0))Re-runpgzrun the screen should now be a reddish square!What is this code doing?WIDTHandHEIGHT control the width and height of your window. The code sets the window size to be 300 pixels ineach zero Documentation , Release a built-in that represents the window display.

3 It has arange of methods for drawing sprites and ()method call is filling the screen with a solid colour, specified as a(red, green, blue)colour tuple.(128, 0, 0)will be a medium-dark red. Try changing these values with numbers between 0 and 255and see what colors you can create. You can also use a string to name a built-in colour s set up a sprite that we can Draw a spriteBefore we can draw anything, we ll need to save an alien sprite to use. You can right click on this one and save it( Save Image As.. or similar).(This sprite has a transparency (or alpha ) channel, which is great for games! But it s designed for a dark background,so you may not be able to see the alien s space helmet until it is shown in the game).

4 Tip:You can find lots of free sprites, including this one, on This one comes from the Platformer ArtDeluxe need to save the file in the right place so that Pygame zero can find it. Create a directory calledimagesand savethe image into it Both of those must be lower case. Pygame zero will complain otherwise, to alertyou to a potential cross-platform compatibility you ve done that, your project should look like this:. the standard directory that Pygame zero will look in to find your s a built-in class calledActorthat you can use to represent a graphic to be drawn to the s define one now. Change to read:1alien = Actor('alien') = 100, 5634 WIDTH = 5005 HEIGHT = + 2067defdraw() () ()Your alien should now be appearing on screen!

5 By passing the string'alien'to theActorclass, it automaticallyloads the sprite, and has attributes like positioning and dimensions. This allows us to set theHEIGHTof the windowbased on the height of the ()method draws the sprite to the screen at its current 1. CoursesPygame zero Documentation , Release Moving the alienLet s set the alien off-screen; change to = 0, 10 Note how you can assign totoprightto move the alien actor by its top-right corner. If the right-hand edge of thealien is at0, the alien is just offscreen to the left. Now let s make it move. Add the following code to the bottom ofthe file:defupdate() += > = 0 Pygame zero will call yourupdate()function once every frame.

6 Moving the alien a small number of pixels everyframe will cause it to slide across the screen. Once it slides off the right-hand side of the screen, we reset it back to functionsdraw()andupdate()work in similar ways but are designed for two different purposes. Thedraw()function draws the current position of the alien while theupdate()function is used to show the alienmoving on the Handling clicksLet s make the game do something when you click on the alien. To do this we need to define a function calledon_mouse_down(). Add this to the source code:1defon_mouse_down(pos) (pos):3print("Eek!)

7 ")4else:5print("You missed me!")You should run the game and try clicking on and off the zero is smart about how it calls your functions. If you don t define your function to take aposparameter, Pygame zero will call it without a position. There s also abuttonparameter foron_mouse_down. So we couldhave written:defon_mouse_down():print("You clicked!")or:defon_mouse_down(pos, button):ifbutton == (pos):print("Eek!") Sounds and imagesNow let s make the alien appear hurt. Save these files: - save this theimagesdirectory. - create a directory calledsoundsand save this that Introduction to Pygame Zero5 Pygame zero Documentation , Release project should now look like this.

8 The standard directory that Pygame zero will look in to find your sound let s change theon_mouse_downfunction to use these new resources:defon_mouse_down(pos) (pos) = 'alien_hurt' ()Now when you click on the alien, you should hear a sound, and the sprite will change to an unhappy s a bug in this game though; the alien doesn t ever change back to a happy alien (but the sound will play on eachclick). Let s fix this ClockIf you re familiar with Python outside of games programming, you might know ()method thatinserts a delay. You might be tempted to write code like this:1defon_mouse_down(pos) (pos) = 'alien_hurt' () (1) = 'alien'Unfortunately, this is not at all suitable for use in a ()blocks all activity; we want the game togo on running and animating.

9 In fact we need to return fromon_mouse_down, and let the game work out when toreset the alien as part of its normal processing, all the while running yourdraw()andupdate() is not difficult with Pygame zero , because it has a built-inClockthat can schedule functions to be called , let s refactor (ie. re-organise the code). We can create functions to set the alien as hurt and also to change itback to normal:1defon_mouse_down(pos) (pos):3set_alien_hurt()456defset_alien_h urt() = 'alien_hurt' ()91011defset_alien_normal() = 'alien'6 Chapter 1. CoursesPygame zero Documentation , Release is not going to do anything different ()won t be called.

10 But let s changeset_alien_hurt()to use the clock, so that theset_alien_normal()will be called a little while () = 'alien_hurt' () (set_alien_normal, ) ()will causeset_alien_normal()to be called ()also prevents the same function being scheduled more than once, such as if you click it, and you ll see the alien revert to normal after second. Try clicking rapidly and verify that the alien doesn trevert until second after the last ()accepts both integers and float numbers for the time interval. In the tutorial we areusing a float number to show this but feel free to use both to see the difference and effects the different values SummaryWe ve seen how to load and draw sprites, play sounds, handle input events, and use the built-in might like to expand the game to keep score, or make the alien move more are lots more features built in to make Pygame zero easy to use.


Related search queries