马里奥躲避炮弹的python之pygame小游戏源码

2019-01-03  本文已影响0人  ___Clannad

#以下为个人原创代码,未经许可,不得用于商业目的!仅供学习交流,侵权必究!

如需要所有素材请联系作者

球球你们来看文章评论呀呀呀呀呀呀!!!

#Exp14_4.py

#encoding=utf-8

import pygame

#import pygame._view 没有对应的此包,运行结果没有差别

import random

from pygame.locals import *

import objects,config

def show_text(surface_handle, pos, text, color, font_bold = False, font_size = 13, font_italic = False):

    '''

    Function:文字处理函数

    Input:surface_handle:surface句柄

          pos:文字显示位置

          color:文字颜色

          font_bold:是否加粗

          font_size:字体大小

          font_italic:是否斜体

    '''     

    #获取系统字体stsong(宋体),并设置文字大小

    cur_font = pygame.font.SysFont('stsong' , font_size)

    #设置是否加粗属性

    cur_font.set_bold(font_bold)

    #设置是否斜体属性

    cur_font.set_italic(font_italic)

    #设置文字内容

    text_fmt = cur_font.render(text, 1, color)

    #绘制文字

    surface_handle.blit(text_fmt, pos)             

#主程序

if __name__=="__main__":

    pygame.init() #初始化所有的pygame模块

    background = pygame.image.load(config.background_image)

    star      = pygame.image.load(config.star_image)

    dinosaur  = pygame.image.load(config.dinosaur_image)

    dinosaur_rect=dinosaur.get_rect()

    background_w=int(background.get_width())

    background_h=int(background.get_height())

    star_w=int(star.get_width())

    star_h=int(star.get_height())

    dinosaur_w=int(dinosaur.get_width())

    dinosaur_h=int(dinosaur.get_height())

    flag=0  #默认窗口

    if config.full_screen:

        flag=FULLSCREEN #全屏模式

    #载入启动画面。按图片的宽、高创建一个窗口

    start_image = pygame.image.load('d2.jpg')

    start_image_w=int(start_image.get_width())

    start_image_h=int(start_image.get_height())

    start_size=( start_image_w, start_image_h)

    start_screen=pygame.display.set_mode(start_size,flag, 32)

    pygame.display.set_caption('超级玛丽躲避蘑菇')

    start_screen.blit(start_image, (0, 0)) #绘制窗口

    '''生成超级玛丽、蘑菇'''

    speed=1

    MStar=objects.Star(speed,config.star_image)

    MDinosaur=objects.Dinosaur(config.dinosaur_image)

    pygame.mouse.set_visible(False)

    flag=0

    dinosaur_left=background_w/2

    dinosaur_top=background_h-dinosaur_h

    MDinosaur.rect.left=background_w/2

    MDinosaur.rect.top=background_h-dinosaur_h

    star_top=-star_h

    MStar.rect.top=-star_h

    star_num=0

    level=1

    star_x=random.randrange(0,background_w)

    MStar.rect.left=random.randrange(0,background_w) 

    start=False

    title_info = "游戏载入完成,请单击鼠标进入游戏!" 

    show_text(start_screen, (50, 400), title_info, (200, 0, 10), True, 30)

    pygame.display.flip()#更新窗体

    n=0  #用来标识背景是否载入

    #主循环

    while True:

        # 检测事件

        for event in pygame.event.get():

            if event.type == QUIT:

                #接收到关闭窗口事件后退出程序

                pygame.quit()           

                flag=-1

                break

            if event.type==KEYDOWN and event.key==K_ESCAPE:

                #用户按Esc键后退出程序

                pygame.quit() 

                flag=-1

                break

        if flag==-1:

            break

        # 获取键盘的按键情况

        pressed_keys=pygame.key.get_pressed()

        # 获取鼠标的按键情况

        pressed_mouse=pygame.mouse.get_pressed()

        # 用start控制游戏的启、停

        if start:

            # 让陨石垂直向下移动

            #star_top+=config.drop_speed*config.speed_increase*level

            MStar.rect.top+=config.drop_speed*config.speed_increase*level*5

            '''

            如果陨石走出屏幕底部,则从顶部从新走。落下的流陨石加1,将陨石移动到屏幕顶端的随机位置。   

            '''

            if MStar.rect.top>background_h:

                MStar.rect.top=-5*star_h #star_top=-2*star_h

                star_num+=0

                MStar.rect.left=random.randrange(0,background_w) 

                #star_x=random.randrange(0,background_w)

            #载入背景

            if n==0:

                screen_size=( background_w, background_h)

                screen=pygame.display.set_mode(screen_size,flag,32)

                n=n+1

            #把背景画上去

            screen.blit(background, (0,0))

            #显示关数、陨石数 

            # 把陨石画上去

            screen.blit(MStar.image, MStar.rect)#(star_x,star_top))

            # 把动物画上去

            screen.blit(MDinosaur.image, MDinosaur.rect) #(dinosaur_left,dinosaur_top))

            # 根据鼠标的位置计算出动物的水平位置

            MDinosaur.rect.left=pygame.mouse.get_pos()[0]-MDinosaur.rect.width   

            #dinosaur_left=pygame.mouse.get_pos()[0]-dinosaur_w

            # 控制动物,不让其离开窗口

            if MDinosaur.rect.left<0:#dinosaur_left<0:

                MDinosaur.rect.left=0

            ''' 若本关内落下的陨石数达到设置值,则进入下一关,落下的陨石数清零,

                为下一关的计数做准备。

                进入下一关,游戏暂停。

            '''   

            if star_num>=config.stars_per_level:

                level+=1

                star_num=0

                start=False

                title_info = "恭喜!顺利通关!请单击鼠标进入下一关!" 

                show_text(screen, (100, 200), title_info, (200, 0, 0), True, 20)

                if 4<level<=7:

                    title_info = "干得漂亮!继续努力!" 

                    show_text(screen, (100, 300), title_info, (254, 245, 63), True, 50)

                elif 7<level<10:

                    title_info = "您是种子级选手!" 

                    show_text(screen, (100, 300), title_info, (253, 40, 17), True, 70)

                elif level>=10:

                    title_info = "您是天才级选手!" 

                    show_text(screen, (100, 300), title_info, (156, 156, 10), True, 70)

                MStar.image=pygame.image.load(config.li[level%13]) #(image_filename)

            '''若动物碰撞陨石,则回到第1关,游戏暂停。'''

            if MDinosaur.touches(MStar):

                MStar.rect.top=-2*star_h

                level=1

                MStar.image=pygame.image.load('j.gif')

                start=False

                star_num=0

                title_info = "很抱歉,没能通关,请单击鼠标回到第一关!" 

                show_text(screen, (100, 200), title_info, (50, 0 ,255), True, 20)

        else:

            # 在游戏暂停时,按鼠标左键或键盘空格键则启动游戏

            if pressed_keys[K_SPACE]or pressed_mouse[0]:

                start=True

        # 更新屏幕,上面的所画的内容才看得见。

        pygame.display.update()

上一篇下一篇

猜你喜欢

热点阅读