pygame跳跳猫类超级玛丽接金币多关卡解迷游戏

python like super mario game跳跳猫类超级玛丽游戏
python like super mario game跳跳猫类超级玛丽游戏

和超级玛丽有点像的接金币小游戏,以下是部分代码预览:

"""跳跳猫接金币多关卡解迷游戏,本程序给上一个版本加上Coin类。
   本程序设计有10多个关卡,有些金币需要动脑筋才能接收到。"""

__author__ = "李兴球"
__date__ = "2018/12/18"

import os
import pygame
from pygame.locals import *

class Coin():
    def __init__(self,images,position,player,level_number,sound):
        self.images = [pygame.image.load(image).convert_alpha() for image in images]
        self.images = [pygame.transform.scale(image,(int(20 * image.get_width()/image.get_height()),40)) for image in self.images]
        self.position = position        
        self.current_costume = self.images[0]             # 当前造型
        self.costume_amounts = len(self.images)           # 造型数量,注意每个图片的矩形对象不一样
        self.rects = [ costume.get_rect() for costume in self.images]  # 所有造型的矩形对象        
        self.rect = self.current_costume.get_rect()       # 第一个造型的矩形对象
        self.rect.center = position                       # 第一个造型的设定坐标
        self.player = player                              # 可访问玩家对象
        self.level_number = level_number                  # 每枚金币能记住自己的关卡号,不是它的关卡,则自我死亡
        self.screen = self.player.screen                  # 屏幕对象
        self.sound = sound                                # 金币声
        self.dead = False
        self.index = 0
        self.switch_costume_interval  = 2                 # 切换造型间隔
        self.switch_costume_counter  = 0                  # 切换造型计数器
    def next_costume(self):
        if not self.dead:
           self.switch_costume_counter = self.switch_costume_counter + 1
           if self.switch_costume_counter > self.switch_costume_interval:              
              self.index = self.index + 1
              self.index = self.index % self.costume_amounts
              self.switch_costume_counter = 0
              self.current_costume = self.images[self.index]
              self.rect = self.rects[self.index]                 # 矩形对象
              self.rect.center = self.position                  # 重新设定坐标
              
    def level_check(self,current_level_number):
        """关卡号不对,则自杀"""
        if self.level_number != current_level_number:
            self.dead = True
            
    def draw(self):
        if not self.dead:
            self.screen.blit(self.current_costume,self.rect)

    def collide_player_check(self):
        if self.rect.colliderect(self.player.rect) and not self.dead:
            self.player.coins += 1
            self.dead = True
            try:
               self.sound.play()
            except:
                pass        
        
class Detector(pygame.sprite.Sprite):
    def __init__(self,screen):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        self.image = pygame.Surface((20,20)).convert_alpha()
        self.image.fill((0,155,123))
        self.rect = self.image.get_rect()
        self.mask = pygame.mask.from_surface(self.image)
            
class Player(pygame.sprite.Sprite):
    """玩家类"""
    def __init__(self,image,screen,level,detectors):
        """image:图像
           screen:屏幕
           level:关卡对象
           detectors:左,右,上,下碰撞检测器
        """
        pygame.sprite.Sprite.__init__(self)
        self.lifes = 3                                        # 有三条生命
        self.image = pygame.image.load(image).convert_alpha() # 小猫的造型
        self.image.set_colorkey((255,255,255))          # 设置不渲染的颜色
        self.rect = self.image.get_rect()               # 设置矩形对象,表示坐标与大小的
        self.screen = screen                            # 设置这个属性以便能访问screen  
        self.levels = levels                            # 所有关卡
        self.levels_index = 0                           # 当前关卡索引号,可以在这里直接设定起始关卡
        self.current_level = levels[self.levels_index]  # 当前关卡        
        self.level_amounts = len(levels)                # 关卡总数量
        self.detectors = detectors                      # 4个侦测器,是个列表
        self.screen_width = screen.get_width()          # 以便能访问屏幕宽度
        self.screen_height = screen.get_height()        # 设这个属性以便能访问屏幕高度
        self.xspeed = 0                                 # 水平速度
        self.yspeed = 0                                 # 垂直速度
        self.aspeed = 0.5                               # 加速度
        self.rect.centerx = self.screen_width//2        # 小猫初始的x中间坐标
        self.rect.centery = self.screen_height//2 - 100 # 小猫初始的y中间坐标
        self.__is_in_the_air = True                     # 描述是否在空中的变量

        self.mask = pygame.mask.from_surface(self.image) # 新建掩膜mask,用于碰撞检测        
        self.cantoleft = True
        self.cantoright = True
        "新增加的金币数量属性"
        self.coins = 0
        
    def update(self):        
        """模拟重力的坐标更新"""
        self.rect.move_ip(self.xspeed,self.yspeed)        
        self.yspeed = self.yspeed + self.aspeed
        
    def dead_check(self):
        if self.rect.top > self.screen_height:
            self.lifes -= 1
            self.rect.centerx = 50
            self.rect.centery = self.screen_height//2 - 100
            self.xspeed = 0 
            self.yspeed = 0
        return self.lifes
        
    def bump_obstacle_check(self):        
        """碰地形(障碍物)检测,小猫自带4个侦测器,用它们进行检测即可。"""
        pass
            
    def jump(self):
         
        if not self.__is_in_the_air:
           self.__is_in_the_air = True           
           self.yspeed = -11.5           
           
    def move_left(self):
        if self.cantoleft:self.xspeed = -5
        
    def move_right(self):
        if self.cantoright:self.xspeed = 5
        
    def stop_move(self):
        self.xspeed = 0        
        
    def draw(self):
        self.screen.blit(self.image,self.rect)
        
    def level_check(self):        
        """过关检测,坐标判断"""        
        if self.rect.right > self.screen_width:              # 超过右边缘,下一关                        
            self.levels_index = self.levels_index + 1                     
            if self.levels_index >= self.level_amounts:      # 关卡结束
                return False,self.levels_index                
            else:
                self.next_level(self.levels[self.levels_index])
                return True,self.levels_index  
                
        if self.rect.left < 0 :                               # 超过左边缘,上一关                        
            self.levels_index = self.levels_index - 1                    
            if self.levels_index < 0:                         # 关卡结束
                return False,self.levels_index                  
            else:
                self.previous_level(self.levels[self.levels_index])
                return True,self.levels_index
        return True,self.levels_index
    
    def next_level(self,level):
        self.rect.left = 10
        self.current_level = level
        
    def previous_level(self,level):
        self.rect.right = self.screen_width
        self.current_level = level                
        
class Level(pygame.sprite.Sprite):
    def __init__(self,image,screen):
        self.image = pygame.image.load(image).convert_alpha()         
        self.rect = self.image.get_rect()
        self.mask = pygame.mask.from_surface(self.image)
        self.rect.top = screen.get_height() - self.image.get_height()

def detectors_follow_cat():
    """跟随小猫有左右上下四个小方块,用来对小猫的移动进行检测"""
    detectors[0].rect.left = cat.rect.left                  # 左侦测器x坐标
    detectors[0].rect.centery = cat.rect.centery
    detectors[1].rect.right = cat.rect.right                # 右侦测器x坐标
    detectors[1].rect.centery = cat.rect.centery
    detectors[2].rect.centerx = cat.rect.centerx            # 上侦测器x坐标
    detectors[2].rect.top = cat.rect.top
    detectors[3].rect.centerx = cat.rect.centerx            # 下侦测器x坐标
    detectors[3].rect.bottom = cat.rect.bottom          

if __name__ == "__main__":

    game_title = "跳跳猫接金币多关卡解迷游戏_作者:李兴球,风火轮编程出品 www.scratch8.net"
    backgrounds_png = ["BG" + str(i+1) + ".png" for i in range(14)]
    levels_png = ["Level" + str(i+1) + ".png" for i in range(14)]
    cat_image = "catx.png"
    width,height = 960,720

    pass
        
        

 

如需要查看完整代码,请扫码付款:

VIP免费查看

已有0人支付

本站所有作品,教程等皆为原创,盗版必究。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。QQ群免费教学群:225792826 (提供真实姓名可入群)
李兴球的博客_Python创意编程技术前沿 » pygame跳跳猫类超级玛丽接金币多关卡解迷游戏

学本领,探索更大的世界!

李兴球博客 风火轮编程主页