Pygame Notes
Pygame Notes
imported.
then you should initiate pygame by pygame.init()
you can create a window by pygame.display.set_mode((x,y))
you can give it a title by pygame.display.set_caption("string")
you can make a clock object to set the fps of the game by pygame.time.Clock() and
name it clock or anything you like.
import image by pygame.load.image().convert_ALPHA() to make it be easy to
manipulate by pygame.
to create a font, use pygame.font.Font(the font, size) and then to create a text
surface we can use pygame.font.Font(the font, size).render("your text", False,
color)
to create a surface, use pygame.Surface((the x,y))
to get a rectangle for a surface, use a surface like s.get_rect(center= ,topleft=()
and etc)
in the game loop, for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
exit()
if you want to draw a surface on the screen, you can use the blit funciton that
takes two arguments, first the screen or the main window surface and then a
rectangle that is already defined with its location, or you can just manually
define a location for your surface on the screen.
how to accept mouse position: two ways
1- in the event loop, you can do:
for event in pygame.event.get():
if pygame.type==MOUSEMOTION:
print(event.pos)
2-This one should be outside the main loop, pygame.mouse.getpos() which return the
current location of the mouse.
how to find out if buttons of the mouse is getting pressed also two ways:
1- in the event loop, we can go:
if event.type==pygame.MOUSEBUTTONDOWN:
print("Button pressed")
if event.type==pygame.MOUSEBUTTONUP:
print("Button released")
2- by using pygame.mouse.getpressed() which return 3 booleans, each one
corresponding to one mouse button.
how to detect collisions:
by using rect.colliderect(rect1) we can find out if rect has collided with rect1.
by using rect.collidepoint((x,y)) we can find out if rect has collide with the
point.
to change the dimension of an image we can use
pygame.transform.scale((width,height)).
timers:
create a user event that is triggered in certain time intervals.
first creat a timer by pygame.USEREVERNT + 1
and then to set how often this timer is triggered we can use
pygame.time.set_timer(the event, how often you want it to trigger in miliseconds)
then in your event loop: you can do if (event.type==pygame.USEREVENT + 1(your
desired event))
the sprite class:
a class that contain the surface and a rectangle.
To make a sprite class, to sth like class Player(pygame.sprite.Sprite) and the
constructor is def __init__(self,it can have more arguments)
and the first line in the constructor is super().__init()
the constructor needs at least two attributes, self.image and self.rect.
to create an instance of the class use player=Player(). To draw the sprites, first
place it in Group or GroupSingle and then that group can draw or update all sprites
in that group.
Group is a group that can contain multiple sprites but GroupSingle is a group with
only one sprite(used for player).When we are checking collison, they should be from
different groups.
To creat a group, you can do sth like:
player=pygame.sprite.GroupSingle()
player.add(Player())-->will place the Player instance inside a GroupSingle.
Then you can say in your game loop, player.draw(screen) to draw the sprites, player
here is the name of the GroupSingle.
Every sprite class should have an update method, that is then called in the main
method.
to remove a sprite, you can implement a method def destroy(self):
if condition:
self.kill()
remember the update method in the sprite class is the most important method, since
it is called in the mail loop, so any functionality that you want your sprite to
have, you have to call them inside the update method, so that whenever it is
called, everything about the sprite gets updated.
adding spritecollison:
the method pygame.sprite.spritecollide(player.sprite(player is the group),group(the
group that you want to check the collision of the player with),bool(if true, if the
collision happens, the sprite that collided with player will be deleted)) which
returns a list of collided sprites.
if you want to empty a Group that contains sprites, use groups name.empty()
add music:
to import music, use pygame.mixer.Sound(sound path)
to play that soumd, use sound.play()
you can set volume by sound'name.set_volume(1 for loudest and 0 for mute)
if you want to loop a music you can give the play(loops=-1#if you want to loop
infinitely or give it a number to loop it that number of times)