pygame飞扬小鸟.py

"""pygame飞扬小鸟.py,这是本人制作的一个版本,角色没有继承pygame.sprite.Sprite."""

__author__ = "李兴球"
__date__ = "2018年7月"

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

pygame.init()
screenWidth,screenHeight=480,360
screen = pygame.display.set_mode((screenWidth,screenHeight))
pygame.display.set_caption("飞扬小鸟_李兴球版")

class Bird():
    def __init__(self,costumeList):
        self.costumeId = 0
        self.costumeList = costumeList
        self.cosutmeAmount = len(costumeList)
        self.image = self.costumeList[self.costumeId]
        self.rect = self.image.get_rect()
        self.rect.x = 100
        self.rect.y = 100
        self.xspeed = 0
        self.yspeed = 3
        self.aspeed = 0.5           # 加速度
        self.moveStartTime = time.time()
        self.costumeStartTime  =time.time()
        self.delete =0               # 删除标志
    def move(self):
        if time.time()- self.moveStartTime<0.1:
            self.rect.move_ip(self.xspeed,self.yspeed)
            self.yspeed = self.yspeed + self.aspeed
            self.moveStartTime = time.time()
            
    def 下一个造型(self):
        if time.time() - self.costumeStartTime<0.1:
            self.costumeId = self.costumeId + 1
            self.costumeId = self.costumeId % self.cosutmeAmount
            self.image = self.costumeList[self.costumeId]
            self.costumeStartTime = time.time()
    def draw(self):
        screen.blit(self.image,self.rect)
    def die(self,dieSound):
        self.delete=1
        self.xspeed = -5
        dieSound.play()

class Pipe():
    def __init__(self,image,t):
        self.type = t                    # t为up或者down,用于区分上管道和下管道
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.left = screenWidth+1
        self.sety()
        self.xspeed= -5
        self.yspeed = 0
        self.moveStartTime = time.time()
        self.delete = 0                 #待删除标志
    def sety(self):
        
        if self.type == "up":
            r1 = screenHeight//2-50
            r2 = screenHeight//2-100
            self.rect.bottom = randint(r2,r1)
        if self.type=="down":           #如果是下管道
            r1 = screenHeight//2+50
            r2 = screenHeight//2+100
            self.rect.y = randint(r1,r2)
        
    def move(self):
        if time.time()- self.moveStartTime<0.1:
            self.rect.move_ip(self.xspeed,self.yspeed)
            self.moveStartTime = time.time()

            #如果到了最右边,把它移到最左边,换个速度继续移动 
            if self.rect.right<=0:
                self.rect.left = screenWidth + 100
                self.sety()
    def collide(self,bird):
        if self.rect.colliderect(bird.rect):
            return True
        else:
            return False
        
    def draw(self):
        screen.blit(self.image,self.rect)

        
def 播放背景音乐():
    pygame.mixer.music.load("音乐.wav")
    pygame.mixer.music.play(-1,0)
    
def main():
    punch = pygame.mixer.Sound("punch.wav")
    过音= pygame.mixer.Sound("过.wav")
    
    上管道 = pygame.image.load("上管道.png")
    下管道 = pygame.image.load("下管道.png")
    
    背景= pygame.image.load("背景.png")
    cosList = []
    cosList.append(pygame.image.load("costume1.png"))
    cosList.append(pygame.image.load("costume2.png"))
    cosList.append(pygame.image.load("costume3.png"))
    cosList.append(pygame.image.load("costume4.png"))   
    
    clock = pygame.time.Clock()
    鸟 = Bird(cosList)
    pipeUp = Pipe(上管道,"up")
    pipeDown = Pipe(下管道,"down")

    fontType = pygame.font.Font("C:/windows/fonts/msyh.ttf",20)
    titleImage = fontType.render("飞扬小鸟V1.0版 作者:李兴球",True,(255,0,0))
    endImage = fontType.render("游戏结束!",True,(255,0,255))
    过关数 = 0
    levelString = fontType.render("过关数:" + str(过关数),True,(0,0,0))
    txt = [titleImage,endImage]
本文隐藏内容 登陆 后才可以浏览
pygame.quit() if __name__=="__main__": 播放背景音乐() main()