Last active
January 29, 2026 15:56
-
-
Save horstjens/c1085a1804ab46cc40d5105b4f17a0b1 to your computer and use it in GitHub Desktop.
santa pygame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | |
| sackimage = pygame.image.load("sack.png") | |
| sackimage.convert_alpha() | |
| sackimage = pygame.transform.scale(sackimage, (90,90)) | |
| 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 / 4 | |
| blick = 1 # rechts | |
| zeit = 0 | |
| running = True | |
| score = 0 | |
| geschenke = 0 | |
| ladezeit = 0.5 # 1/10 sekunde | |
| lückenzeit = 0.0 # zeit die vergangen ist seit dem letzten Geschenkabwurf | |
| clock = pygame.time.Clock() | |
| # sprite groups | |
| allgroup = pygame.sprite.Group() | |
| geschenkgroup = pygame.sprite.Group() | |
| hausgroup = pygame.sprite.Group() | |
| textgroup = pygame.sprite.Group() | |
| sackgroup = pygame.sprite.Group() | |
| santagroup = pygame.sprite.GroupSingle() | |
| def write(msg="pygame is cool", | |
| color=(0, 0, 0), | |
| fontsize=32): | |
| """write text into pygame surfaces""" | |
| myfont = pygame.font.SysFont("System", fontsize) | |
| mytext = myfont.render(msg, True, color) | |
| mytext = mytext.convert_alpha() | |
| return mytext | |
| class Santa(pygame.sprite.Sprite): | |
| def __init__(self): | |
| super().__init__(self.groups) | |
| self.image = santaimage | |
| self.rect = self.image.get_rect() | |
| self.x = sx | |
| self.y = sy | |
| self.rect.center = (self.x, self.y) | |
| def update(self, delta_time): | |
| self.image = santaimage | |
| self.x = sx | |
| self.y = sy | |
| self.rect = self.image.get_rect() | |
| self.rect.center = (self.x, self.y) | |
| class Sack(pygame.sprite.Sprite): | |
| def __init__(self): | |
| super().__init__(self.groups) | |
| self.image = sackimage | |
| self.rect = self.image.get_rect() | |
| self.y = -100 | |
| self.x = random.randint(0, SCREEN_X) | |
| self.rect.center = (self.x, self.y) | |
| self.dy = random.uniform(1,4) | |
| self.alpha = 255 | |
| self.image.set_alpha(self.alpha) | |
| def update(self, delta_time): | |
| self.y += self.dy | |
| self.rect.center = (self.x, self.y) | |
| if self.y > SCREEN_Y/2: # beginne ab hälfte y zu faden | |
| self.alpha -= 1 | |
| self.image.set_alpha(self.alpha) | |
| if self.alpha <= 0: | |
| self.kill() | |
| class FlugText(pygame.sprite.Sprite): | |
| def __init__(self, x, y, fading=True, moving=True, text="1"): | |
| super().__init__(self.groups) | |
| self.time = 0 | |
| self.duration = 2 # seconds | |
| self.image = write(text, (255,0,0), 36) | |
| self.rect = self.image.get_rect() | |
| self.rect.center = (x,y) | |
| self.x = x | |
| self.y = y | |
| self.text = text | |
| self.fading = fading | |
| self.moving = moving | |
| self.dy = -40 | |
| self.dx = -80 | |
| self.alpha = 255 | |
| def neuer_text(self, neuer_text): | |
| self.image = write(neuer_text, (255, 0, 0), 36) | |
| self.rect = self.image.get_rect() | |
| self.rect.center = (self.x, self.y) | |
| self.text = neuer_text | |
| def update(self, delta_time): | |
| if self.fading: | |
| self.image.set_alpha(self.alpha) | |
| self.alpha -= 1 | |
| #self.time += delta_time / 1000 | |
| if self.moving: | |
| self.y += self.dy * delta_time / 1000 | |
| self.x += self.dx * delta_time / 1000 | |
| self.rect.center = (self.x, self.y) | |
| if (self.y < 0) or (self.x < 0) or (self.alpha < 1): | |
| self.kill() | |
| 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 | |
| FlugText.groups = allgroup, textgroup | |
| Sack.groups = allgroup, sackgroup | |
| Santa.groups = allgroup, santagroup | |
| #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 | |
| scoresprite = FlugText(SCREEN_X -100, 10, fading=False, | |
| moving=False, text = f"Score: {score}") | |
| geschenkesprite = FlugText(SCREEN_X-100, 30, fading=False, | |
| moving=False, | |
| text=f"Geschenke: {geschenke}") | |
| santasprite = Santa() | |
| while running: | |
| milliseconds = clock.tick(60) | |
| zeit += milliseconds / 1000 | |
| lückenzeit += 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 | |
| # neuer Sack ? | |
| if len(sackgroup) == 0: | |
| if random.random() < 0.01 : | |
| Sack() | |
| 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: | |
| if (geschenke > 0) and (lückenzeit > ladezeit): | |
| Geschenk(sx + 100, sy + 80) | |
| lückenzeit = 0.0 | |
| geschenke -= 1 | |
| geschenkesprite.neuer_text(f"Geschenke: {geschenke}") | |
| 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 | |
| # sack und santa? | |
| if len(sackgroup) == 1: | |
| for sack in sackgroup: | |
| distanz = ((sx-sack.x)**2 + (sy-sack.y)**2)**0.5 | |
| if distanz < 100: | |
| geschenke += 10 | |
| sack.kill() | |
| geschenkesprite.neuer_text(f"Geschenke: {geschenke}") | |
| # haus und geschenk ? | |
| 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: | |
| # Treffer Geschenk auf Kamin | |
| if (mein_haus.x + mein_haus.kamin_x - 20) < mein_geschenk.x < (mein_haus.x + mein_haus.kamin_x + 20): | |
| FlugText(mein_geschenk.x, mein_geschenk.y) | |
| mein_geschenk.kill() | |
| score += 1 | |
| scoresprite.neuer_text(f"Score: {score}") | |
| # 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