Skip to content

Instantly share code, notes, and snippets.

@horstjens
Created January 22, 2026 15:24
Show Gist options
  • Select an option

  • Save horstjens/c1085a1804ab46cc40d5105b4f17a0b1 to your computer and use it in GitHub Desktop.

Select an option

Save horstjens/c1085a1804ab46cc40d5105b4f17a0b1 to your computer and use it in GitHub Desktop.
santa pygame
import pygame
import os
import random
pygame.init()
#pygame.mixer.music.load("")
#pygame.mixer.music.play(-1)
SCREEN_X = 1024
SCREEN_Y = 800
SCREENRECT = pygame.Rect(0, 0, SCREEN_X, SCREEN_Y)
GRAVITY = 70.81
screen = pygame.display.set_mode(SCREENRECT.size) #, winstyle, bestdepth)
#print("")
#schlittenimage = pygame.image.load("schlitten.png")
#schlitten2 = pygame.transform.scale(schlittenimage, (100,100))
# alle Walk bilder laden
santa = {
"walk": [],
"dead": [],
"idle": [],
"jump": [],
"run": [],
}
flocken = []
for _ in range(1000):
flocken.append([random.randint(0, SCREEN_X),
random.randint(0, SCREEN_Y)])
geschenkimage = pygame.image.load("geschenk1.png")
geschenkimage.convert_alpha()
geschenkimage = pygame.transform.scale(geschenkimage, (30, 30))
haus1 = pygame.image.load("haus1.png")
haus1 = pygame.transform.scale(haus1, (160, 160))
haus2 = pygame.image.load("haus2.png")
haus2 = pygame.transform.scale(haus2, (160, 160))
haus3 = pygame.image.load("haus3.png")
haus3 = pygame.transform.scale(haus3, (160, 160))
#for root, dirs, files in os.walk("."):
# for f in files:
# if f.startswith("Walk") and f.endswith(".png"):
for a in range(1, 14):
#print(a)
img = pygame.image.load(f"Walk ({a}).png")
img = pygame.transform.scale(img, (200, 200))
santa["walk"].append(img)
for a in range(1, 18):
#print(a)
img = pygame.image.load(f"Dead ({a}).png")
img = pygame.transform.scale(img, (200, 200))
santa["dead"].append(img)
for a in range(1, 17):
#print(a)
img = pygame.image.load(f"Idle ({a}).png")
img = pygame.transform.scale(img, (200, 200))
santa["idle"].append(img)
for a in range(1, 17):
#print(a)
img = pygame.image.load(f"Jump ({a}).png")
img = pygame.transform.scale(img, (200, 200))
santa["jump"].append(img)
for a in range(1, 12):
#print(a)
img = pygame.image.load(f"Run ({a}).png")
img = pygame.transform.scale(img, (200, 200))
santa["run"].append(img)
modus = "idle"
n = len(santa["walk"])
i = 0
dx = 1
hx = 0
santaimage = santa["walk"][i]
sx, sy = 0, SCREEN_Y - 200
blick = 1 # rechts
zeit = 0
running = True
clock = pygame.time.Clock()
# sprite groups
allgroup = pygame.sprite.Group()
geschenkgroup = pygame.sprite.Group()
hausgroup = pygame.sprite.Group()
class Haus(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__(self.groups)
self.image = random.choice((haus1, haus2, haus3))
self.rect = self.image.get_rect()
if self.image == haus1:
self.kamin_x = self.rect.width * 0.33
elif self.image == haus2:
self.kamin_x = self.rect.width * 0.6
elif self.image == haus3:
self.kamin_x = self.rect.width * 0.5
self.kamin_x -= self.rect.width / 2
self.x = x
self.y = y
self.rect.center = [self.x, self.y]
self.dx = -80.0
def update(self, delta_time):
self.altes_x = self.x
self.x += self.dx * delta_time / 1000
if (self.altes_x > SCREEN_X) and (self.x < SCREEN_X):
Haus(SCREEN_X + self.rect.width, SCREEN_Y - 160 / 2)
self.rect.center = [self.x, self.y]
if self.x + self.rect.width < 0:
#Haus(hx, SCREEN_Y-160/2)
self.kill()
class Geschenk(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__(self.groups)
self.image = geschenkimage
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.dy = random.uniform(-0.5, -1.0)
self.dx = -dx * random.uniform(1.0, 1.5) # links/rechts
def update(self, delta_time):
self.dy += GRAVITY * (delta_time / 1000) * (delta_time / 1000)
self.y += self.dy
self.x += self.dx
self.rect.center = (self.x, self.y)
if self.y > SCREEN_Y + 10:
self.kill()
Geschenk.groups = allgroup, geschenkgroup
Haus.groups = allgroup, hausgroup
#Geschenk(100,300)
#Geschenk(200,350)
hx = 0
#for h in range(10):
while hx < (SCREEN_X + 100):
Haus(hx, SCREEN_Y - 160 / 2)
hx += 160
while running:
milliseconds = clock.tick(60)
zeit += milliseconds / 1000
if zeit > 1 / 10:
i += 1
if i >= n:
i = 0
santaimage = santa[modus][i]
if blick == -1:
santaimage = pygame.transform.flip(santaimage, True, False)
zeit = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_j:
modus = "jump"
n = len(santa[modus])
i = 0
if event.type == pygame.KEYDOWN: # taste wurde gedrückt
if event.key == pygame.K_SPACE:
Geschenk(sx + 100, sy + 80)
if event.key == pygame.K_ESCAPE:
running = False
# modus wechseln
if event.key == pygame.K_g:
modus = "walk"
n = len(santa[modus])
i = 0
if event.key == pygame.K_r:
modus = "run"
n = len(santa[modus])
i = 0
if event.key == pygame.K_k:
modus = "dead"
n = len(santa[modus])
i = 0
if event.key == pygame.K_i:
modus = "idle"
n = len(santa[modus])
i = 0
dx, dy = 0, 0 # delta
# tasten gedrückt ?
pressed = pygame.key.get_pressed()
modus = "idle"
if pressed[pygame.K_a]:
dx = -1
blick = -1 # schaut nach links
if pressed[pygame.K_d]:
dx = 1
blick = 1 # schaut nach rechts
if pressed[pygame.K_w]:
dy = -1
if pressed[pygame.K_s]:
dy = 1
if (dx != 0) or (dy != 0):
if pressed[pygame.K_LSHIFT]:
modus = "run"
dx *= 2
dy *= 2
else:
modus = "walk"
n = len(santa[modus])
sx += dx
sy += dy
# Bildschirm
screen.fill((0, 0, 0)) #schwarz, gesamten Bildschirm löschen
# schneeflocken zeichnen
for x, y in flocken:
pygame.draw.circle(screen, (255, 255, 255), (x, y), 1, )
# sprites updaten
allgroup.update(milliseconds)
# collision detection
for mein_haus in hausgroup:
collidegroup = pygame.sprite.spritecollide(mein_haus,
geschenkgroup,
False,
pygame.sprite.collide_rect)
for mein_geschenk in collidegroup:
if mein_geschenk.y < mein_haus.y:
if (mein_haus.x + mein_haus.kamin_x - 20) < mein_geschenk.x < (mein_haus.x + mein_haus.kamin_x + 20):
mein_geschenk.kill()
# sprites zeichnen
allgroup.draw(screen)
# santa zeichnen
screen.blit(santaimage, (sx, sy))
pygame.display.flip()
pygame.display.set_caption(modus)
# schneeflocken bewegen
for nr, (x, y) in enumerate(flocken):
flocken[nr] = [x, y + random.randint(1, 4)]
if flocken[nr][1] > SCREEN_Y:
flocken[nr][1] = 0
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment