Learning How To Use Pygame
Learning How To Use Pygame
Learning How To Use Pygame
pygame.init()
then I will set the window of the program and I will do this by setting up
the width and length of the window. It is stored in a variable usually
called screen
screen = pygame.display.set_mode((width,length))
u replace the width with the number you want to set the width to(unit is
pixels though u don’t need to add it) then u set the length’s number.
screen = pygame.display.set_mode((800,400))
When we run what we initially wrote. A pygame will open for a while
and close. Well the window does not open for a long time because
python will terminate the code after executing the last line. To prevent
this we must use a while True loop to allow it to run continuously
Code
while True:
#draw all our elements and update everything
for event in pygame.event.get():
when u run this code now the window runs continuously but there is a
problem. We can’t close the window so now we must write code to
implement the close button in the window control box
if event.type == pygame.QUIT:
pygame.quit()
this will implement a functioning close button. Now we encounter
another problem. When we close the window, we get an error. This is
because the pygame.quit() is ending the program which is the opposite
of pygame.init() which is initializing the program. To solve this problem
we must import a function from the system library which is exit
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
while True:
#draw all our elements and update everything
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
finally at the end of the while loop always ensure it ends with
pygame.display.update()
this ensures that the window can accept user input which will update in
the program. To prevent yourself from forgetting to write this important
code write it first after typing while True: and write the rest of the code
in between.
test_surface.fill('Red')
the numbers determine the width and length of the display surface. You
enter the colour in the parenthesis of fill.If you want the colour to fill the
screen then make the numbers the same as the screen variable.
NB: the Surface after the pygame. Should always start with a capital ‘S’
and start the colour you enter with a capital letter
Now to determine the positioning of the display surface just in case the
surface is not the same size as the screen. You type this
screen.blit(test_surface,(0,0))
the numbers in the parenthesis act like x and y on the Cartesian plane.
But here the plane acts a little different. So on the original Cartesian
plane the point 0,0 starts at the bottom left corner while in python this
point is at the top left corner.
Now we are coming to set the pace of the game.
clock = pygame.time.Clock()
clock.tick(60)
Below I will show how the code is supposed to look and the positioning
of the various commands
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption('adventure run')
clock = pygame.time.Clock()
test_surface = pygame.Surface((100,200))
test_surface.fill('Red')
while True:
#draw all our elements and update everything
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(test_surface,(0,0))
pygame.display.update()
clock.tick(60)
We can display text on the display surface. But before we can do that we
have to determine what type of font style we want to use and import that
file
test_font=pygame.font.Font(r'C:\Users\ALEXANDRA\OneDrive\Desktop\
pythonProject\mygame\pixletype.ttf',50)
Next we are determine the positioning of the text on the display surface
screen.blit(text_surface,(300,50))
Now we are going to implement one of the enemies in the game which is
the snail.
snail_surface=pygame.image.load(r'C:\Users\ALEXANDRA\OneDrive\Desktop\
pythonProject\mygame\snail1 (3).png')
now we just use the screen.blit command and enter the coordinates the
snail will just be static on the screen. But we want it to move. The code
we use is this
snail_x_pos = 600
Below is how the code is supposed to look and the positioning of the
various commands
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption('adventure run')
clock = pygame.time.Clock()
test_font = pygame.font.Font(r'C:\Users\ALEXANDRA\OneDrive\Desktop\
pythonProject\mygame\pixletype.ttf',50)
sky_surface = pygame.image.load(r'C:\Users\ALEXANDRA\OneDrive\Desktop\
pythonProject\mygame\Sky.png')
ground_surface = pygame.image.load(r'C:\Users\ALEXANDRA\OneDrive\Desktop\
pythonProject\mygame\ground.png')
text_surface = test_font.render("my game",False,'Pink')
snail_surface = pygame.image.load(r'C:\Users\ALEXANDRA\OneDrive\Desktop\
pythonProject\mygame\snail1 (3).png')
snail_x_pos = 600
while True:
#draw all our elements and update everything
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(sky_surface,(0,0))
screen.blit(ground_surface,(0,300))
screen.blit(text_surface,(300,50))
snail_x_pos -= 3
if snail_x_pos < -100: snail_x_pos = 800
screen.blit(snail_surface,(snail_x_pos,250))
pygame.display.update()
clock.tick(60)
it uses the same principle with the first number being the x coordinate
and the second being the y coordinate. It is important to use 300 as the y
coordinate because it is at this point that the floor picture is
now that we are using rectangles for the snail picture we have to change
the code so that the snail can move and reappear at the other side when it
leaves the screen
snail_rectangle.right -= 3
if snail_rectangle.left < -100: snail_rectangle.left = 800
screen.blit(snail_surface,snail_rectangle)
we are going to import the player image and use a rectangle to determine
its positioning
pygame.image.load('player_walk_1.png')
player_rectangle = player_surface.get_rect(midbottom = (80,300))
screen.blit(player_surface,player_rectangle)
.convert() or .convert_alpha()
This method converts imported images to a suitable format for pygame.
Use .convert() on the first image used as the display surface and
use .convert_alpha() when another image will be placed on top of
another image
sky_surface = pygame.image.load('Sky.png').convert()
ground_surface = pygame.image.load('ground.png').convert()
snail_surface =
pygame.image.load('snail1(3).png').convert_alpha()
player_surface=pygame.image.load('player_walk_1.png').convert_alp
ha()
Below is how the code is supposed to look and the positioning of the
various commands
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption('adventure run')
clock = pygame.time.Clock()
test_font = pygame.font.Font('pixletype.ttf',50)
sky_surface = pygame.image.load('Sky.png').convert()
ground_surface = pygame.image.load('ground.png').convert()
text_surface = test_font.render("my game",False,'Pink')
snail_surface = pygame.image.load('snail1
(3).png').convert_alpha()
snail_rectangle = snail_surface.get_rect(midbottom = (600,300))
player_surface =
pygame.image.load('player_walk_1.png').convert_alpha()
snail_rectangle.right -= 3
if snail_rectangle.left < -100: snail_rectangle.left = 800
screen.blit(snail_surface,snail_rectangle)
#player_rectangle.left += 1
screen.blit(player_surface,player_rectangle)
pygame.display.update()
clock.tick(60)
so if you press any mouse button the phrase button down is outputted.
You can also determine if the mouse button is released after being
pressed
if event.type == pygame.MOUSEBUTTONUP:
print(‘button up’)
if you do not press the mouse button at all. Nothing is outputted but if
you press and release it,the moment you release it ‘button up’ is
outputted
we learnt earlier how to detect if the mouse pointer collides with the
player. Here is another method using the event type
if event.type == pygame.MOUSEMOTION:
if player_rectangle.collidepoint(event.pos):
print('collision')
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption('adventure run')
clock = pygame.time.Clock()
test_font = pygame.font.Font('pixletype.ttf', 50)
sky_surface = pygame.image.load('Sky.png').convert()
ground_surface = pygame.image.load('ground.png').convert()
text_surface = test_font.render("my game", False, 'Pink')
snail_surface = pygame.image.load('snail1
(3).png').convert_alpha()
snail_rectangle = snail_surface.get_rect(midbottom=(600, 300))
player_surface =
pygame.image.load('player_walk_1.png').convert_alpha()
# player_rectangle = pygame.Rect(left,top,width,height)
player_rectangle = player_surface.get_rect(midbottom=(80, 300))
while True:
# draw all our elements and update everything
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEMOTION:
if player_rectangle.collidepoint(event.pos):
print('collision')
snail_rectangle.right -= 3
if snail_rectangle.left < -100:
snail_rectangle.left = 800
screen.blit(snail_surface, snail_rectangle)
# player_rectangle.left += 1
screen.blit(player_surface, player_rectangle)
# if player_rectangle.colliderect(snail_rectangle):
#print('collision')
#mouse_position = pygame.mouse.get_pos()
#if player_rectangle.collidepoint(mouse_position):
# print('collision')
# print(pygame.mouse.get_pressed())
pygame.display.update()
clock.tick(60)
#pygame.draw.line(screen,'pink',(0,0),pygame.mouse.get_pos(),20)