Pygame – Access CDROM

Pygame – Access CDROM ”; Previous Next The pygame library has pygame.cdrom module that enables the program to manage playback from audio CDs and DVDs. We need to explicitly initialize this module for its use. >>> import pygame >>> pygame.cdrom.init() The module defines all important CD class to represent the CDROM device. The constructor requires ID of CDROM drive available, starting with 0. >>> obj=pygame.cdrom.CD(0) The CDROM object has access to following useful functions to control the playback. init() initialize a cdrom drive for use quit() uninitialize a cdrom drive for use play() start playing audio stop() stop audio playback pause() temporarily stop audio playback resume() unpause audio playback eject() eject or open the cdrom drive get_busy() true if the drive is playing audio get_paused() true if the drive is paused get_empty() False if a cdrom is in the drive get_numtracks() the number of tracks on the cdrom get_track_audio() true if the cdrom track has audio data get_track_start() start time of a cdrom track get_track_length() length of a cdrom track First, initialize the object. >>> obj.init() To find out how many tracks are present in the current CD − >>> obj.get_numtracks() 8 To start playing the required track, give its number to play() function. >>> obj.play(4) To pause, resume and stop the playback, we can use relevant functions listed above. Print Page Previous Next Advertisements ”;

Pygame – Event objects

Pygame – Event Objects ”; Previous Next All events are instances of pygame.event.EventType class. Pygame identifies following event types − Event Type attributes QUIT None ACTIVEEVENT gain, state KEYDOWN unicode, key, mod KEYUP key, mod MOUSEMOTION pos, rel, buttons MOUSEBUTTONUP pos, button MOUSEBUTTONDOWN pos, button JOYAXISMOTION joy, axis, value JOYBALLMOTION joy, ball, rel JOYHATMOTION joy, hat, value JOYBUTTONUP joy, button JOYBUTTONDOWN joy, button VIDEORESIZE size, w, h VIDEOEXPOSE None USEREVENT Code Print Page Previous Next Advertisements ”;

Pygame – Transforming Images

Pygame – Transforming Images ”; Previous Next The pygame.ransform module contains definitions of a number of functions for manipulation of Surface objects obtained out of image or text blocks. Manipulation of a surface include flipping, rotation, scaling, resizing and zooming the object. Following functions are found in pygame.transform module. flip() flip vertically and horizontally scale() resize to new resolution rotate() rotate an image rotozoom() filtered scale and rotation scale2x() specialized image doubler smoothscale() scale a surface to an arbitrary size smoothly get_smoothscale_backend() return smoothscale filter version in use – ”GENERIC”, ”MMX”, or ”SSE” set_smoothscale_backend() set smoothscale filter version to one of – ”GENERIC”, ”MMX”, or ”SSE” chop() gets a copy of an image with an interior area removed laplacian() find edges in a surface average_surfaces() find the average surface from many surfaces. average_color() finds the average color of a surface threshold() finds which, and how many pixels in a surface are within a threshold of a ”search_color” or a ”search_surf”. Let us first use the flip() function whose syntax is as follows − flip(Surface, xbool, ybool) This function can flip the surface object either horizontally, vertically or both. The orientation is decided by two bool parameters. To flip the image horizontally, use the following command − pygame.transform.flip(img2,True, False) To flip vertically, use the following command − pygame.transform.flip(img2,False, True) In the following example, pygame logo image is displayed normally and flipping in both directions. First obtained flipped surface from original image object, fetch its Rect object and then built it. To render horizontally flipped image, img1 = pygame.image.load(”pygame.png”) img2=img1 img2=pygame.transform.flip(img2,True, False) #inside event loop rect2 = img2.get_rect() rect2.center = 200, 150 screen.blit(img2, rect2) Example The complete code for rendering original Pygame logo and its flipped images is as follows − import pygame pygame.init() screen = pygame.display.set_mode((400, 300)) pygame.display.set_caption(“Flip image”) img1 = pygame.image.load(”pygame.png”) img2=img1 img3=img1 img2=pygame.transform.flip(img2,True, False) img3=pygame.transform.flip(img3, False, True) done = False bg = (127,127,127) while not done: for event in pygame.event.get(): screen.fill(bg) rect1 = img1.get_rect() rect1.center = 200, 50 screen.blit(img1, rect1) rect2 = img2.get_rect() rect2.center = 200, 150 screen.blit(img2, rect2) rect3 = img3.get_rect() rect3.center = 200, 250 screen.blit(img3, rect3) if event.type == pygame.QUIT: done = True pygame.display.update() Output The rotate() function takes following arguments − rotate(Surface, angle) Example Negative value of angle rotates the surface in clockwise direction. import pygame pygame.init() screen = pygame.display.set_mode((400, 300)) pygame.display.set_caption(“rotate image”) img1 = pygame.image.load(”pygame.png”) img2=img1 img3=img1 img2=pygame.transform.rotate(img2,90) img3=pygame.transform.rotate(img3, -90) done = False bg = (127,127,127) while not done: for event in pygame.event.get(): screen.fill(bg) rect1 = img1.get_rect() rect1.center = 200, 50 screen.blit(img1, rect1) rect2 = img2.get_rect() rect2.center = 100, 200 screen.blit(img2, rect2) rect3 = img3.get_rect() rect3.center = 300,200 screen.blit(img3, rect3) if event.type == pygame.QUIT: done = True pygame.display.update() Output Example The laplacian() function extracts outline of the surface object. The function just takes one argument, the image object itself. import pygame pygame.init() screen = pygame.display.set_mode((400, 300)) pygame.display.set_caption(“Laplacian of image”) img1 = pygame.image.load(”pygame.png”) img2=img1 img2=pygame.transform.laplacian(img2) done = False bg = (127,127,127) while not done: for event in pygame.event.get(): screen.fill(bg) rect1 = img1.get_rect() rect1.center = 200, 50 screen.blit(img1, rect1) rect2 = img2.get_rect() rect2.center = 200, 200 screen.blit(img2, rect2) if event.type == pygame.QUIT: done = True pygame.display.update() Output To make the Surface object move along with mouse movement, calculate the x, y coordinates from the center of the image. We also calculate the center-mouse distance d. The atan2(y, x) math function allows to find the rotation angle. We need to transform radians in degrees. From the distance mouse-center we calculate the scale argument. mouse = event.pos Pygame 54 x = mouse[0] – 200 y = mouse[1] – 150 d = math.sqrt(x ** 2 + y ** 2) angle = math.degrees(-math.atan2(y, x)) scale = abs(5 * d / 400) Finally, we use rotzoom() function which performs combined rotation and scaling transform. rotozoom(Surface, angle, scale) Example Following code renders Pygame logo image that can be rotated in accordance with mouse movement. import pygame , math from pygame.locals import * pygame.init() screen = pygame.display.set_mode((400, 300)) pygame.display.set_caption(“Move image with mouse”) img1 = pygame.image.load(”pygame.png”) done = False bg = (127,127,127) while not done: for event in pygame.event.get(): screen.fill(bg) if event.type == pygame.QUIT: done = True if event.type == MOUSEMOTION: mouse = event.pos x = mouse[0] – 200 y = mouse[1] – 150 d = math.sqrt(x ** 2 + y ** 2) angle = math.degrees(-math.atan2(y, x)) scale = abs(5 * d / 400) img2 = pygame.transform.rotozoom(img1, angle, scale) rect = img2.get_rect() rect.center = (200,150) screen.blit(img2, rect) pygame.display.update() Output Run above code, try and move mouse cursor along display window. The image shall rotate and either shrink or grow accordingly. Print Page Previous Next Advertisements ”;

Pygame – Keyboard events

Pygame – Keyboard Events ”; Previous Next Pygame recognizes KEYUP and KEYDOWN events. The pygame.key module defines functions useful for handling keyboard interaction. pygame.KEYDOWN and pygame.KEYUP events are inserted in event queue when the keys are pressed and released. key attribute is an integer ID representing every key on the keyboard. import pygame, sys pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption(“Hello World”) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: key=pygame.key.name(event.key) print (key, “Key is pressed”) if event.type == pygame.KEYUP: key=pygame.key.name(event.key) print (key, “Key is released”) Run the above code and press various keys while the Pygame window is active. Following is a sample output on Python console. q Key is pressed q Key is released right shift Key is released 1 Key is pressed 1 Key is released enter Key is pressed enter Key is released backspace Key is pressed backspace Key is released x Key is pressed x Key is released home Key is pressed home Key is released f1 Key is pressed f1 Key is released left Key is pressed left Key is released right Key is pressed right Key is released up Key is pressed up Key is released down Key is pressed down Key is released As we see, event.key attribute returns a unique identifier associated with each key. The arrow keys left, right, up and down are used very often in a game situation. We can right appropriate logic if a particular key press is detected. Other useful attributes in pygame.key module are listed below − pygame.key.get_pressed get the state of all keyboard buttons pygame.key.get_mods determine which modifier keys are being held pygame.key.set_repeat control how held keys are repeated pygame.key.get_repeat see how held keys are repeated pygame.key.name get the name of a key identifier pygame.key.key_code get the key identifier from a key name pygame.key.start_text_input start handling Unicode text input events pygame.key.stop_text_input stop handling Unicode text input events Print Page Previous Next Advertisements ”;

Pygame – The Sprite Module

Pygame – The Sprite Module ”; Previous Next Any bitmap that is drawn in our game window and that can move around is called a Sprite. The pygame.sprite module contains classes and functionality useful in game development. Along with a Sprite class to create collection of sprite objects, there are functions that enable collision of sprite objects. The Sprite class serves as a base class for different objects in the game. You may have to put one more objects in groups. for that purpose, group classes are also provided. Let us first develop a Sprite class by subclassing the sprite.Sprite class. Each object of this Block class is a rectangular block filled with black color. class Block(pygame.sprite.Sprite): def __init__(self, color, width, height): super().__init__() self.image = pygame.Surface([width, height]) self.image.fill(color) self.rect = self.image.get_rect() We shall create 50 block objects and put them in a list. for i in range(50): block = Block(BLACK, 20, 15) # Set a random location for the block block.rect.x = random.randrange(screen_width) block.rect.y = random.randrange(screen_height) # Add the block to the list of objects block_list.add(block) all_sprites_list.add(block) We create a block with red color call it player, and add it too to the list. # Create a RED player block player = Block(RED, 20, 15) all_sprites_list.add(player) Inside the game’s event loop, detect the collision of red block (player) as it moves along with mouse motion and black block and count the collisions. Example The event loop code is as follows − while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # Clear the screen screen.fill(WHITE) # Get the current mouse position. This returns the position # as a list of two numbers. pos = pygame.mouse.get_pos() # Fetch the x and y out of the list, # just like we”d fetch letters out of a string. # Set the player object to the mouse location player.rect.x = pos[0] player.rect.y = pos[1] # See if the player block has collided with anything. blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True) # Check the list of collisions. for block in blocks_hit_list: score += 1 print(score) # Draw all the spites all_sprites_list.draw(screen) # Go ahead and update the screen with what we”ve drawn. pygame.display.flip() # Limit to 60 frames per second clock.tick(60) pygame.quit() Output Run the above code. Move the player block to capture as many black blocks. The score will be echoed on the console. Print Page Previous Next Advertisements ”;

Pygame – Moving with mouse

Pygame – Moving with Mouse ”; Previous Next Moving an object according to movement of mouse pointer is easy. The pygame.mouse module defines get_pos() method. It returns a two-item tuple corresponding to x and y coordinates of current position of mouse. (mx,my) = pygame.mouse.get_pos() After capturing mx and my positions, the image is rendered with the help of bilt() function on the Surface object at these coordinates. Example Following program continuously renders the given image at moving mouse cursor position. filename = ”pygame.png” import pygame from pygame.locals import * from sys import exit pygame.init() screen = pygame.display.set_mode((400,300)) pygame.display.set_caption(“Moving with mouse”) img = pygame.image.load(filename) x = 0 y= 150 while True: mx,my=pygame.mouse.get_pos() screen.fill((255,255,255)) screen.blit(img, (mx, my)) for event in pygame.event.get(): if event.type == QUIT: exit() pygame.display.update() Print Page Previous Next Advertisements ”;

Pygame – Overview

Pygame – Overview ”; Previous Next Pygame is a popular Python library used for developing video games. It is free, open source and cross-platform wrapper around Simple DirectMedia Library (SDL). Abstraction of SDL functions provided by Pygame makes development of multi-media applications using Python very easy. Originally developed by Peter Shinners, Lenard Lindstrom, René Dudfield and others in Oct 2000, latest version of Pygame is 2.0.1, released in Dec 2020. In addition to SDL functionality, Pygame also provides other features such as vector maths, collision detection, camera and MIDI support etc. Pygame applications can be used on Android based mobile phones also. Environment Setup Easiest way to install Pygame on any machine is by using PIP installer that comes with standard Python distribution. Ensure that you have latest version of pip. It is recommended to install Pygame in a new virtual environment using following command − pip3 install pygame For Raspberri Pi, Pygame is pre-installed in raspbian Linus distribution. As far as installation on MacOS is concerned, newer versions require Pygame 2.0. Prebuilt binary packages are available for many flavours of Linux. They can be installed using respective package manager utilities. For Ubuntu, following is the command − sudo apt-get install pygame For Fedora, the following is the command − sudo yum install pygame It is also possible to compile Pygame’s source code (available at https://github.com/pygame/pygame) and install on desired operating system with the help of respective C/C++ compiler and Python’s setuptools library. To verify if Pygame has been successfully installed, try and import pygame package and check its version. (pygmenv) C:pygmenv>python Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32 Type “help”, “copyright”, “credits” or “license” for more information. >>> import pygame pygame 2.0.1 (SDL 2.0.14, Python 3.7.4) Hello from the pygame community. https://www.pygame.org/contribute.html Pygame library consists of following modules − pygame._sdl2.touch to work with touch input pygame.camera camera use pygame.cdrom audio cdrom control pygame.cursors cursor resources pygame.display to control the display window and screen pygame.draw drawing shapes pygame.event interacting with events and queues pygame.examples module of example programs pygame.fastevent interacting with events and queues from multiple threads. pygame.font loading and rendering fonts pygame.freetype loading and rendering computer fonts pygame.gfxdraw drawing shapes pygame.image image transfer pygame.joystick interacting with joysticks, gamepads, and trackballs. pygame.key to work with the keyboard pygame.locals pygame constants pygame.mask image masks. pygame.math vector classes pygame.midi interacting with midi input and output. pygame.mixer loading and playing sounds pygame.mixer.music controlling streamed audio pygame.mouse to work with the mouse pygame.pixelcopy general pixel array copying pygame.scrap clipboard support. pygame.sndarray accessing sound sample data pygame.sprite basic game object classes pygame.surfarray accessing surface pixel data using array interfaces pygame.tests unit test suite package pygame.time monitoring time pygame.transform to transform surfaces Print Page Previous Next Advertisements ”;

Pygame – Display modes

Pygame – Display Modes ”; Previous Next As in the above example, a display surface is created by set_mode() function defined in pygame.display module. pygame.display.set_mode(size, flags, depth, display, vsync) The size parameter is a tuple of width and height in pixels. If size is not set, the surface will have the size of current resolution. The flags parameter controls the type of display represented by following predefined constants − pygame.FULLSCREEN create a fullscreen display pygame.DOUBLEBUF recommended for HWSURFACE or OPENGL pygame.HWSURFACE hardware accelerated, only in FULLSCREEN pygame.OPENGL create an OpenGL-renderable display pygame.RESIZABLE display window should be sizeable pygame.NOFRAME display window will have no border or controls pygame.SCALED resolution depends on desktop size and scale graphics pygame.SHOWN window is opened in visible mode (default) pygame.HIDDEN window is opened in hidden mode If the vsync parameter is set to 1, it is possible to get a display with vertical sync, but you are not guaranteed to get one. The request only works at all for calls to set_mode() with the pygame.OPENGL or pygame.SCALED flags set. The display index 0 means the default display is used. Depth parameter will default to the best and fastest color depth for the system. For given width and height, Pygame will choose best mode available from list_modes(). >>> print (pygame.display.list_modes()) [(1366, 768), (1360, 768), (1280, 768), (1280, 720), (1024, 768), (800, 600), (640, 480)] pygame.display.mode_ok() This function Pick the best color depth for a display mode. It is used to determine if a requested display mode is available. It will return 0 if the display mode cannot be set. Otherwise it will return a pixel depth that best matches the display asked for. pygame.display.update() This function will update the contents of the entire display. Print Page Previous Next Advertisements ”;

Pygame – Quick Guide

Pygame – Quick Guide ”; Previous Next Pygame – Overview Pygame is a popular Python library used for developing video games. It is free, open source and cross-platform wrapper around Simple DirectMedia Library (SDL). Abstraction of SDL functions provided by Pygame makes development of multi-media applications using Python very easy. Originally developed by Peter Shinners, Lenard Lindstrom, René Dudfield and others in Oct 2000, latest version of Pygame is 2.0.1, released in Dec 2020. In addition to SDL functionality, Pygame also provides other features such as vector maths, collision detection, camera and MIDI support etc. Pygame applications can be used on Android based mobile phones also. Environment Setup Easiest way to install Pygame on any machine is by using PIP installer that comes with standard Python distribution. Ensure that you have latest version of pip. It is recommended to install Pygame in a new virtual environment using following command − pip3 install pygame For Raspberri Pi, Pygame is pre-installed in raspbian Linus distribution. As far as installation on MacOS is concerned, newer versions require Pygame 2.0. Prebuilt binary packages are available for many flavours of Linux. They can be installed using respective package manager utilities. For Ubuntu, following is the command − sudo apt-get install pygame For Fedora, the following is the command − sudo yum install pygame It is also possible to compile Pygame’s source code (available at https://github.com/pygame/pygame) and install on desired operating system with the help of respective C/C++ compiler and Python’s setuptools library. To verify if Pygame has been successfully installed, try and import pygame package and check its version. (pygmenv) C:pygmenv>python Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32 Type “help”, “copyright”, “credits” or “license” for more information. >>> import pygame pygame 2.0.1 (SDL 2.0.14, Python 3.7.4) Hello from the pygame community. https://www.pygame.org/contribute.html Pygame library consists of following modules − pygame._sdl2.touch to work with touch input pygame.camera camera use pygame.cdrom audio cdrom control pygame.cursors cursor resources pygame.display to control the display window and screen pygame.draw drawing shapes pygame.event interacting with events and queues pygame.examples module of example programs pygame.fastevent interacting with events and queues from multiple threads. pygame.font loading and rendering fonts pygame.freetype loading and rendering computer fonts pygame.gfxdraw drawing shapes pygame.image image transfer pygame.joystick interacting with joysticks, gamepads, and trackballs. pygame.key to work with the keyboard pygame.locals pygame constants pygame.mask image masks. pygame.math vector classes pygame.midi interacting with midi input and output. pygame.mixer loading and playing sounds pygame.mixer.music controlling streamed audio pygame.mouse to work with the mouse pygame.pixelcopy general pixel array copying pygame.scrap clipboard support. pygame.sndarray accessing sound sample data pygame.sprite basic game object classes pygame.surfarray accessing surface pixel data using array interfaces pygame.tests unit test suite package pygame.time monitoring time pygame.transform to transform surfaces Pygame – Hello World First step is to import and initialize pygame modules with the help of init() function. import pygame pygame.init() We now set up Pygame display window of preferred size, and give it a caption. screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption(“Hello World”) This will render a game window which needs to be put in an infinite event loop. All event objects generated by user interactions such as mouse movement and click etc. are stored in an event queue. We shall terminate the event loop when pygame.QUIT is intercepted. This event is generated when user clicks the CLOSE button on the title bar. while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() Complete code for displaying Pygame window with Hello World caption is as follows − import pygame, sys pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption(“Hello World”) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() Save above script as hello.py and run to get following output − This window will be closed only if the close (X) button is clicked. Pygame – Display Modes As in the above example, a display surface is created by set_mode() function defined in pygame.display module. pygame.display.set_mode(size, flags, depth, display, vsync) The size parameter is a tuple of width and height in pixels. If size is not set, the surface will have the size of current resolution. The flags parameter controls the type of display represented by following predefined constants − pygame.FULLSCREEN create a fullscreen display pygame.DOUBLEBUF recommended for HWSURFACE or OPENGL pygame.HWSURFACE hardware accelerated, only in FULLSCREEN pygame.OPENGL create an OpenGL-renderable display pygame.RESIZABLE display window should be sizeable pygame.NOFRAME display window will have no border or controls pygame.SCALED resolution depends on desktop size and scale graphics pygame.SHOWN window is opened in visible mode (default) pygame.HIDDEN window is opened in hidden mode If the vsync parameter is set to 1, it is possible to get a display with vertical sync, but you are not guaranteed to get one. The request only works at all for calls to set_mode() with the pygame.OPENGL or pygame.SCALED flags set. The display index 0 means the default display is used. Depth parameter will default to the best and fastest color depth for the system. For given width and height, Pygame will choose best mode available from list_modes(). >>> print (pygame.display.list_modes()) [(1366, 768), (1360, 768), (1280, 768), (1280, 720), (1024, 768), (800, 600), (640, 480)] pygame.display.mode_ok() This function Pick the best color depth for a display mode. It is used to determine if a requested display mode is available. It will return 0 if the display mode cannot be set. Otherwise it will return a pixel depth that best matches the display asked for. pygame.display.update() This function will update the contents of the entire display. Pygame – Locals Module This module contains definitions of various constants used frequently in a Pygame application. Although, these constants are defined in respective modules, it becomes easier to use them from locals module. For example, Key or Mouse events (such as KEYDOWN or MOUSEBUTTONDOWN) are defined as pygame.key.KEYDOWN or pygame.mouse.MOUSEBUTTON respectively, these constants can be used without qualifying the module name by importing from locals module. Here, we are using QUIT event from locals module. import pygame,sys from pygame.locals

Pygame – Sound objects

Pygame – Sound Objects ”; Previous Next Use of music and sounds make any computer game more engaging. Pygame library supports this feature through pygame.mixer module. This module contains Sound class for loading Sound objects and controlling playback. All sound playback is mixed in background threads. For less laggy sound use a smaller buffer size. To obtain Sound object from a sound file or file object, use following constructor − pygame.mixer.Sound(filename or file object) The Sound class defines following methods for controlling playback − play(loops=0, maxtime=0, fade_ms=0) Begin playback of the Sound (i.e., on the computer”s speakers) on an available Channel. Loops parameter is for repeated play. stop() This will stop the playback of this Sound on any active Channels. fadeout(time) This will stop playback of the sound after fading it out over the time argument in milliseconds. set_volume(value) This will set the playback volume for this Sound,immediately affecting the Sound if it is playing and any future playback of this Sound. volume in the range of 0.0 to 1.0 get_length() Return the length of this Sound in seconds. In following example, a text button is rendered at the bottom of display window. A space key fires an arrow upwards accompanied by a sound playing. font = pygame.font.SysFont(“Arial”, 14) text1=font.render(” SHOOT “, True, bg) rect1 = text1.get_rect(midbottom=(200,300)) img=pygame.image.load(“arrow.png”) rect2=img.get_rect(midtop=(200, 270)) Inside the game event loop, for a space key detected, an arrow object is place above the SHOOT button and repeatedly rendered with decrementing y coordinate. The shooting sound is also played at the same time. sound=pygame.mixer.Sound(“sound.wav”)img=pygame.image.load(“arrow.png”) rect2=img.get_rect(midtop=(200, 270)) if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: 18. Pygame — Sound objects print (“space”) if kup==0: screen.blit(img, (190,y)) kup=1 if kup==1: y=y-1 screen.blit(img, (190,y)) sound.play() if y<=0: kup=0 y=265 Example Following listing demonstrates use of Sound object. import pygame from pygame.locals import * pygame.init() screen = pygame.display.set_mode((400, 300)) done = False white = (255,255,255) bg = (127,127,127) sound=pygame.mixer.Sound(“sound.wav”) font = pygame.font.SysFont(“Arial”, 14) text1=font.render(” SHOOT “, True, bg) rect1 = text1.get_rect(midbottom=(200,300)) img=pygame.image.load(“arrow.png”) rect2=img.get_rect(midtop=(200, 270)) kup=0 psmode=True screen = pygame.display.set_mode((400,300)) screen.fill(white) y=265 while not done: for event in pygame.event.get(): screen.blit(text1, rect1) pygame.draw.rect(screen, (255,0,0),rect1,2) if event.type == pygame.QUIT: sound.stop() done = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: print (“space”) if kup==0: screen.blit(img, (190,y)) kup=1 if kup==1: y=y-1 screen.blit(img, (190,y)) sound.play() if y<=0: kup=0 y=265 pygame.display.update() Print Page Previous Next Advertisements ”;