Simple Pycodes

Simple Pycodes

Pygame library built on top of the SDL library which designed for game developing on high level languages. but pygame holds more abilities, pygame written in Assembly, C, Python.

in this tutorial we will get familiar with pygame by developing simple classic game.

 

of course its not possible to cover all aspects of the library or game developing in single game, but its a good introduction on the subject.

so lets start :)

before we start coding let me show you what we are going to have when we complete the code.

 

Im using python3 and before you start this tutorial make sure that you have pygame installed on your system, for linux users you need to specify your python version when you use pip install and it will be something like:

python3 -m pip install pygame

for windows users if you have only on version of python installed on your system you would need to use pip install in cmd:

python -m pip install pygame

-----------------------------------------------------------------------------------------------------------------------------------------

First thing we need to import the library itself by:

import pygame

 

Color's format in pygame

 

In pygame colors are represented in RGB, so we can use the numbers directly in the code or at the beginning define them as constants.

 

COLOR = (RED,GREEN,BLUE  each color have a range from 0 to 255.

for example red = (255,0,0) blue = (0,0,255) and green = (0,255,0), by this way later on you dont need to write your colors by their numbers over and over, you will use the variable instead.

 

now we will have 3 more constants :

width and height for screen and fps for frame per second, with fram per second we can keep running different parts of the game with the same speed in the main game loop.

 

lets create the screen by:

screen = pygame.display.set_mode((width, height)) we can pass width and heigh as list datat type like [width, height] or as a tuple, not much of difference there.

screen is just a variable and it can be anything like s, window, even jack :D but as a general rule try to avoid one letter variables and come up with meaning full variables which can make your code more understandable for yourself and others.

 

 

 

now we have our screen but there is no loop to keep it up and running. so we need to make one.

in most of the game loops in pygame we need to take care of couple of things.

 

events

game updates

drawings

reloading the screen

 

in the game loop first of all we need to make sure all processes are running in a same speed, thats why we need to use clock.tick(fps).

next we have to make pygame listen to events, if you are familiar with turtle module you will find it similar to turtle.listen().

in pygame we need to loop through all events, so if we can manage Keyboard, mouse or any input.

for event in pygame.event.get():

    if event.type == pygame.QUIT:

        running = False

by this simple loop we can close the screen by click on close button.

after event handling we need to take care of the rest of sections.

now that we have the structure, lets create some classes using sprite in pygame, sprite gives us a lot of functionality and control over objects in pygame.

 

We will have two classes one for ball and another for player. In original code which I have in github I have 3 classes and the extra one was for powerups which are actually increase and decrease the length of the paddle, but in this tutorial we focus only on ball and player class.

 

 

now we need to activate or acces to rect method.

self.rect = self.image.get_rect()

 

self.rect.center = (WIDTH/2, HEIGHT /4 - 30)

and we set the position of the object using the line above.

so the basic form of our class will looks like this now:

 

 

in pygame when we work with sprite, we can use sprite.Group() which is actually container class, it will gives us many useful toolls and controls over single sprites or groups of sprites, for example sprite.update() and sprite.draw() are some of the useful tolls available there.

before we create an object from Class Ball lets create a an object from countaier class.

we can call it what ever we want but its best to use something meaningful and fit to our code!

all_objects = pygame.sprite.Group()

and now we can create an object from class Ball by ball = ball()

and the we add  the object to the countainer by all_objects.add(ball)

and now even we dont have nothing to update we can use all_objects.update() and all_objects.draw() in side the game loop.

 

 

now we create another class for player and add it to the container as well, so process will be the same with two different attribs.

from this point you can follow the video tutorials in my youtube channel.

Bouncing ball

Part 1

Bouncing ball

Part 2

Bouncing ball

Part 3

Bouncing ball

Part 4

Bouncing ball

Part 5

Bouncing ball

Part 6