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)

You must log in to read the rest of this article. Please log in or register as a user.