Background code
Completion requirements
#import pygame # pygame library - in console type : pip install pygameimport os # needed to get path to imagesfrom PIL import Image # Import the IMage library from Pillow. Used to get image data (e.g. width). To install pip install pillowimport random # random number code# Get Path to current file - ugh but does work cross OSmypath = os.path.dirname( os.path.realpath( __file__ ) )#initialse pygame - in tutorial but not apparently needed#pygame.init()#create the screenscreenX = 800screenY = 600screen = pygame.display.set_mode((screenX,screenY))# You can determine screen size with pygame.display.get_surface().get_width() or .get_height()# backgroundbackground = pygame.image.load( os.path.join( mypath , "images\\background.png") )# Title and Icon (icon from flaticon.com)pygame.display.set_caption("Space Invaders")icon = pygame.image.load( os.path.join( mypath , "images\\spaceship16.png") ) # images from flaticon or opengameart or even google image searchpygame.display.set_icon(icon)#playerplayerImg = pygame.image.load( os.path.join( mypath , "images\\player.png") )playerImgData = Image.open( os.path.join( mypath , "images\\player.png") ) # use Pillows to load the image for image dataplayerW = playerImgData.size[1] # get player size data; 2 is heightplayerX = screenX / 2 - playerW / 2 #400 is half windows - half of image (64)playerY = screenY - 120playerX_change = 0 # variable to hold player movement#enemyenemyImg = pygame.image.load( os.path.join( mypath , "images\\enemy.png") )enemyImgData = Image.open( os.path.join( mypath , "images\\enemy.png") )enemyW = enemyImgData.size[1]enemyX = random.randint(0, int(screenX - enemyW) ) # prob dont need to convccert to int but safe than sorryenemyY = 50enemyX_change = 1enemyY_change = 40def player( x, y ):screen.blit( playerImg, (x, y) ) # blit basically means to draw https://en.wikipedia.org/wiki/Bit_blitdef enemy( x, y ):screen.blit( enemyImg, (x, y) )# game looprunning = Truewhile running:for event in pygame.event.get(): # get all pygame events and loop through themif event.type == pygame.QUIT:running = False# if key stroke is pressed, check if right or left arrowif event.type == pygame.KEYDOWN: # Was a key pressed down (KEYUP is releasing)if event.key == pygame.K_LEFT: # Left arrow keyplayerX_change = -1 # Tell player to mover left slightlyif event.key == pygame.K_RIGHT: # Right arrow keyplayerX_change = 1 # tell player to move right slightlyif event.type == pygame.KEYUP:if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:playerX_change = 0 # stop moving the playerscreen.fill((0,0,0)) # RGB - Red, Green, Blue: needed to clear the screen each frame, without old images are left behind# Draw backgroundscreen.blit(background, (0, 0) ) # really slows the game drawing down, so need to bump up the player and enemy movement speeds# Player movementplayerX += playerX_change # apply any player change to the playerX coordinateif playerX <= 0: # if the player tries to move off left edgeplayerX = 0if playerX >= screenX - playerW: # if the player tries to mover past right edge - player widthplayerX = screenX - playerW# enemy movementenemyX += enemyX_change # apply any enemy change to the enemyX coordinateif enemyX <= 0 or enemyX >= screenX - enemyW: # if the enemy tries to move off any edgeenemyX_change *= -1 # flip the change valueenemyY += enemyY_change# drawplayer( playerX, playerY ) # player drawn over screenenemy( enemyX, enemyY ) # player drawn over screenpygame.display.update() # needed to apply the change
Last modified: Tāite, 29 Oketopa 2020, 3:29 PM