secret
Created

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
import pygame, sys, random
 
skier_images = ["skier_down.png", "skier_right1.png",
"skier_right2.png", "skier_left2.png",
"skier_left1.png"]
#This part is to create the skier
class SkierClass(pygame.sprite.Sprite) :
def __init__(self) :
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("skier_down.png")
self.rect = self.image.get_rect()
self.rect.center = [320, 100]
self.angle = 0
#This part is to turn the skier
def turn(self, direction) :
self.angle = self.angle + direction
if self.angle < -2: self.angle = -2
if self.angle > 2: self.angle = 2
center = self.rect.center
self.image = pygame.image.load(skier_images[self.angle])
self.rect = self.image.get_rect()
self.rect.center = center
speed = [self.angle, 6 - abs(slef.angle) * 2]
return speed
#This part is to move the skier left and right
def move(self, speed):
self.rect.centerx = self.rect.centerx + speed[0]
if self.rect.centerx < 20: self.rect.centerx = 20
if self.rect.centerx > 620: self.rect.centerx = 620
#This part is to create the trees and flags
class ObstacleClass(pygame.sprite.Sprite):
def _init__(self, image_file, location, type):
pygmae.sprite.Sprite.__init__(self)
self.image_file = image_file
self.image = pygame.image.load(image_file)
self.location = location
self.rect = self.image.get_rect()
self.rect.center = location
self.type = type
self.passed = False
#This part is to make the scenery scroll up
def scroll(self, terrainPos):
self.rect.centery = self.location[1] - terrainPos
#This part creates one window of random trees and flags
def create_map(start, end):
obstacles = pygame.sprite.Group()
locations = []
gates = pygame.sprite.Group()
for i in range(10):
row = random.randint (start, end)
col = random.randint (0,9)
location = [col * 64 + 20, row * 64 + 20]
if not (location in locations):
locations.append(location)
type = random.choice(["tree", "flag"])
if type == "tree": img = "skier_tree.png"
elif type == "flag": img = "skier_flag.png"
obstacle = ObstacleClass(img, location, type)
obstacles.add(obstacle)
return obstacle
#This part redraws screen when things move
def animate ():
screen.fill([255, 255, 255])
pygame.display.update(obstacle.draw(screen))
screen.blit(skier.image, skier.rect)
screen.blit(score_text, [10, 10])
pygame.display.flip()
#This part changes to next screen of scenery
def updateObstacleGroup (map0, map1):
obstacles = pygame.sprite.Group()
for ob in map0: obstacles.add(ob)
for ob in map1: obstacles.add(ob)
return obstacles
#This part gets everything ready
pygame.init()
screen = pygame.display.set_mode([640, 640])
clock = pygame.time.Clock()
skier = SkierClass()
speed = [0, 6]
map_position = 0
points = 0
map0 = create_map(20, 29)
map1 = create_map(10, 19)
activeMap = 0
#Group for all obstacles to do collision detection
obstacles = updateObstacleGroup(map0, map1)
#Font object for score
font = pygame.font.Font(None, 50)
#This part checks for keypresses or window close
while True: #This part starts the main loop
clock.tick(30) #this part updates graphics 30 times per second
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed = skier.turn(1)
elif event.key == pygame.K_RIGHT:
speed = skier.turn(1)
skier.move(speed) #This part moves the skier
map_position += speed[1] #This part scrolls the scenery
#This part changes from one window of scenery to the next
if map_position >=640 and activeMap == 0:
activeMap = 1
map0 = create_map(20, 29)
obstacles = updateObstacleGroup(map0, map1)
if map_position >=1280 and activeMap == 1:
activeMap = 0
for ob in map0:
ob.location[1] = ob.location[1] - 1280
map_position = map_position - 1280
map1 = create_map(10, 19)
obstacles = updateObstacleGroup (map0, map1)
 
for obstacle in obstacles:
obstacle.scroll(map_position)
#This part checks for hitting trees and getting flags
hit = pygame.sprite.spritecollide(skier, obstacles, False)
if hit:
if hit[0].type == "tree" and not hit[0].passed:
points = points - 100
skier.image = pygame.image.load("skier_crash.png")
animate()
pygame.time.delay(1000)
skier.image = pygame.image.load("skier_down.png")
skier.angle = 0
speed = [0, 6]
hit[0].passed = True
elif hit[0].type == "flag" and not hit[0].passed:
points += 10
obstacles.remove(hit[0])
 
 
score_text = font.render("Score: " +str(points), 1, (0, 0, 0)) #This part displays score
animate()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.