AlmostĪll the logic for the game goes into these two classes. Here we create two classes to represent the objects in our game. Then we simply return the loaded Sound object.
#Sprite monkey gif full#
First we create a full path to the sound image, and This function is similar to the image loading function, but handles someĭifferent problems. This will act enough like a normal Sound object for this game to run without If not, it returns a small class instance that has a dummy play method. The first thing this functionĭoes is check to see if the pygame.mixer pygame module for loading and playing sounds module was imported correctly. Next is the function to load a sound file. get_init (): return NoneSound () fullname = os. In this case theįunction will lookup the color at the topleft pixel of the image, and useĭef load_sound ( name ): class NoneSound : def play ( self ): pass if not pg. You can also pass a value of -1 as the colorkey. This would usually just be a color RGB value, like (255, 255, 255) for If the user supplied an argumentįor the colorkey argument we use that value as the colorkey for the image. To scaleīy a scalar, we can get the size and scale the x and y by the scalar. This function takes a Surface and the size it should be scaled to. We then scale the image, using the () resize to new resolution function. Image to the screen will happen as quickly as possible.
![sprite monkey gif sprite monkey gif](https://i.pinimg.com/originals/82/83/c7/8283c7b7b68f765e2b3bf46fe9c3682f.gif)
Its color format and depth to match the display. This makes a new copy of a Surface and converts Next we load the image using the () load new image from a file (or file-like object) function.Īfter the image is loaded, we make an importantĬall to the convert() function. The os.path.join function, a pathname will be created that works for whatever In this example all the resources are in a "data" subdirectory. The first thing this function does is create a full pathname to the file. A colorkey is used in graphics to represent a color of the Takes an argument it can use to set a colorkey for the image, and an argument This function takes the name of an image to load. convert () if colorkey is not None : if colorkey = - 1 : colorkey = image. get_size () size = ( size * scale, size * scale ) image = pg. Look at each function individually in this section.ĭef load_image ( name, colorkey = None, scale = 1 ): fullname = os. Here we have two functions we can use to load images and sounds. It then prepares the variable data_dir to tell the Main_dir uses the os.path module and the _file_ variable providedīy Python to locate the game's python file, and extract the folder from
#Sprite monkey gif code#
Lastly, we prepare two paths for the rest of the code to use.
![sprite monkey gif sprite monkey gif](https://pa1.narvii.com/6403/8caa27410175319c3f02a5f0038a543991164487_hq.gif)
(Although they will only be unavailable in very uncommon situations). Mixer modules in pygame are not available. Some pygame modules are optional, and if they aren't found, Pygame as pg, so that all of the functionality of pygame is able to In the next line, we import the pygame package. Us to do things like create platform independent file paths. join ( main_dir, "data" )įirst, we import the standard "os" python module. mixer : print ( "Warning, sound disabled" ) main_dir = os. font : print ( "Warning, fonts disabled" ) if not pg. # Import Modules import os import pygame as pg if not pg.