def _update_high_score_list(self): high_score_rect = pygame.Rect(0, 0, config.screen_width // 3, self.high_score_rect.height) high_score_rect.top = config.screen_height // 8 high_score_rect.centerx = config.screen_width // 2 self.score_group.empty() # pad score list out to 10, if necessary padded_list = [ _Score("???", -1) for _ in range(10 - len(self.high_scores)) ] self.high_scores += padded_list score_counter = 1 rect = None for score in self.high_scores: image = self._render_high_score(config.screen_width // 2, score_counter, score) rect = rect or image.get_rect() rect.top = high_score_rect.bottom + 10 + (score_counter - 1) * (rect.height + 6) sprite = StaticAnimation(image) sprite.rect = rect.copy() sprite.rect.centerx = config.screen_rect.centerx self.score_group.add(sprite) score_counter += 1
def __init__(self, img1, x1, y1, img2=None, x2=None, y2=None): Sprite.__init__(self) self.prevDist = None self.img1 = None self.img2 = None #corrector for gaps - there are still gaps though -_- self.gapCorrector = 5 #corrector for loading levels self.loadCorrector = 900 self.totalx = 0 self.prevx = None self.dx = 0 if img1: self.img1 = StaticAnimation(img1) self.rect1 = self.img1.get_rect() self.rect1.topleft = (x1, y1) #copy of the first image for when img1 is done self.x1copy = False self.rect1copy = self.img1.get_rect() self.rect1copy.topleft = (x1, y1) if img2: self.img2 = StaticAnimation(img2) self.rect2 = self.img2.get_rect() self.rect2.topleft = (x2, y2) #copy of the second image for when img2 is done self.x2copy = False self.rect2copy = self.img2.get_rect() self.rect2.topleft = (x2, y2)
def __init__(self, img1,x1,y1, img2=None,x2=None,y2=None): Sprite.__init__(self) self.prevDist = None self.img1 = None self.img2 = None #corrector for gaps - there are still gaps though -_- self.gapCorrector = 5 #corrector for loading levels self.loadCorrector = 900 self.totalx = 0 self.prevx = None self.dx = 0 if img1: self.img1 = StaticAnimation(img1) self.rect1 = self.img1.get_rect() self.rect1.topleft = (x1,y1) #copy of the first image for when img1 is done self.x1copy = False self.rect1copy = self.img1.get_rect() self.rect1copy.topleft = (x1,y1) if img2: self.img2 = StaticAnimation(img2) self.rect2 = self.img2.get_rect() self.rect2.topleft = (x2,y2) #copy of the second image for when img2 is done self.x2copy = False self.rect2copy = self.img2.get_rect() self.rect2.topleft = (x2,y2)
class LevelObject(Sprite): base_img_path = None can_get_hurt = False can_give_hurt = False solid = True #can object teleport player? #if true, then must implement teleport() teleporter = False #direction to enter teleport object's 'portal' teleportDir = DOWN def __init__(self, x, y, velX=0, velY=0, xmult=0, ymult=0): Sprite.__init__(self) #optional terrain velocities self.velX = velX self.velY = velY #optional terrain velocity multipliers self.xmult = xmult self.ymult = ymult #If this isn't set we assume the base class has already loaded its image if self.base_img_path: self.anim = StaticAnimation(self.base_img_path) self.rect = self.anim.get_rect() self.rect.topleft = (x, y) #implement in specific classes def update(self): pass #implement in specific classes def handleNodeCollision(self, node): pass def draw(self, camera): image = self.anim.get_image() if image != None: camera.draw(image, self.rect) def get_rect(self): return self.rect def try_hurt(self, by): if self.can_get_hurt and by.can_give_hurt: self.got_hurt(by) elif by.can_get_hurt and self.can_give_hurt: by.got_hurt(self) def die(self): pass
class LevelObject(Sprite): base_img_path = None can_get_hurt = False can_give_hurt = False solid = True #can object teleport player? #if true, then must implement teleport() teleporter = False #direction to enter teleport object's 'portal' teleportDir = DOWN def __init__(self,x,y,velX=0,velY=0,xmult=0,ymult=0): Sprite.__init__(self) #optional terrain velocities self.velX = velX self.velY = velY #optional terrain velocity multipliers self.xmult = xmult self.ymult = ymult #If this isn't set we assume the base class has already loaded its image if self.base_img_path: self.anim = StaticAnimation( self.base_img_path ) self.rect = self.anim.get_rect() self.rect.topleft = (x,y) #implement in specific classes def update(self): pass #implement in specific classes def handleNodeCollision(self, node): pass def draw(self,camera): image = self.anim.get_image() if image != None: camera.draw(image,self.rect) def get_rect(self): return self.rect def try_hurt(self,by): if self.can_get_hurt and by.can_give_hurt: self.got_hurt(by) elif by.can_get_hurt and self.can_give_hurt: by.got_hurt(self) def die(self): pass
def __init__(self, x, y, velX=0, velY=0, xmult=0, ymult=0): Sprite.__init__(self) #optional terrain velocities self.velX = velX self.velY = velY #optional terrain velocity multipliers self.xmult = xmult self.ymult = ymult #If this isn't set we assume the base class has already loaded its image if self.base_img_path: self.anim = StaticAnimation(self.base_img_path) self.rect = self.anim.get_rect() self.rect.topleft = (x, y)
def __init__(self, input_state, game_stats: SessionStats, starfield=None): super().__init__(input_state) self.stats = game_stats self.font = pygame.font.SysFont(None, 48) self.prompt_group = pygame.sprite.Group() self.high_score_state = HighScore(self.input_state) self.starfield = starfield or Starfield() new_high_score = self.font.render("New high score!", True, config.text_color) new_high_score = StaticAnimation(new_high_score) new_high_score.rect.centerx = config.screen_rect.centerx new_high_score.rect.top = 50 self.prompt_group.add(new_high_score) self.entered_name_image = None self.entered_name_rect = None self.entered_name = "" self._update_name_image() self.entered_name_rect.centerx = new_high_score.rect.centerx self.entered_name_rect.top = new_high_score.rect.bottom + 10 # if this is not a new high score, this state will just transition directly to displaying the # high score list self.done = not self.high_score_state.is_new_high_score( game_stats.score)
def initialize_static(self, name, color_key=None, override_width=None, override_height=None): rect = self._fetch(name, self.sprite_rects) if override_width or override_height: rect = rect.copy() # don't affect original dimensions rect.width = override_width or rect.width rect.height = override_height or rect.height assert 0 <= rect.width <= self.atlas.get_width(), "width out of range" assert 0 <= rect.height <= self.atlas.get_height( ), "height out of range" assert 0 <= rect.x <= self.atlas.get_width( ) - rect.width, "x position out of range" assert 0 <= rect.y <= self.atlas.get_height( ) - rect.height, "y position out of range" surf = self.atlas.subsurface(rect) if color_key is not None: surf = surf.convert() surf.set_colorkey(color_key) self.statics[name] = StaticAnimation(surf)
def create_corpse_animation(animation): # creates a simple corpse by flipping current animation frame current_frame = animation.image corpse = pygame.transform.flip(current_frame, False, True) return StaticAnimation(corpse)
def _create_point_sprite(self, alien_surf, point_value, top_coord): point_value = self.font.render("{} Points".format(point_value), True, config.text_color) alien_rect = alien_surf.get_rect() surf = pygame.Surface((alien_rect.width + point_value.get_width() + 20, max(alien_rect.height, point_value.get_height()))) surf = surf.convert_alpha(pygame.display.get_surface() ) # want 32 bit surface for per-pixel alpha surf.fill((0, 0, 0, 0)) # fill with transparent, note no color key # center alien rect vertically alien_rect.centery = surf.get_height() // 2 surf.blit(alien_surf, alien_rect) r = point_value.get_rect() r.left = alien_rect.width + 20 r.centery = alien_rect.centery surf.blit(point_value, r) sprite = StaticAnimation(surf) sprite.rect.top = top_coord sprite.rect.centerx = config.screen_rect.centerx return sprite
def __populate_image_variables(self): animd = self.animFolder self.norm_attack = StaticAnimation('images/' + animd + '/norm_attack_left.gif'),\ StaticAnimation('images/' + animd + '/norm_attack_right.gif') self.jump_attack = StaticAnimation('images/' + animd + '/jump_attack_left.gif'),\ StaticAnimation('images/' + animd + '/jump_attack_right.gif') #self.spec_attack = StaticAnimation(''),\ # StaticAnimation('') self.jump = StaticAnimation('images/' + animd + '/jump_left.gif'),\ StaticAnimation('images/' + animd + '/jump_right.gif') self.stand = StaticAnimation('images/' + animd + '/stand_left.gif'),\ StaticAnimation('images/' + animd + '/stand_right.gif') self.walk = Animation('images/' + animd + '/move_left{0}.gif', self.numWalkFrames, self.walkDelay ),\ Animation('images/' + animd + '/move_right{0}.gif', self.numWalkFrames, self.walkDelay )
def __init__(self,x,y,velX=0,velY=0,xmult=0,ymult=0): Sprite.__init__(self) #optional terrain velocities self.velX = velX self.velY = velY #optional terrain velocity multipliers self.xmult = xmult self.ymult = ymult #If this isn't set we assume the base class has already loaded its image if self.base_img_path: self.anim = StaticAnimation( self.base_img_path ) self.rect = self.anim.get_rect() self.rect.topleft = (x,y)
def __init__(self, input_state): super().__init__(input_state) self.next_state = None self.game = RunGame(self.input_state) dialog_surf = util.create_dialog("Click to Begin!", config.text_color, config.green_color, (0, 0, 0), 48, config.screen_width // 4, 64) dialog_surf_mo = util.create_dialog("Begin!", (0, 0, 0), config.text_color, (0, 0, 0), 48, dialog_surf.get_width(), dialog_surf.get_height()) self.dialog = StaticAnimation([dialog_surf, dialog_surf_mo]) self.dialog.rect.center = config.screen_rect.center
def __init__(self, level, points): if isinstance(points, int): points = str(points) global floaty_font from animation import StaticAnimation # lazy load font floaty_font = floaty_font or pygame.font.Font( "scoring/super_mario_font.ttf", 12) frame = floaty_font.render(points, True, pygame.Color('white')) animation = StaticAnimation(frame) super().__init__(level, animation, floaty_parameters, FloatyPoints.DURATION, True)
def __init__(self, level, direction, shell_animation): self.level = level self.sounds = level.asset_manager.sounds from animation import StaticAnimation self.shell = StaticAnimation(shell_animation.image) super().__init__(self.shell, level, deadly_shell_parameters, (1, 1), (14, 14), constants.Block, constants.Enemy | constants.Mario) self.movement.velocity = make_vector( math.copysign(deadly_shell_parameters.max_horizontal_velocity, direction), 0.) # state self._reverse = False
class Parallax(Sprite): #x and y are the topleft coords for their corresponding img #img is the path of the actual image #images are from farthest to closest def __init__(self, img1,x1,y1, img2=None,x2=None,y2=None): Sprite.__init__(self) self.prevDist = None self.img1 = None self.img2 = None #corrector for gaps - there are still gaps though -_- self.gapCorrector = 5 #corrector for loading levels self.loadCorrector = 900 self.totalx = 0 self.prevx = None self.dx = 0 if img1: self.img1 = StaticAnimation(img1) self.rect1 = self.img1.get_rect() self.rect1.topleft = (x1,y1) #copy of the first image for when img1 is done self.x1copy = False self.rect1copy = self.img1.get_rect() self.rect1copy.topleft = (x1,y1) if img2: self.img2 = StaticAnimation(img2) self.rect2 = self.img2.get_rect() self.rect2.topleft = (x2,y2) #copy of the second image for when img2 is done self.x2copy = False self.rect2copy = self.img2.get_rect() self.rect2.topleft = (x2,y2) # x = player.rect.x y = player.rect.y def update(self,x,y): #init values for starting/loading a level if not self.prevx: self.prevx = x #need to start parallax in the right-ish place when loading a level if self.img1: self.rect1.x = x - self.loadCorrector if self.img2: self.rect2.x = x - self.loadCorrector else: #store partial moves self.dx += x - self.prevx #calculate move amount amount1 = -floor(self.dx/10) #move images if self.rect1: self.rect1.move_ip(amount1,0) if self.x1copy: self.rect1copy.move_ip(amount1,0) if self.rect2: self.rect2.move_ip(-1,0) if self.x2copy: self.rect2copy.move_ip(-1,0) #reset? self.dx %= 10 #save current x self.prevx = x #bg images need to be looped on right side if x + SCREEN_WIDTH >= self.rect1.right and not self.x1copy: self.x1copy = True self.rect1copy.x = x + SCREEN_WIDTH - self.gapCorrector if x + SCREEN_WIDTH >= self.rect2.right and not self.x2copy: self.x2copy = True self.rect2copy.x = x + SCREEN_WIDTH - self.gapCorrector """ assumption is that player won't run backwards.. #bg images need to be looped on left side if not self.x1copy and x - SCREEN_WIDTH <= self.rect1.left: self.x1copy = True self.rect1copy.x = x - (SCREEN_WIDTH + self.rect1copy.width) if not self.x2copy and x - SCREEN_WIDTH <= self.rect2.left: self.x2copy = True self.rect2copy.x = x - (SCREEN_WIDTH + self.rect1copy.width) """ #player passed the original bg image - going right #make the original image's rect the copy's if x - SCREEN_WIDTH > self.rect1.right and self.x1copy: self.rect1.x = self.rect1copy.x self.x1copy = False if x - SCREEN_WIDTH > self.rect2.right and self.x2copy: self.rect2.x = self.rect2copy.x self.x2copy = False """ assumption is that player won't run backwards #player has passed the original bg image - going left #make the original image's rect the copy's if x + SCREEN_WIDTH < self.rect1.left and self.x1copy: self.rect1.x = self.rect1copy.x self.x1copy = False if x + SCREEN_WIDTH < self.rect2.left and self.x2copy: self.x2 = self.x2copy self.rect2.x = self.rect2copy.x self.x2copy = False """ #TODO #free copy image - ex: unseen on left side, unseen on right def draw(self,camera): #draw main images image1 = self.img1.get_image() if image1: camera.draw(image1,self.rect1) image2 = self.img2.get_image() if image2: camera.draw(image2,self.rect2) #draw copies if needed if self.x1copy: camera.draw(image1,self.rect1copy) if self.x2copy: camera.draw(image2,self.rect2copy)
class Parallax(Sprite): #x and y are the topleft coords for their corresponding img #img is the path of the actual image #images are from farthest to closest def __init__(self, img1, x1, y1, img2=None, x2=None, y2=None): Sprite.__init__(self) self.prevDist = None self.img1 = None self.img2 = None #corrector for gaps - there are still gaps though -_- self.gapCorrector = 5 #corrector for loading levels self.loadCorrector = 900 self.totalx = 0 self.prevx = None self.dx = 0 if img1: self.img1 = StaticAnimation(img1) self.rect1 = self.img1.get_rect() self.rect1.topleft = (x1, y1) #copy of the first image for when img1 is done self.x1copy = False self.rect1copy = self.img1.get_rect() self.rect1copy.topleft = (x1, y1) if img2: self.img2 = StaticAnimation(img2) self.rect2 = self.img2.get_rect() self.rect2.topleft = (x2, y2) #copy of the second image for when img2 is done self.x2copy = False self.rect2copy = self.img2.get_rect() self.rect2.topleft = (x2, y2) # x = player.rect.x y = player.rect.y def update(self, x, y): #init values for starting/loading a level if not self.prevx: self.prevx = x #need to start parallax in the right-ish place when loading a level if self.img1: self.rect1.x = x - self.loadCorrector if self.img2: self.rect2.x = x - self.loadCorrector else: #store partial moves self.dx += x - self.prevx #calculate move amount amount1 = -floor(self.dx / 10) #move images if self.rect1: self.rect1.move_ip(amount1, 0) if self.x1copy: self.rect1copy.move_ip(amount1, 0) if self.rect2: self.rect2.move_ip(-1, 0) if self.x2copy: self.rect2copy.move_ip(-1, 0) #reset? self.dx %= 10 #save current x self.prevx = x #bg images need to be looped on right side if x + SCREEN_WIDTH >= self.rect1.right and not self.x1copy: self.x1copy = True self.rect1copy.x = x + SCREEN_WIDTH - self.gapCorrector if x + SCREEN_WIDTH >= self.rect2.right and not self.x2copy: self.x2copy = True self.rect2copy.x = x + SCREEN_WIDTH - self.gapCorrector """ assumption is that player won't run backwards.. #bg images need to be looped on left side if not self.x1copy and x - SCREEN_WIDTH <= self.rect1.left: self.x1copy = True self.rect1copy.x = x - (SCREEN_WIDTH + self.rect1copy.width) if not self.x2copy and x - SCREEN_WIDTH <= self.rect2.left: self.x2copy = True self.rect2copy.x = x - (SCREEN_WIDTH + self.rect1copy.width) """ #player passed the original bg image - going right #make the original image's rect the copy's if x - SCREEN_WIDTH > self.rect1.right and self.x1copy: self.rect1.x = self.rect1copy.x self.x1copy = False if x - SCREEN_WIDTH > self.rect2.right and self.x2copy: self.rect2.x = self.rect2copy.x self.x2copy = False """ assumption is that player won't run backwards #player has passed the original bg image - going left #make the original image's rect the copy's if x + SCREEN_WIDTH < self.rect1.left and self.x1copy: self.rect1.x = self.rect1copy.x self.x1copy = False if x + SCREEN_WIDTH < self.rect2.left and self.x2copy: self.x2 = self.x2copy self.rect2.x = self.rect2copy.x self.x2copy = False """ #TODO #free copy image - ex: unseen on left side, unseen on right def draw(self, camera): #draw main images image1 = self.img1.get_image() if image1: camera.draw(image1, self.rect1) image2 = self.img2.get_image() if image2: camera.draw(image2, self.rect2) #draw copies if needed if self.x1copy: camera.draw(image1, self.rect1copy) if self.x2copy: camera.draw(image2, self.rect2copy)
def __init__(self, input_state, starfield=None): super().__init__(input_state) self.font = pygame.font.SysFont(None, Menu.TitleSize) self.starfield = starfield or Starfield() self.title = Group() # create "Space Invaders" logo # green "SPACE" space_title = StaticAnimation( self.font.render("SPACE", True, config.green_color)) space_title.rect.centerx = config.screen_rect.centerx space_title.rect.centery = config.screen_height // 8 # white "INVADERS" self.font = pygame.font.SysFont(None, Menu.TitleSize // 2) invaders_title = StaticAnimation( self.font.render("INVADERS", True, pygame.Color('white'))) invaders_title.rect.left = space_title.rect.left + space_title.rect.width // 8 invaders_title.rect.top = space_title.rect.bottom + 10 self.title.add(space_title, invaders_title) last_y = config.screen_height - config.screen_height // 3 self.options = Group() self.font = pygame.font.SysFont(None, Menu.MenuItemSize) # create options for option in [("Play Space Invaders", self._play_game), ("High Scores", self._view_high_scores), ("Quit", self._quit)]: option_sprite = StaticAnimation( self.font.render(option[0], True, config.text_color)) option_sprite.callback = option[1] option_sprite.rect.centerx = config.screen_width // 2 option_sprite.rect.top = last_y last_y = option_sprite.rect.bottom + Menu.MenuItemSpacing self.options.add(option_sprite) # create a small sprite to use as menu item selector left_selector = config.atlas.load_static("selector") # need to flip the arrow... right_selector = StaticAnimation( pygame.transform.flip(left_selector.image, True, False)) right_selector.rect.left = config.screen_width self.selectors = Group(left_selector, right_selector) # point values for aliens self.aliens = Group() y_pos = invaders_title.rect.bottom + 50 for alien_stats in config.alien_stats: alien = config.atlas.load_animation( alien_stats.sprite_name).frames[0] spr = self._create_point_sprite(alien, alien_stats.points, y_pos) self.aliens.add(spr) y_pos = spr.rect.bottom + 10 ufo = config.atlas.load_animation("ufo").frames[0] spr = self._create_point_sprite(ufo, "???", y_pos) self.aliens.add(spr) # finish creating state values self.next_state = None self._set_selected(0) pygame.mouse.set_visible(True) sounds.play_music(sounds.menu_music_name)
def initialize_static_from_surface(self, name, surf): self.statics[name] = StaticAnimation(surf)