Skip to content

Instantly share code, notes, and snippets.

Created May 3, 2014 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9a529b53909ef8148bd4 to your computer and use it in GitHub Desktop.
Save anonymous/9a529b53909ef8148bd4 to your computer and use it in GitHub Desktop.
import pygame
import sys
import random
skierf = "monkey_man.png"
class SkierClass(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("monkey_man.png")
self.rect = self.image.get_rect()
self.rect.center = [100, 365]
def go_Down(self):
self.rect.centery += 5
def go_Up(self):
self.rect.centery -= 5
def move(self, speed):
self.rect.centery = self.rect.centery + speed[1]
if self.rect.centery < 10: self.rect.centery = 10
if self.rect.centery > 720: self.rect.centery = 720
# class for obstacle sprites (trees and flags)
class ObstacleClass(pygame.sprite.Sprite):
def __init__(self, image_file, location, type):
pygame.sprite.Sprite.__init__(self)
self.image_file = image_file
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.center = location
self.type = type
self.passed = False
def update(self):
global speed
self.rect.centerx -= speed[0]
# create one "screen" of obstacles: 640 x 640
# use "blocks" of 64 x 64 pixels, so objects aren't too close together
def create_map():
global obstacles
locations = []
for i in range(10): # 10 obstacles per screen
row = random.randint(0, 9)
col = random.randint(0, 9)
location = [col * 76 + 38 + 760, row * 73 + 32] #center x, y for obstacle
if not (location in locations): # prevent 2 obstacles in the same place
locations.append(location)
type = random.choice([1, 2, 3, 4, 5])
if type == 1 or type == 3 or type == 5: img = "skier_flag.png"
elif type == 2: img = 'flag.png'
elif type == 4: img = 'gift.png'
obstacle = ObstacleClass(img, location, type)
obstacles.add(obstacle)
# redraw the screen, including all sprites
def animate():
screen.fill([0,0,0])
screen.blit(picture, [0,0])
obstacles.draw(screen)
screen.blit(skier.image, skier.rect)
screen.blit(text, [10, 10])
screen.blit(picture, [0,0])
pygame.display.flip()
# initialize everything
pygame.init()
pygame.mixer.init()
picture = pygame.image.load('background.gif')
picture = pygame.transform.scale(picture, (770, 740))
screen = pygame.display.set_mode([760, 730])
pygame.display.set_caption('Snowman Duplex')
clock = pygame.time.Clock()
speed = [6, 0]
points = 0
skier = SkierClass()
obstacles = pygame.sprite.Group()
create_map()
map_position = 0
delay = 100
interval = 50
pygame.key.set_repeat(delay, interval)
# main Pygame event loop
running = True
held_down = False
frames = 30
pygame.mixer.music.load('music.mp3')
pygame.mixer.music.set_volume(0.275)
pygame.mixer.music.play(-1)
open_m = pygame.mixer.Sound('open.wav')
open_m.set_volume(0.7)
while running:
clock.tick(frames)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.mixer.music.fadeout(1000)
pygame.time.delay(1000)
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
held_down = True
elif event.type == pygame.MOUSEBUTTONUP:
held_down = False
elif event.type == pygame.MOUSEMOTION:
if held_down:
skier.rect.center = event.pos
skier.move(speed)
map_position += speed[0] # scroll the obstacles
# create a new block of obstacles at the bottom
if map_position >= 760:
create_map()
map_position = 0
hit = pygame.sprite.spritecollide(skier, obstacles, False)
if hit:
if hit[0].type == 1 and not hit[0].passed or hit[0].type == 3 and not hit[0].passed or hit[0].type == 5 and not hit[0].passed:
points = points - 150
skier.image = pygame.image.load('blow_up.png')
animate()
pygame.time.delay(540)
skier.image = pygame.image.load(skierf)
hit[0].passed = False
elif hit[0].type == 2 and not hit[0].passed:
points += 10
hit[0].kill()
elif hit[0].type == 4 and not hit[0].passed:
points += 25
hit[0].kill()
obstacles.update()
font = pygame.font.Font(None, 75)
text = font.render("Score: " + str(points), 1, (255, 255, 255))
animate()
pygame.quit()
# By the way, I have 'monkey_man.png'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment