pygame闯关小游戏命悬一线之勇闯太空隧道.py

"""命悬一线之通闯太空隧道.py 这是用pygame制作一个小游戏,操作一个小猫闯关,碰撞检测用的是mask"""

import pygame
from pygame.locals import *

gamename = "《命悬一线之勇闯太空隧道》"
pygame.init()
pygame.mixer.init()
screenWidth ,screenHeight=480,360

screen = pygame.display.set_mode((screenWidth,screenHeight))
pygame.display.set_caption(gamename + "_作者:李兴球_mask碰撞实例_风火轮少儿编程")

failSound = pygame.mixer.Sound("Fail.wav")
hurtSound = pygame.mixer.Sound("hurt.wav")
succSound = pygame.mixer.Sound("小号胜利.wav")

def playmusic():
    pygame.mixer.music.load("TheAvengers.wav")
    pygame.mixer.music.play(-1,0)
    
def start_shell():
    """显示开始界面"""
    封面图= pygame.image.load("封面设计《命悬一线》.png")
    running = True
    while running:
        for event in pygame.event.get():
            if event.type==QUIT:pygame.quit()
            if event.type==KEYDOWN or event.type==MOUSEBUTTONDOWN:running = False
        screen.blit(封面图,(0,0))
        pygame.display.update()
    

class Ball():
    def __init__(self,imageRight,imageLeft,x,y):
         
        self.color = color
        self.imageRight = imageRight           # 向右造型
        self.imageLeft = imageLeft             # 向左造型     
        self.image = imageRight                # 初始为向右的图        
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.xspeed = 0
        self.yspeed = 0
        self.delete = 0
        self.mask = pygame.mask.from_surface(self.image) # 用于遮罩碰撞检测
        self.lifes = 1                                   # 生命个数
    def move(self,direction):
        if self.lifes>0:
            self.rect.move_ip(self.xspeed*direction,self.yspeed*direction)
 
    def draw(self):
        screen.blit(self.image,self.rect)

def main():
    # 以下字典用来描述每个关卡的出口,四元组分别表示上,下,左,右,值为0时,表时没有出口,值为1时,表示有出口.
    # 由于关卡号刚好从0,开始,所以用列表也可以.
    ed = {0:(0,0,0,1),1:(1,0,1,0),2:(0,1,0,1),3:(0,1,1,0),4:(1,0,0,0)}      # 表示每关出口标志,上下左右
    关卡号  = 0
    
    背景1 = pygame.image.load("背景1.png").convert_alpha()
    背景2 = pygame.image.load("背景2.png").convert_alpha()
    背景3 = pygame.image.load("背景3.png").convert_alpha()
    背景4 = pygame.image.load("背景4.png").convert_alpha()
    背景5 = pygame.image.load("背景5.png").convert_alpha()
    背景列表= [背景1,背景2,背景3,背景4,背景5]    
    背景mask = [pygame.mask.from_surface(背景) for 背景 in 背景列表]

    小飞猫 = pygame.image.load("飞猫.png").convert_alpha()
    小飞猫右 =pygame.transform.scale(小飞猫,(30,20))
    小飞猫左 =pygame.transform.flip(小飞猫右,True,False)
    
    ball = Ball(小飞猫右,小飞猫左,30,280)
    
    clock = pygame.time.Clock()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:pygame.quit()
        keys = pygame.key.get_pressed()
        if keys[K_RIGHT]:ball.xspeed = ball.xspeed + 0.1;ball.image = ball.imageRight
        if keys[K_LEFT]:ball.xspeed = ball.xspeed - 0.1;ball.image  = ball.imageLeft
        if keys[K_DOWN]:ball.yspeed = ball.yspeed + 0.1
        if keys[K_UP]:ball.yspeed = ball.yspeed - 0.1
        
        pass       
        
        ball.draw()
        pygame.display.update()
        clock.tick(60)

     # 停止背景音乐
    pygame.mixer.music.stop()
    笑脸图=pygame.image.load("笑脸.png")
    哭脸图=pygame.image.load("哭脸.png")
    f = pygame.font.Font("C:/windows/fonts/msyh.ttf",20)
    if ball.lifes==0 :       
        fontimage = f.render("游戏结束,你失败了!",True,(255,0,0))
        failSound.play()                                         # 播放失败音效
    else:
        fontimage = f.render("游戏结束,你成功了!",True,(255,255,125))
        succSound.play()                                         # 播放胜利音效
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:running=False
            if event.type ==MOUSEBUTTONDOWN:running=False
        screen.fill((0,0,0))
        if ball.lifes==0:
            screen.blit(哭脸图,(screenWidth//2-100,50))
        else:
            screen.blit(笑脸图,(screenWidth//2-80,50))
        screen.blit(fontimage,(screenWidth//2-100,80+screenHeight/2))
        pygame.display.update()
        
    pygame.quit()
    

if __name__ == "__main__":
    
    playmusic()
    start_shell()
    main()
    

 

如需要下载完整源代码及素材,请扫码付款:

VIP免费查看

已有0人支付

本站所有作品,教程等皆为原创,盗版必究。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。扫码付款后的朋友可以加QQ: 406273900,提供技术支持及发送作品图片与配音素材。亦可直接向微信号scratch8付款购买。
李兴球的博客_Python创意编程技术前沿 » pygame闯关小游戏命悬一线之勇闯太空隧道.py

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

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