Pygame zero 4: Alien Fall

Adding some lines of code to the game

Let’s give a little of more code to our very simple game, making the alien want to catch the other aliens falling from the sky… instead of landing on the one o the ground.

# python filename.py

import pgzrun
from random import randint

WIDTH = 800
HEIGHT = 600
alien_hurt = Actor('alien', midbottom=(WIDTH // 2, HEIGHT))
alien = Actor('alien_hurt')
alien.topright = 100, 10
xpos = randint(0, 800)
score = 0
lor = randint(0, 1)
somewhere = randint(0, 0)


def draw():
    screen.fill('blue')
    alien.draw()
    alien_hurt.draw()


def update():
    global score, somewhere
    alien.y += 5 + somewhere
    alien.x += somewhere
    if alien_hurt.x > 30:
        if keyboard.left:
            alien_hurt.x -= 5
    if alien_hurt.x < 770:
        if keyboard.right:
            alien_hurt.x += 5
    if alien_hurt.colliderect(alien):
        score += 1
        respawn()
    if alien.y > 600:
        score -= 1
        respawn()
    if alien.x < 30 or alien.x > 770:
        somewhere = -somewhere


def respawn():
    global somewhere
    print(score)
    alien.y = 0
    xpos = randint(0, 800)
    alien.x = xpos
    somewhere = randint(-3, 3)

pgzrun.go()

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.