class Fish_Catch(Fish_BgImage): def __init__(self,pos,size,path,fname,surface,catch): Fish_BgImage.__init__(self,pos,size,path,fname,surface) self.pos=pos self.signal=catch self.size=size self.path=path self.fname=fname self.screen=surface self.bgcb=imcombine(self.fname,self.path) self.bgim=StaticAniSprite(self.pos,self.bgcb,90,self.screen) self.count=0 self.catched='none' def fish_containsPoint(self,pos): left,top=self.pos width,height=self.size rect=pygame.Rect(left,top,width,height) return rect.collidepoint(pos) def fish_mouseOver(self): pass def fish_mouseLeft(self): pass def fish_mouseUp(self): pass def fish_mouseDown(self): self.catched='catched' def fish_random(self): self.pos=(randint(30,800/2.0),randint(30,500/2.0)) def fish_draw(self): if self.count<1: self.bgim.render() if self.bgim._frame==0: self.count+=1 elif self.count>0: self.fish_random() self.bgim=StaticAniSprite(self.pos,self.bgcb,90,self.screen) self.count=0 return 'end'
class Player(pygame.sprite.Sprite): # -- Attributes # Set speed vector of player change_x=0 change_y=0 # Triggered if the player wants to jump. jump_ready = False # Count of frames since the player hit 'jump' and we # collided against something. Used to prevent jumping # when we haven't hit anything. frame_since_collision = 0 frame_since_jump = 0 # -- Methods # Constructor function def __init__(self,x,y,isize,path,fname,screen): #screen size self.screen=screen #image size self.isize=isize # Call the parent's constructor pygame.sprite.Sprite.__init__(self) # Set height, width self.image = pygame.Surface([self.isize,self.isize]) #self.image.fill((red)) self.fname=fname self.path=path bgcb=imcombine(self.fname,self.path) self.bgim=StaticAniSprite(bgcb,15) self.bgim.render(self.image) # Make our top-left corner the passed-in location. self.rect = self.image.get_rect() self.rect.topleft = [x,y] # Change the speed of the player def changespeed_x(self,x): self.change_x = x def changespeed_y(self,y): self.change_y = y # Find a new position for the player def update(self,blocks): # Save the old x position, update, and see if we collided. old_x = self.rect.left print 'old_x:',old_x new_x = old_x + self.change_x print 'new_x',new_x if old_x!=new_x: self.bgim.render(self.image) self.rect.left = new_x collide = pygame.sprite.spritecollide (self, blocks, False) if collide: # We collided, go back to the old pre-collision location self.rect.left = old_x # Save the old y position, update, and see if we collided. old_y = self.rect.top new_y = old_y + self.change_y self.rect.top = new_y block_hit_list = pygame.sprite.spritecollide(self, blocks, False) for block in block_hit_list: # We collided. Set the old pre-collision location. self.rect.top = old_y self.rect.x = old_x # Stop our vertical movement self.change_y = 0 # Start counting frames since we hit something self.frame_since_collision = 0 # If the player recently asked to jump, and we have recently # had ground under our feet, go ahead and change the velocity # to send us upwards if self.frame_since_collision < 6 and self.frame_since_jump < 6: self.frame_since_jump = 100 self.change_y -= 8 # Increment frame counters self.frame_since_collision+=1 self.frame_since_jump+=1 # Calculate effect of gravity. def calc_grav(self): self.change_y += .35 # See if we are on the ground. if self.rect.top >= self.screen[1]-self.isize and self.change_y >= 0: self.change_y = 0 self.rect.top = self.screen[1]-self.isize self.frame_since_collision = 0 # Called when user hits 'jump' button def jump(self,blocks): self.jump_ready = True self.frame_since_jump = 0 self.bgim.render(self.image)