Posted February 06, 2025 by gempocket
#pygame #tutorial #cheatsheet #python #animation #gravity #collision
Note: The first item of a python list is numbered/indexed as 0.
---------------------------------------------------------------------------------------------------
SPRITE CLASS DEFINITION AND GROUP UPDATE
class GameSprite(pygame.sprite.Sprite):
def __init__(self, size): <-- init variable (size)
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((size, size))
self.rect = self.image.get_rect()
def update(self, action): <-- update method is declared with a parameter
if action == "value":
---------------------------------------------------------------------------------------------------
spritegroup = pygame.sprite.Group
spritegroup.add(GameSprite(size)) <-- init variable (size)
spritegroup.update(action) <-- executing update with an argument on all added sprites
spritegroup.draw(screen)
---------------------------------------------------------------------------------------------------
MOVEMENT AND COLLISION CHECKS AGAINST GROUP
gravity_list = [0,0,1,2,4,7,10,15]
if gravity_index < (len(gravity_list)-1):
gravity_index += 1
self.rect.y += gravity_list[gravity_index]
if pygame.sprite.spritecollide(self, group, False):
while pygame.sprite.spritecollide(self, group, False):
self.rect.y -= 1
---------------------------------------------------------------------------------------------------
ANIMATION CYCLE - MILLISECOND TIMING
animation_image_list = [pygame.image.load("1.png").convert_alpha(), pygame.image.load("2.png").convert_alpha()]
if (pygame.time.get_ticks() - animation_start_time) > animation_time_limit:
animation_start_time = pygame.time.get_ticks()
animation_index += 1
if animation_index == len(animation_image_list):
animation_index = 0
self.image = animation_image_list[animation_index]
---------------------------------------------------------------------------------------------------
FONT - SYSTEM OR LOCAL .ttf FILE
pygame.font.init()
font_system = pygame.font.SysFont("arial", 25)
font_local = pygame.font.Font("cool_font.ttf", 25)
text = "Hello world."
rendered_text = font_system.render(text, True, (255, 255, 255)) #True anti-aliasing
self.image.blit(rendered_text, (0,0))
---------------------------------------------------------------------------------------------------
KEYBOARD INPUT SIGNALS - TWO TYPES
1.
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a: #single pulse signal
if event.type == pygame.KEYUP:
if event.key == pygame.K_a: #single pulse signal
2.
keys = pygame.key.get_pressed()
if keys[pygame.K_a]: #continuous signal
if not keys[pygame.K_a]: #continuous signal
---------------------------------------------------------------------------------------------------
MOUSE BUTTON PRESS - BOOLEAN
button_1, button_2, button_3 = pygame.mouse.get_pressed(num_buttons=3)
mouse_x, mouse_y = pygame.mouse.get_pos()
mouse_surface.rect.x = mouse_x
mouse_surface.rect.y = mouse_y
if button_1:
if pygame.sprite.spritecollide(mouse_surface, units, False):
pass
---------------------------------------------------------------------------------------------------
MOUSE SCROLL
from pygame.locals import *
for event in events:
if event.type == MOUSEWHEEL:
if event.y == 1:
players[reference].map.update("mouse_wheel_up")
if event.y == -1:
players[reference].map.update("mouse_wheel_down")
---------------------------------------------------------------------------------------------------
dictionary[pythonclass].spritegroup #DATA STRUCTURE
---------------------------------------------------------------------------------------------------
OTHER
rect.inflate_ip(-value,-value) #alter hitbox size : right, bottom
clock.tick(48) #pygame at 48 FPS
sprite.remove(group) #remove sprite from its group, do collision check on its own group
group.add(sprite) #add it back after the collision check
mouse_focused = pygame.mouse.get_focused()
if mouse_focused == 0: #mouse outside the screen (web)