import pygame # pygame library - in console type : pip install pygame
import os # needed to get path to images
from PIL import Image # Import the IMage library from Pillow. Used to get image data (e.g. width). To install pip install pillow
import random # random number code

# Get Path to current file - ugh but does work cross OS
mypath = os.path.dirname( os.path.realpath( __file__ ) )

#initialse pygame - in tutorial but not apparently needed
#pygame.init()

#create the screen
screenX = 800
screenY = 600
screen = pygame.display.set_mode((screenX,screenY))
# You can determine screen size with pygame.display.get_surface().get_width() or .get_height()

# background  
background = 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 search
pygame.display.set_icon(icon)

#player
playerImg = 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 data
playerW = playerImgData.size[1] # get player size data; 2 is height
playerX = screenX / 2 - playerW / 2 #400 is half windows - half of image (64)
playerY = screenY - 120
playerX_change = 0 # variable to hold player movement

#enemy
enemyImg = 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 sorry
enemyY = 50
enemyX_change = 1
enemyY_change = 40 

def player( x, y ):
    screen.blit( playerImg, (x, y) ) # blit basically means to draw https://en.wikipedia.org/wiki/Bit_blit

def enemy( x, y ):
    screen.blit( enemyImg, (x, y) )

# game loop
running = True
while running:
    for event in pygame.event.get(): # get all pygame events and loop through them
        if event.type == pygame.QUIT:
            running = False

        # if key stroke is pressed, check if right or left arrow
        if event.type == pygame.KEYDOWN: # Was a key pressed down (KEYUP is releasing)
            if event.key == pygame.K_LEFT: # Left arrow key
                playerX_change = -1 # Tell player to mover left slightly
            if event.key == pygame.K_RIGHT: # Right arrow key
                playerX_change = 1 # tell player to move right slightly
    
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0 # stop moving the player    
    
    screen.fill((0,0,0)) # RGB - Red, Green, Blue: needed to clear the screen each frame, without old images are left behind

    # Draw background
    screen.blit(background, (0, 0) ) # really slows the game drawing down, so need to bump up the player and enemy movement speeds

    # Player movement
    playerX += playerX_change # apply any player change to the playerX coordinate

    if playerX <= 0: # if the player tries to move off left edge
        playerX = 0
    if playerX >= screenX - playerW: # if the player tries to mover past right edge - player width
        playerX = screenX - playerW

    # enemy movement
    enemyX += enemyX_change # apply any enemy change to the enemyX coordinate

    if enemyX <= 0 or enemyX >= screenX - enemyW: # if the enemy tries to move off any edge
        enemyX_change *= -1 # flip the change value
        enemyY += enemyY_change

    # draw
    player( playerX, playerY ) # player drawn over screen
    enemy( enemyX, enemyY ) # player drawn over screen
    pygame.display.update() # needed to apply the change


Last modified: Thursday, 29 October 2020, 3:29 PM