pygame鼠闯迷宫闯关小游戏

pygame mouse maze game 鼠闯迷宫pygame鼠标牵引游戏
pygame mouse maze game 鼠闯迷宫pygame鼠标牵引游戏

以下是部分代码预览:

"""鼠闯迷宫闯关小游戏.一只老鼠在一个巨大的迷宫中,它要出去才不致于被饿死,用鼠标牵引它移动即可。
碰撞使用的是mask,老鼠是相对于迷宫的移动(迷宫在动)"""

__author__ = "李兴球"
__date__ = "2018年12月左右"

import os,math,sys
import pygame
from pygame.locals import *

class Maze(pygame.sprite.Sprite):
    def __init__(self,image,scale,screen):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        pass
        
    def update(self,dx,dy):
        """迷宫朝相反的dx和dy移动"""
        self.rect.move_ip(dx*0.05,dy*0.05)      
        
    def draw(self):
        self.screen.blit(self.image,self.rect)
        
class Mouse(pygame.sprite.Sprite):
    def __init__(self,image,position,screen,maze=None):
        pygame.sprite.Sprite.__init__(self)
        self.position = position                                  # 原始坐标
        self.screen = screen
        self.maze = maze                                          # 引用当前的迷宫对象
        pass
        
    def distance(self,mousexy):
        """到鼠标指针的距离"""
        dy = mousexy[1] - self.rect.centery
        dx = mousexy[0] - self.rect.centerx
        return math.sqrt(dx * dx  + dy * dy)        
        
    def update(self,mousexy,logic):
        """根据方向角度更新图像"""
        if self.distance(mousexy) > 50 :
            self.image = pygame.transform.rotate(self.raw_image,self.angle).convert_alpha()
            self.image.set_colorkey((0,0,0))
            self.mask = pygame.mask.from_surface(self.image)        # 设定掩膜属性,以后用于和迷宫mask的碰撞检测
            pass                  
            
    def bump_check(self):
        """对碰黑墙和碰绿门进行碰撞检测,实际上是对maze进行通过mask的碰撞检测,返回point坐标。
        ,再侦测像素值就知道是碰到门还是碰到绿色"""
        level_end = False
        point = pygame.sprite.collide_mask(self.maze,self)
        pass
    def draw(self):
        self.screen.blit(self.image,self.rect)

       
def display_shell(screensize,shell_image,begin_button_images):
    width,height = screensize
    index = 0
    button_image = begin_button_images[index]
    
    button_rect = button_image.get_rect()
    button_width = button_rect.width
    button_height = button_rect.height
    button_rect.center = (width//2,100+height//2)
    clock = pygame.time.Clock()
    running = True
    continue_game = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
                pass
        pygame.display.update()
        clock.tick(30)
    
    return continue_game

def display_end(end_image):
    clock = pygame.time.Clock()
    running = True    
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False              
                break
        mousekeys = pygame.mouse.get_pressed()        
        if mousekeys[0] : running = False
            
        screen.blit(end_image,(0,0))                    
        pygame.display.update()
        clock.tick(30)       
    
    
if __name__ == "__main__":
    
    pass


        

 

下载完整源代码与素材,请扫码付款。

VIP免费查看

已有0人支付

本站所有作品,教程等皆为原创,版权所有。只供个人及单位内部研究使用,对外展示或传播必需经本站同意,且注明来自本站。培训机构等用本站资源培训学生,需经本站授权。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。入住QQ群:225792826 和爱好者共同交流,并且能下载免费提供的Python资源(需提供真实姓名才可入群)
李兴球的博客_Python创意编程技术前沿_pygame » pygame鼠闯迷宫闯关小游戏
scratch

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

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