Creating Games in Python

Installing Pygame Zero

This module is meant for beginner game programmers. It is the simpler version of the pygame module.

pip install pygame
pip install pgzero

Simple Test

def draw():
    screen.draw.text('Hello', topleft = (10, 10))

pgzrun hello.py

Shooting Game

Create a folder named "shooting game".
Under this folder, create a Python file named "shoot.py".
Also create a folder named "images" to store the images.







Download the apple image and keep it in the "images" folder.

from random import randint
#Create a new actor called apple
#Actors are like Sprites in Scratch
apple = Actor('apple')
score = 0
def draw():
    screen.clear()
    apple.draw()
#Placing an apple at a specific position
def place_apple():
    apple.x = randint(10, 500)
    apple.y = randint(10, 400)
#Dealing with clicks
def on_mouse_down(pos):
    if apple.collidepoint(pos):
        global score
        score += 1
        print('Good shot!')
        print('Score =', score)
        place_apple()
    else:
        print('You missed!')
        quit()
place_apple()


Comments