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, left: int, top: int, game_player, image_height: int=50): super().__init__() self.game_player = game_player surface = game_player.character.face_image name = game_player.player.name stock = game_player.stock # キャラ画像 image_width = image_height rect = Rect(left, top, image_width, image_height) image = surface_fit_to_rect(surface, rect) self.character_sprite = SimpleSprite(rect, image) self.add(self.character_sprite) # ストックの丸 self.stock = stock self.stock_sprites = [Group() for i in range(stock)] x, y = self.character_sprite.rect.bottomright stock_radius = int(0.05 * image_height) left, top = x, y - stock_radius * 2 for i in range(stock): surface = Surface((stock_radius * 2, stock_radius * 2)).convert_alpha() surface.fill((125, 125, 125)) surface.set_colorkey(surface.get_at((0, 0))) pygame.gfxdraw.filled_circle(surface, stock_radius, stock_radius, stock_radius-1, (255, 255, 255)) stock_sprite = SimpleSprite(Rect(left, top, stock_radius * 2, stock_radius * 2), surface) self.stock_sprites[i].add(stock_sprite) print(self.stock_sprites[i]) left += stock_radius * 2 # プレイヤーネーム font_size = int(0.2 * image_height) font = pygame.font.Font(None, font_size) left, bottom = self.character_sprite.rect.bottomright bottom -= stock_radius * 2 self.player_name_sprite = TextSprite( x=left, y=bottom, align="left", vertical_align="bottom", text=name, font=font, color=(255, 255, 255) ) self.add(self.player_name_sprite) # プレイヤーネームのbg width = self.character_sprite.rect.w + max(stock_radius * 2 * stock, self.player_name_sprite.rect.w) + 10 height = self.character_sprite.rect.h self.base_rect = Rect(*self.character_sprite.rect.topleft, width, height) rect = self.base_rect bg_image = Surface(rect.size).convert_alpha() bg_image.fill((0, 0, 0)) bg_image.set_alpha(225) bg_sprite = SimpleSprite(rect, bg_image) self.bg_group = Group() self.bg_group.add(bg_sprite)
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, game_player1, game_player2, rect) -> None: super().__init__() self.winner = self._winner(game_player1, game_player2) if self.winner is None: self.text = "DRAW" else: self.text = self.winner.player.name + " WIN!!" self.winner.player.win_num += 1 game_player1.player.matches_num += 1 game_player2.player.matches_num += 1 self.rect = rect self.font_size = self.rect.height // 3 textsprite = TextSprite(x=self.rect.centerx, y=self.rect.centery, text=self.text, font=pygame.font.Font( "./fonts/Mplus2-Medium.ttf", self.font_size), align="center", vertical_align="middle") textsprite.center = self.rect.center self.add(textsprite)
def label_sprites(self, sprites, color, font): for sprite in sprites: name = sprite.name label = TextSprite("Label for {}".format(name)) self.set_text( label, name, color, font ) label.set_group(sprite.group) w, h = label.size x, y = sprite.position label.set_position(x, y - h)
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 replace_text(self, text="", font_size=int(1e5), color=(0, 0, 0)): """[summary]. Args: text (str, optional): [description]. Defaults to "". font_size ([type], optional): [description]. Defaults to int(1e5). color (tuple, optional): [description]. Defaults to (0, 0, 0). """ if self.badgesprite.sprite.rect.height < font_size: font_size = int(2**0.5 * self.badgesprite.sprite.rect.height) text_sprite = TextSprite(x=self.badgesprite.sprite.rect.centerx, y=self.badgesprite.sprite.rect.centery, text=text, font=pygame.font.Font(None, font_size), color=color, align="center", vertical_align="middle") if 2**0.5 * self.badgesprite.sprite.rect.width < text_sprite.rect.width: print("dekasugi") self.textsprite.sprite.add(None) self.textsprite.add(text_sprite)
def _set_animation(self): """アニメーションの設定 """ self.timer_group = TimerGroup() self.middle_sprites.add(self.timer_group) self.transform_manager = SpriteTransformManager() self.middle_sprites.add(self.transform_manager) rects = layout_rects(self.view_area, 2, 1, padding=40, margin_vertical=40) self.hand_sprites = { self.actor1: { self.Hand.ROCK: SimpleSprite( rects[0], fit_surface(self.actor1.game_player.character.arm_image[0], rects[0])), self.Hand.SCISSORS: SimpleSprite( rects[0], fit_surface(self.actor1.game_player.character.arm_image[1], rects[0])), self.Hand.PAPER: SimpleSprite( rects[0], fit_surface(self.actor1.game_player.character.arm_image[2], rects[0])), }, self.actor2: { self.Hand.ROCK: SimpleSprite( rects[1], fit_surface(self.actor2.game_player.character.arm_image[0], rects[1])), self.Hand.SCISSORS: SimpleSprite( rects[1], fit_surface(self.actor2.game_player.character.arm_image[1], rects[1])), self.Hand.PAPER: SimpleSprite( rects[1], fit_surface(self.actor2.game_player.character.arm_image[2], rects[1])), } } # wait_surface = self.font.render("wait", True, (0, 0, 0), (255, 255, 255)) # ready_surface = self.font.render("ready", True, (0, 0, 0), (255, 255, 255)) self.actor_state_sprites = { self.actor1: { True: TextSprite(*rects[0].midbottom, align="center", vertical_align="bottom", text="wait", font=self.font, color=(0, 0, 0), bgcolor=(255, 255, 255)), False: TextSprite(*rects[0].midbottom, align="center", vertical_align="bottom", text="ready", font=self.font, color=(0, 0, 0), bgcolor=(255, 255, 255)), }, self.actor2: { True: TextSprite(*rects[1].midbottom, align="center", vertical_align="bottom", text="wait", font=self.font, color=(0, 0, 0), bgcolor=(255, 255, 255)), False: TextSprite(*rects[1].midbottom, align="center", vertical_align="bottom", text="ready", font=self.font, color=(0, 0, 0), bgcolor=(255, 255, 255)), } } self.actor_state_group = Group() self.middle_sprites.add(self.actor_state_group) # 「さいしょは ぐー」などのセリフスプライト self.before_battle_sprite1 = TextSprite(*self.view_area.center, align="center", vertical_align="middle", text="さいしょは ぐー", font=self.font, color=(0, 0, 0), bgcolor=(255, 255, 255)) self.before_battle_sprite2 = TextSprite(*self.view_area.center, align="center", vertical_align="middle", text="じゃんけん...", font=self.font, color=(0, 0, 0), bgcolor=(255, 255, 255)) self.before_battle_sprite3 = TextSprite(*self.view_area.center, align="center", vertical_align="middle", text="ぽん", font=self.font, color=(0, 0, 0), bgcolor=(255, 255, 255)) self.before_battle_sprite4 = TextSprite(*self.view_area.center, align="center", vertical_align="middle", text="あいこで", font=self.font, color=(0, 0, 0), bgcolor=(255, 255, 255)) self.before_battle_sprite5 = TextSprite(*self.view_area.center, align="center", vertical_align="middle", text="しょ", font=self.font, color=(0, 0, 0), bgcolor=(255, 255, 255)) self.end_battle_sprite = TextSprite(*self.view_area.center, align="center", vertical_align="middle", text="そこまで", font=self.font, color=(0, 0, 0), bgcolor=(255, 255, 255)) # あいこ中フラグ self.pre_aiko = False
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
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:
def __init__(self, x: int, y: int, min_: int, max_: int, font: pygame.font.Font, front_group: pygame.sprite.Group, back_group: pygame.sprite.Group, color: Color=(0, 0, 0), bgcolor: Optional[Color]=None, sound: Optional[pygame.mixer.Sound]=None ): """(x, y)を中心に,カウンターを設置する. カウント範囲は[min_, max_]. """ self.min = min_ self.max = max_ self.count = min_ self.front_group = front_group self.back_group = back_group self.sound = sound self.press_rects = [] # カウンター表示のためのスプライトの生成 left = x right = x top = y self.sprites = [] for i in range(min_, max_+1): num_sprite = TextSprite( x=x, y=y, text=str(i), font=font, color=color, bgcolor=bgcolor, align="center", vertical_align="middle", ) self.sprites.append(num_sprite) left = min(left, num_sprite.rect.left) right = max(right, num_sprite.rect.right) top = min(top, num_sprite.rect.top) # 現在のスプライトを追加 self.front_group.add(self._get_sprite()) # 操作ボタン(左)の追加 self.left_btn_sprite = TextSprite( x=left, y=top, text="<", font=font, color=color, bgcolor=bgcolor, align="right", vertical_align="top", ) self.front_group.add(self.left_btn_sprite) self.press_rects.append(PressRect(self.left_btn_sprite.rect, self._count_down)) # 操作ボタン(右)の追加 self.right_btn_sprite = TextSprite( x=right, y=top, text=">", font=font, color=color, bgcolor=bgcolor, align="left", vertical_align="top", ) self.front_group.add(self.right_btn_sprite) self.press_rects.append(PressRect(self.right_btn_sprite.rect, self._count_up)) self.rect = Rect( self.left_btn_sprite.rect.left, self.left_btn_sprite.rect.top, self.right_btn_sprite.rect.right - self.left_btn_sprite.rect.left, self.left_btn_sprite.rect.height, )
def _set_text(self): self.text_sprite = TextSprite(0, 0, text=self.text, font=self.font) self.text_sprite.rect.center = self.baserect.center
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): 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)