Skip to content

Instantly share code, notes, and snippets.

@stamaniorec
stamaniorec / pygame_init.py
Created October 31, 2015 11:47
#pygame initialize
import pygame
pygame.init()
game_display = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption(GAME_CAPTION)
pygame.quit()
quit()
@stamaniorec
stamaniorec / pygame_event_handling.py
Created October 31, 2015 11:48
#pygame event handling
while is_running:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
is_running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
is_running = False
elif event.key == pygame.K_RIGHT:
@stamaniorec
stamaniorec / pygame_rect_draw.py
Created October 31, 2015 11:48
#pygame draw rect
game_display = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
BLACK = (0,0,0)
pygame.draw.rect(game_display, BLACK, [x, y, width, height])
game_display.fill(BLACK, rect=[x, y, width, height])
@stamaniorec
stamaniorec / pygame_fps.py
Created October 31, 2015 11:48
#pygame cap fps
clock = pygame.time.Clock()
FPS = 60
...
while is_running:
for event in pygame.event.get():
...
...
pygame.display.update()
clock.tick(FPS)
@stamaniorec
stamaniorec / pygame_image.py
Created October 31, 2015 11:49
#pygame load and draw image
import os, sys
def load_image(name):
fullname = os.path.join('data', name)
try:
image = pygame.image.load(fullname)
except pygame.error, message:
print 'Cannot load image:', name
raise SystemExit, message
image = image.convert()
return image
@stamaniorec
stamaniorec / pygame_hide_mouse.py
Created October 31, 2015 11:49
#pygame hide mouse
pygame.mouse.set_visible(0)
@stamaniorec
stamaniorec / pygame_resize_img.py
Created October 31, 2015 11:49
#pygame resize image
image = pygame.transform.scale(image, (desired_width,desired_height))
@stamaniorec
stamaniorec / pygame_background.py
Created October 31, 2015 11:50
#pygame add background
background = pygame.Surface(game_display.get_size()) # (width,height)
background = background.convert()
background.fill((100, 100, 100))
game_display.blit(background, (0,0))
pygame.display.update()
@stamaniorec
stamaniorec / pygame_text.py
Created October 31, 2015 11:51
#pygame draw text
font_size = 36
text_color = (10,10,10)
background_text_color = (255, 255, 255)
background = pygame.Surface(game_display.get_size())
if pygame.font:
font = pygame.font.Font(None, font_size)
text = font.render("Text", 1, text_color, background_text_color) # color and background
textpos = text.get_rect(centerx=background.get_width()/2, centery=background.get_height()/2)
# textpos = (50,50)
background.blit(text, textpos)
@stamaniorec
stamaniorec / pygame_mouse.py
Created October 31, 2015 11:51
#pygame mouse event handling
while is_running:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
print("Left button")
elif event.button == 3:
print("Right button")
# 4, 5, 6 are scroll events, not sure about the details
elif event.type == MOUSEBUTTONUP: