pygame小鸟捕手.py_飞扬小鸟变种趣味小游戏

"""小鸟捕手.py 这是飞扬小鸟变种小游戏,在这个游戏中不再是帮助小鸟飞了,而是按空格键操作管道去"捕"小鸟"""
__author__ = "李兴球"
__date__ = "2018年6月"

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

class Button():
    def __init__(self,images,position):
        self.images = images              # 造型列表     
        self.index=0                      # 造型索引号
        self.image = images[self.index]
        self.rect = self.image.get_rect()
        self.rect.center = position
        self.cursor_in= False           # 鼠标指针是否在按钮矩形内
        
    def contain_point(self,point):
        """判断某点是否在矩形范围内"""
        if self.rect.collidepoint(point) :           
            self.cursor_in=True
            self.index=0
            self.image = self.images[self.index] 
                                    
        else: 
            self.cursor_in= False
            self.index=1
            self.image = self.images[self.index] 
             
    def draw(self):        
        screen.blit(self.image,self.rect) 

class Bird():
    """定义鸟类"""
    pass              # 相信读者根据上下文可以写出它的代码:)
        
class Pipe():
    def __init__(self,图像,x,y,yspeed):
        self.image = 图像
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.yspeed = yspeed
        self.move_counts = 0          #按空格键时让它的值为10
    def move(self):
        if self.move_counts >0:
           self.rect.move_ip(0,self.yspeed)
           self.move_counts = self.move_counts  - 1
           if self.move_counts ==5:   # 移动次数为5就反向移动
               self.yspeed = - self.yspeed
    def draw(self):
        screen.blit(self.image,self.rect)
        
screen_size = (480,360)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("小鸟捕手_作者:李兴球_萍乡风火轮少儿编程")
background = pygame.image.load("backdrop2.png")
开始按钮0 = pygame.image.load("开始按钮0.png")
开始按钮1 = pygame.image.load("开始按钮1.png")
按钮们 = [开始按钮0,开始按钮1]
 
鸟0 = pygame.image.load("鸟0.png")
鸟1 = pygame.image.load("鸟1.png")
鸟2 = pygame.image.load("鸟2.png")
上管道 = pygame.image.load("上管道.png")
下管道 = pygame.image.load("下管道.png")

开始按钮 = Button(按钮们, (screen_size[0]//2,screen_size[1]//2)) 
        
pygame.mixer.init()
pygame.mixer.music.load("Itty Bitty 8 Bit.wav")
pygame.mixer.music.play(-1,0)
捕声效 = pygame.mixer.Sound("footstep grass.wav")

pygame.font.init()
font = pygame.font.Font("c:/windows/fonts/msyh.ttf",30)
fontSurface = font.render("小鸟捕手",True,(255,0,0))
开始界面= True
inex = 0                    #按钮索引
while 开始界面:
    for event in pygame.event.get():
        if event.type ==QUIT:
            开始界面 = False
        if event.type ==MOUSEBUTTONDOWN:
            if 开始按钮.cursor_in:开始界面 = False                
            
    鼠标位置 = pygame.mouse.get_pos()
    开始按钮.contain_point(鼠标位置)  
    screen.blit(background,(0,0))
    screen.blit(fontSurface,(200,50))
    开始按钮.draw()
    pygame.display.update()
    
print("进入游戏中...")

bird_images = [鸟0,鸟1,鸟2]
pipe_up = Pipe(上管道,400,0,5)        
鸟 = Bird(bird_images,0,180)

clock = pygame.time.Clock()
游戏中 = True
while 游戏中:
    for event in pygame.event.get():
        if event.type ==QUIT:
            游戏中 = False
        if event.type ==KEYDOWN:
            if event.key == K_SPACE and pipe_up.move_counts==0:             #按空格键上管道下移后又弹回
                捕声效.play()
                pipe_up.yspeed = 20
                pipe_up.move_counts=10

    鸟.move()
    if 鸟.rect.y >screen_size[0] or 鸟.rect.x>screen_size[1] + randint(500,2000):  
        del 鸟
        鸟 = Bird(bird_images,0,180)
    if 鸟.碰到(pipe_up.rect):     #如果鸟碰到了管道,让它的垂直速度为22,水平速度为0
         鸟.yspeed = 22
         鸟.xspeed=0
    pipe_up.move()    
 
    screen.blit(background,(0,0))
    screen.blit(fontSurface,(200,50))
    鸟.draw()
    pipe_up.draw()
    
    pygame.display.update()
    clock.tick(30)   
        
pygame.quit()