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): 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