pygame老鼠过街-配音与封面版本

"""pygame老鼠过街-配音与封面版本,在川流不息的车流中,你需要操作一只小老鼠成功过街."""

import pygame
from pygame.locals import *
from random import randint
import time,sys

class Rat():
    def __init__(self,images,x,y,w,h):
        self.framesList = images        # 造型列表
        self.造型切换间隔时长=0.4       # 以秒为单位
        self.过关=False
        self.rect = pygame.Rect(x,y,w,h)
        self.造型编号=0
        self.begintime = time.time()
        
    def 下一个造型(self):
        if (time.time()-self.begintime) >= self.造型切换间隔时长:
            self.造型编号 = 1- self.造型编号              
            self.begintime = time.time()
            
    def up(self):         
        self.rect.y -= 10  

    def down(self):         
        self.rect.y += 10  
        
    def 过关检测(self):
        if self.rect.y<20:
            self.过关=True
        
    def draw(self):
        screen.blit(self.framesList[self.造型编号],self.rect)         

class GameObject():
    def __init__(self,framesRight,framesLeft,x,y,w,h):
        self.frames_right = framesRight
        self.frames_left = framesLeft
        self.造型数量 = len(self.frames_left)
        self.造型编号=0
        self.造型切换间隔时长=0.1    # 以秒为单位
        self.移动间隔时长=0.03       # 以秒为单位 
        self.rect = pygame.Rect(x,y,w,h)
        self.xspeed=randint(1,5)
        self.yspeed=0
        self.begintime = time.time()
        self.begintime2 = time.time()
        
    def 下一个造型(self):
        if (time.time()-self.begintime) >= self.造型切换间隔时长:
            self.造型编号 = self.造型编号 + 1
            self.造型编号 = self.造型编号 % self.造型数量
            self.begintime = time.time()
            
    def move(self):
        
        if (time.time()-self.begintime2) >= self.移动间隔时长:        
            self.rect.x += self.xspeed
            self.rect.y += self.yspeed 
            if self.rect.left<0 or self.rect.right>screen_width:
                self.xspeed = - self.xspeed
            self.begintime2 = time.time()
            
    def collide(self,rat):
        if self.rect.colliderect(rat.rect):
            return True
        else:
            return False        
        
    def draw(self):
        if self.xspeed>0:
            screen.blit(self.frames_right[self.造型编号],self.rect)
        else:            
            screen.blit(self.frames_left[self.造型编号],self.rect)
 
pygame.init()
screen_width,screen_height=480,360
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("pygame老鼠过街--作者:李兴球")
背景 = pygame.image.load("街道.png")
 
BellToll  = pygame.mixer.Sound("BellToll.wav")
Cricket  = pygame.mixer.Sound("Cricket.wav")
Cricket.play()
Laugh_male1 = pygame.mixer.Sound("Laugh-male1.wav")
pygame.mixer.music.load("欢快女唱电.wav")
pygame.mixer.music.play(-1,0)

rat0 = pygame.image.load("mouse1-a.png")
rat1 = pygame.image.load("mouse1-b.png")
rats = [rat0,rat1]
rat = Rat(rats,screen_width//2,screen_height-30,20,30)
frame0 = pygame.image.load("小汽车.png") 
framesRight = [frame0]
framesLeft = [pygame.transform.flip(f,True,False) for f in framesRight]

font = pygame.font.Font("c:/windows/fonts/msyh.ttf",30)
textstring = " "
textImage =font.render(textstring,True,(0,255,255))
(tx,ty,tw,th) = textImage.get_rect()
textpos=(screen_width//2 - tw //2,screen_height//2 - th/2 -100)
#新建一些小汽车
car1 = GameObject(framesRight,framesLeft,100,30,70,30)
car2 = GameObject(framesRight,framesLeft,200,160,70,30)
car3 = GameObject(framesRight,framesLeft,300,260,70,30)
car4 = GameObject(framesRight,framesLeft,400,200,70,30)
car5 = GameObject(framesRight,framesLeft,230,90,70,30)
cars = [ car1,car2,car3,car4,car5]

封面  = pygame.image.load("封面设计.png")
running = True
game_over = False
while running:
    for event in pygame.event.get():
        if event.type==QUIT:
            running=False
            game_over = True
        if event.type==KEYDOWN or event.type==MOUSEBUTTONDOWN:
            running=False
    screen.blit(封面,(0,0))
    pygame.display.update()

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