def __init__(self, parent, name): Location.__init__(self, parent) pygame.key.set_repeat(10) pygame.mouse.set_visible(0) self.doodle = Doodle(name, screen_width // 2, screen_height - 120) self.allsprites = pygame.sprite.Group() self.allsprites.add(self.doodle) self.start_line = StartLine(screen_width // 2, screen_height - 40) self.is_remove_start_line = True self.allsprites.add(self.start_line) self.platforms = [] self.springs = [] for i in range(0, platform_count): platform = self._random_platform(False) self.platforms.append(platform) self.allsprites.add(platform) if platform.get_spring() != None: self.springs.append(platform.get_spring()) self.allsprites.add(platform.get_spring()) self.score_sprite = TextSprite(50, 25, self.doodle.name, 45, (0,0,0)) self.allsprites.add(self.score_sprite) self.header = Rectangle(screen_width, 50, (0, 191, 255, 128)) self.window.blit(self.background, (0, 0))
def __init__(self, parent, name): Location.__init__(self, parent) pygame.key.set_repeat(10) pygame.mouse.set_visible(0) self.doodle = Doodle(name) self.doodle.name = name self.allsprites = pygame.sprite.Group() self.allsprites.add(self.doodle) for i in range(0, platform_count): self.allsprites.add(self.randomPlatform(False)) for platform in self.allsprites: if isinstance(platform, Platform) and platform.spring != None: self.allsprites.add(platform.spring) self.score_sprite = TextSprite(50, 25, self.doodle.name, 45, (0, 0, 0)) self.allsprites.add(self.score_sprite) self.header = Rectangle(screen_width, 50, (0, 191, 255, 128)) self.window.blit(self.background, (0, 0)) self.monster = None
def __init__(self, parent, name): Location.__init__(self, parent) pygame.mouse.set_visible(0) self.doodle = Doodle(name) self.doodle.name = name self.allsprites = pygame.sprite.Group() self.allsprites.add(self.doodle) for i in range(0, 8): self.allsprites.add(self.randomPlatform(randint(-100, 450), randint(0, 640))) self.score_sprite = TextSprite(50,25,self.doodle.name, 45, (0,0,0)) self.allsprites.add(self.score_sprite) self.header = Header()
def __init__(self, parent, name): Location.__init__(self, parent) pygame.key.set_repeat(10) pygame.mouse.set_visible(0) self.doodle = Doodle(name) self.doodle.name = name self.allsprites = pygame.sprite.Group() self.allsprites.add(self.doodle) for i in range(0, 12): self.allsprites.add(self.randomPlatform(False)) self.score_sprite = TextSprite(50,25,self.doodle.name, 45, (0,0,0)) self.allsprites.add(self.score_sprite) self.header = Rectangle(480, 50, (0,191,255,128)) self.window.blit(self.background, (0, 0))
def __init__(self, parent, name): Location.__init__(self, parent) pygame.key.set_repeat(10) pygame.mouse.set_visible(0) self.doodle = Doodle(name) self.doodle.name = name self.allsprites = pygame.sprite.Group() self.allsprites.add(self.doodle) for i in range(0, platform_count): self.allsprites.add(self.randomPlatform(False)) for platform in self.allsprites: if isinstance(platform, Platform) and platform.spring != None: self.allsprites.add(platform.spring) self.score_sprite = TextSprite(50,25,self.doodle.name, 45, (0,0,0)) self.allsprites.add(self.score_sprite) self.header = Rectangle(screen_width, 50, (0,191,255,128)) self.window.blit(self.background, (0, 0)) self.monster = None
class GameLocation(Location): gravitation = 0.2 mouse_enabled = True transparent_walls = True def __init__(self, parent, name): Location.__init__(self, parent) pygame.key.set_repeat(10) pygame.mouse.set_visible(0) self.doodle = Doodle(name) self.doodle.name = name self.allsprites = pygame.sprite.Group() self.allsprites.add(self.doodle) for i in range(0, 12): self.allsprites.add(self.randomPlatform(False)) self.score_sprite = TextSprite(50,25,self.doodle.name, 45, (0,0,0)) self.allsprites.add(self.score_sprite) self.header = Rectangle(480, 50, (0,191,255,128)) self.window.blit(self.background, (0, 0)) def randomPlatform(self,top = True): x = randint(-20, 450) bad_y = [] for spr in self.allsprites: bad_y.append((spr.y-20, spr.y+20)) good = 0 while good == 0: if top: y = randint(-50, 50) else: y = randint(0, 640) good = 1 for bad_y_item in bad_y: if bad_y_item[0] <= y <= bad_y_item[1]: good = 0 break dig = randint(0, 100) if dig < 35: return MovingPlatform(x,y) elif dig >= 35 and dig < 50: return CrashingPlatform(x,y) else: return Platform(x,y) def draw(self): if self.doodle.alive == 1: self.allsprites.clear(self.window, self.background) # doodler jumps mousePos = pygame.mouse.get_pos() self.doodle.incYSpeed(-self.gravitation) if self.mouse_enabled: self.doodle.setX(mousePos[0]) else: if self.transparent_walls: if self.doodle.x < 0: self.doodle.setX(480) elif self.doodle.x > 480: self.doodle.setX(0) self.doodle.moveY(-self.doodle.ySpeed) for spr in self.allsprites: # if platform under legs if isinstance(spr, Platform) and self.doodle.getLegsRect().colliderect(spr.getSurfaceRect()) and self.doodle.ySpeed <= 0: if isinstance(spr,CrashingPlatform): spr.crash() break self.doodle.ySpeed = 10 if isinstance(spr, Platform): # renew platforms if spr.y >= 640: self.allsprites.remove(spr) self.allsprites.add(self.randomPlatform()) #spr.renew() # move blue and crashed platforms if isinstance(spr,MovingPlatform) or (isinstance(spr,CrashingPlatform) and spr.crashed == 1): spr.move() # moving whole world if self.doodle.y < 300: self.doodle.incScore(self.doodle.ySpeed) for spr in self.allsprites: if not isinstance(spr, TextSprite): spr.moveY(self.doodle.ySpeed) #draw all on canvas self.allsprites.draw(self.window) self.score_sprite.setText(" %s, %s" % (self.doodle.name, int(self.doodle.score/10))) self.window.blit(self.header, (0,0)) else: #if dead - load exit location self.parent.location = GameLocation(self.parent,self.doodle.name) def event(self,event): if event.type == KEYDOWN: if event.key == K_LEFT: self.doodle.setX(self.doodle.x - 10) elif event.key == K_RIGHT: self.doodle.setX(self.doodle.x + 10)
class GameLocation(Location): def __init__(self, parent, name): Location.__init__(self, parent) pygame.key.set_repeat(10) pygame.mouse.set_visible(0) self.doodle = Doodle(name) self.doodle.name = name self.allsprites = pygame.sprite.Group() self.allsprites.add(self.doodle) for i in range(0, platform_count): self.allsprites.add(self.randomPlatform(False)) for platform in self.allsprites: if isinstance(platform, Platform) and platform.spring != None: self.allsprites.add(platform.spring) self.score_sprite = TextSprite(50, 25, self.doodle.name, 45, (0, 0, 0)) self.allsprites.add(self.score_sprite) self.header = Rectangle(screen_width, 50, (0, 191, 255, 128)) self.window.blit(self.background, (0, 0)) self.monster = None def randomPlatform(self, top=True): x = randint(0, screen_width - platform_width) bad_y = [] for spr in self.allsprites: bad_y.append((spr.y - platform_y_padding, spr.y + platform_y_padding + spr.rect.height)) good = 0 while not good: if top: y = randint(-100, 50) else: y = randint(0, screen_height) good = 1 for bad_y_item in bad_y: if bad_y_item[0] <= y <= bad_y_item[1]: good = 0 break dig = randint(0, 100) if dig < 35: return MovingPlatform(x, y) elif dig >= 35 and dig < 50: return CrashingPlatform(x, y) else: return Platform(x, y) def draw(self): if self.doodle.alive == 1: # create monster if self.monster == None: case = randint(-1000, 5) if case > 0: self.monster = Monster(randint(0, screen_width), randint(-50, 50)) self.allsprites.add(self.monster) self.monster.move() else: self.monster.move() # touch monster if self.doodle.rect.colliderect(self.monster.rect): self.doodle.alive = 0 if self.monster.y >= screen_height: self.allsprites.remove(self.monster) self.monster = None self.allsprites.clear(self.window, self.background) # doodler jumps mousePos = pygame.mouse.get_pos() self.doodle.inc_y_speed(-gravitation) if mouse_enabled: self.doodle.set_x(mousePos[0]) else: if transparent_walls: if self.doodle.x < 0: self.doodle.set_x(screen_width) elif self.doodle.x > screen_width: self.doodle.set_x(0) self.doodle.move_y(-self.doodle.ySpeed) for spr in self.allsprites: # if spring under legs if isinstance( spr, Spring) and self.doodle.get_legs_rect().colliderect( spr.get_top_surface()) and self.doodle.ySpeed <= 0: spr.compress() self.doodle.ySpeed = spring_speed # if platform under legs if isinstance(spr, Platform) and self.doodle.get_legs_rect( ).colliderect( spr.get_surface_rect()) and self.doodle.ySpeed <= 0: if isinstance(spr, CrashingPlatform): spr.crash() break self.doodle.ySpeed = jump_speed if isinstance(spr, Platform): # renew platforms if spr.y >= screen_height: self.allsprites.remove(spr) platform = self.randomPlatform() self.allsprites.add(platform) if isinstance(platform, Platform) and platform.spring != None: self.allsprites.add(platform.spring) # move blue and crashed platforms if isinstance(spr, MovingPlatform) or (isinstance( spr, CrashingPlatform) and spr.crashed == 1): spr.move() # moving whole world if self.doodle.y < horizont: self.doodle.inc_score(self.doodle.ySpeed) for spr in self.allsprites: if not isinstance(spr, TextSprite): spr.move_y(self.doodle.ySpeed) #draw all on canvas self.allsprites.draw(self.window) self.score_sprite.setText( " %s, %s" % (self.doodle.name, int(self.doodle.score / 10))) self.window.blit(self.header, (0, 0)) else:
class GameLocation(Location): def __init__(self, parent, name): Location.__init__(self, parent) pygame.key.set_repeat(10) pygame.mouse.set_visible(0) self.doodle = Doodle(name) self.doodle.name = name self.allsprites = pygame.sprite.Group() self.allsprites.add(self.doodle) for i in range(0, platform_count): self.allsprites.add(self.randomPlatform(False)) for platform in self.allsprites: if isinstance(platform, Platform) and platform.spring != None: self.allsprites.add(platform.spring) self.score_sprite = TextSprite(50,25,self.doodle.name, 45, (0,0,0)) self.allsprites.add(self.score_sprite) self.header = Rectangle(screen_width, 50, (0,191,255,128)) self.window.blit(self.background, (0, 0)) self.monster = None def randomPlatform(self,top = True): x = randint(0, screen_width - platform_width) bad_y = [] for spr in self.allsprites: bad_y.append((spr.y-platform_y_padding, spr.y + platform_y_padding + spr.rect.height)) good = 0 while not good: if top: y = randint(-100, 50) else: y = randint(0, screen_height) good = 1 for bad_y_item in bad_y: if bad_y_item[0] <= y <= bad_y_item[1]: good = 0 break dig = randint(0, 100) if dig < 35: return MovingPlatform(x,y) elif dig >= 35 and dig < 50: return CrashingPlatform(x,y) else: return Platform(x,y) def draw(self): if self.doodle.alive == 1: # create monster if self.monster == None: case = randint(-1000,5) if case > 0: self.monster = Monster(randint(0, screen_width), randint(-50, 50)) self.allsprites.add(self.monster) self.monster.move() else: self.monster.move() # touch monster if self.doodle.rect.colliderect(self.monster.rect): self.doodle.alive = 0 if self.monster.y >= screen_height: self.allsprites.remove(self.monster) self.monster = None self.allsprites.clear(self.window, self.background) # doodler jumps mousePos = pygame.mouse.get_pos() self.doodle.inc_y_speed(-gravitation) if mouse_enabled: self.doodle.set_x(mousePos[0]) else: if transparent_walls: if self.doodle.x < 0: self.doodle.set_x(screen_width) elif self.doodle.x > screen_width: self.doodle.set_x(0) self.doodle.move_y(-self.doodle.ySpeed) for spr in self.allsprites: # if spring under legs if isinstance(spr, Spring) and self.doodle.get_legs_rect().colliderect(spr.get_top_surface()) and self.doodle.ySpeed <= 0: spr.compress() self.doodle.ySpeed = spring_speed # if platform under legs if isinstance(spr, Platform) and self.doodle.get_legs_rect().colliderect(spr.get_surface_rect()) and self.doodle.ySpeed <= 0: if isinstance(spr,CrashingPlatform): spr.crash() break self.doodle.ySpeed = jump_speed if isinstance(spr, Platform): # renew platforms if spr.y >= screen_height: self.allsprites.remove(spr) platform = self.randomPlatform() self.allsprites.add(platform) if isinstance(platform, Platform) and platform.spring != None: self.allsprites.add(platform.spring) # move blue and crashed platforms if isinstance(spr,MovingPlatform) or (isinstance(spr,CrashingPlatform) and spr.crashed == 1): spr.move() # moving whole world if self.doodle.y < horizont: self.doodle.inc_score(self.doodle.ySpeed) for spr in self.allsprites: if not isinstance(spr, TextSprite): spr.move_y(self.doodle.ySpeed) #draw all on canvas self.allsprites.draw(self.window) self.score_sprite.setText(" %s, %s" % (self.doodle.name, int(self.doodle.score/10))) self.window.blit(self.header, (0,0)) else: #if dead - load exit location self.parent.location = GameLocation(self.parent,self.doodle.name) def event(self,event): if event.type == KEYDOWN: if event.key == K_LEFT: self.doodle.set_x(self.doodle.x - 10) elif event.key == K_RIGHT: self.doodle.set_x(self.doodle.x + 10)
class GameLocation(Location): gravitation = 0.2 def __init__(self, parent, name): Location.__init__(self, parent) pygame.mouse.set_visible(0) self.doodle = Doodle(name) self.doodle.name = name self.allsprites = pygame.sprite.Group() self.allsprites.add(self.doodle) for i in range(0, 8): self.allsprites.add(self.randomPlatform(randint(-100, 450), randint(0, 640))) self.score_sprite = TextSprite(50,25,self.doodle.name, 45, (0,0,0)) self.allsprites.add(self.score_sprite) self.header = Header() def randomPlatform(self, x , y): dig = randint(0, 100) if dig < 35: return MovingPlatform(x,y) elif dig >= 35 and dig < 50: return CrashingPlatform(x,y) else: return Platform(x,y) def draw(self): self.window.blit(self.background, (0, 0)) if self.doodle.alive == 1: # doodler jumps mousePos = pygame.mouse.get_pos() self.doodle.incYSpeed(-self.gravitation) self.doodle.setX(mousePos[0]) self.doodle.moveY(-self.doodle.ySpeed) for spr in self.allsprites: # if platform under legs if isinstance(spr, Platform) and self.doodle.getLegsRect().colliderect(spr.getSurfaceRect()) and self.doodle.ySpeed <= 0: if isinstance(spr,CrashingPlatform): spr.crash() break self.doodle.ySpeed = 10 if isinstance(spr, Platform): # renew platforms if spr.y >= 640: spr.renew() # move blue and crashed platforms if isinstance(spr,MovingPlatform) or (isinstance(spr,CrashingPlatform) and spr.crashed == 1): spr.move() # moving whole world if self.doodle.y < 300: self.doodle.incScore(self.doodle.ySpeed) for spr in self.allsprites: if not isinstance(spr, TextSprite): spr.moveY(self.doodle.ySpeed) #draw all on canvas self.allsprites.draw(self.window) self.score_sprite.setText(" %s, %s" % (self.doodle.name, int(self.doodle.score/10))) self.window.blit(self.header, (0,0)) else: #if dead - load exit location self.parent.location = GameLocation(self.parent,self.doodle.name) def event(self,event): if event.type == KEYDOWN: print event.key
class GameLocation(Location): def __init__(self, parent, name): Location.__init__(self, parent) pygame.key.set_repeat(10) pygame.mouse.set_visible(0) self.doodle = Doodle(name, screen_width // 2, screen_height - 120) self.allsprites = pygame.sprite.Group() self.allsprites.add(self.doodle) self.start_line = StartLine(screen_width // 2, screen_height - 40) self.is_remove_start_line = True self.allsprites.add(self.start_line) self.platforms = [] self.springs = [] for i in range(0, platform_count): platform = self._random_platform(False) self.platforms.append(platform) self.allsprites.add(platform) if platform.get_spring() != None: self.springs.append(platform.get_spring()) self.allsprites.add(platform.get_spring()) self.score_sprite = TextSprite(50, 25, self.doodle.name, 45, (0,0,0)) self.allsprites.add(self.score_sprite) self.header = Rectangle(screen_width, 50, (0, 191, 255, 128)) self.window.blit(self.background, (0, 0)) def _random_platform(self, top = True): x = randint(platform_width, screen_width - platform_width) bad_y = [] for spr in self.allsprites: bad_y.append((spr.y - platform_y_padding, spr.y + platform_y_padding + spr.rect.height)) good = False for i in range(0, 10): if top: y = randint(-40, 30) else: y = randint(0, screen_height) good = True for bad_y_item in bad_y: if bad_y_item[0] <= y <= bad_y_item[1]: good = False break if good: break dig = randint(0, 100) if dig <= 30: return MovingPlatform(x, y) elif dig > 30 and dig <= 60: return CrashingPlatform(x, y) else: return BasicPlatform(x, y) def _update_platforms(self): for spr in self.platforms: if self.doodle.get_legs_rect().colliderect(spr.get_surface_rect()) and self.doodle.get_y_speed() <= 0: spr.crash() self.doodle.set_y_speed(jump_speed) spr.move() if spr.y >= screen_height: self.allsprites.remove(spr) self.platforms.remove(spr) if spr.get_spring() != None: self.springs.remove(spr.get_spring()) self.allsprites.remove(spr.get_spring()) platform = self._random_platform() self.platforms.append(platform) self.allsprites.add(platform) if platform.get_spring() != None: self.springs.append(platform.get_spring()) self.allsprites.add(platform.get_spring()) def _update_springs(self): for spr in self.springs: if self.doodle.get_legs_rect().colliderect(spr.get_top_surface()) and self.doodle.get_y_speed() <= 0: spr.compress() self.doodle.set_y_speed(spring_speed) def _update_scores(self): if self.doodle.y < screen_height // 2: self.doodle.inc_score(self.doodle.get_y_speed()) for spr in self.allsprites: if not isinstance(spr, TextSprite): spr.move_y(self.doodle.get_y_speed()) def _update_start_line(self): if self.is_remove_start_line: if self.doodle.get_legs_rect().colliderect(self.start_line.get_surface_rect()) and self.doodle.get_y_speed() <= 0: self.doodle.set_y_speed(jump_speed) if self.start_line.y >= screen_height: self.allsprites.remove(self.start_line) self.is_remove_start_line = False def draw(self): if self.doodle.is_live(): self.allsprites.clear(self.window, self.background) self.doodle.inc_y_speed(-gravitation) if self.doodle.x < 0: self.doodle.set_x(screen_width) elif self.doodle.x > screen_width: self.doodle.set_x(0) self.doodle.move_y(-self.doodle.get_y_speed()) self._update_platforms() self._update_springs() self._update_start_line() self._update_scores() self.allsprites.draw(self.window) self.score_sprite.set_text(" %s, %s" % (self.doodle.get_name(), int(self.doodle.get_score() // 10))) self.window.blit(self.header, (0,0)) else: self.parent.location = GameLocation(self.parent, self.doodle.get_name()) def event(self, event): if event.type == KEYDOWN: if event.key == K_LEFT: self.doodle.set_x(self.doodle.x - 10) elif event.key == K_RIGHT: self.doodle.set_x(self.doodle.x + 10)