【Pygame实战】嗷大喵历险记之程序员吸猫指南:真的太上头了~

导语

哈喽~大家好,我是木子,首先今天木子先给大家 讲个小故事

在喵界有这样一只网红——混迹于二次元、表情包界,贱萌活泼,调皮机灵,白色的大圆脸,脖子

上系了个铃铛,年龄不详,传说可于儿童、少年、青年间随意切换。

他的欢乐日常带给了很多人温暖和爆笑,他的乐观积极也是很多人治疗伤心难过空虚寂寞冷综合症

的良方,他就是—— 嗷大喵

【Pygame实战】嗷大喵历险记之程序员吸猫指南:真的太上头了~

Ps小介绍:

嗷大喵资料:是一位开朗、乐观、热情奔放、温暖人心的喵星人。他静如瘫痪,动如癫痫,喜欢的

事情是吃和睡,他的直率和可爱还经常被人误以为是神经病呢。

嗷大喵和他的小伙伴们快乐的生活在一起,他们总是能给大家带来欢乐。 大家都说:”嗷大喵真

棒!”。

………………………………………………………………………………………………………………………………………..

介绍到这里就差不多了哈,今天要写的小游戏也跟嗷大喵有关的呢~

利用这个”网红”ip打造出一个独一无二的嗷大喵表情 包小游戏给大家解解乏哦~上班摸鱼也可以.jpg

………………………………………………………………………………………………………………………………………..

​正文

一、准备中

1.1 素材准备

背景音乐:(可修改)

【Pygame实战】嗷大喵历险记之程序员吸猫指南:真的太上头了~

图片素材:(可修改)

【Pygame实战】嗷大喵历险记之程序员吸猫指南:真的太上头了~

1.2 游戏规则

嗷大喵遇险记:嗷大喵即玩家,遭遇飞机失联,一猫🐱独自掉落到无人居住的荒岛,好在荒岛上

资源充足,嗷大喵一个猫生存了下来,某一天嗷大喵追赶一只奇奇怪怪的生物,跑到了一个很高的

洞口,好奇心驱使嗷大喵紧张的走进了洞穴,不料想一进洞就遇到了一只传说中的恶龙,嗷大喵吓

得只好赶紧跑出洞外——自此一直被恶龙追赶……

Over ,咳咳咳……儿童故事就听到这里吧。 大家要做的就是解救嗷大喵~远离恶龙。

玩法:按住空格躲避恶龙的火焰,击中的话会减速,然后被恶龙吃掉。

二、环境安装

环境:Python3、Pycharm、Pygame以及一些自带模块。

这里模块安装命令:

pip install +模块名   或者豆瓣镜像源 pip install -i https://pypi.douban.com/simple/ +模块名

三、正式敲代码

3.1 定义开始游戏这个按钮

-*- coding: utf-8 -*-
import pygame
from sys import exit
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((300,200),0,32)
upImageFilename = 'game_start_up.png'
downImageFilename = 'game_start_down.png'

class Button(object):
    def __init__(self, upimage, downimage,position):
        self.imageUp = pygame.image.load(upimage).convert_alpha()
        self.imageDown = pygame.image.load(downimage).convert_alpha()
        self.position = position

    def isOver(self):
        point_x,point_y = pygame.mouse.get_pos()
        x, y = self. position
        w, h = self.imageUp.get_size()

        in_x = x - w/2 < point_x < x + w/2
        in_y = y - h/2 < point_y < y + h/2
        return in_x and in_y

    def render(self):
        w, h = self.imageUp.get_size()
        x, y = self.position

        if self.isOver():
            screen.blit(self.imageDown, (x-w/2,y-h/2))
        else:
            screen.blit(self.imageUp, (x-w/2, y-h/2))
    def is_start(self):
        b1,b2,b3 = pygame.mouse.get_pressed()
        if b1 == 1:
            return True

button = Button(upImageFilename,downImageFilename, (150,100))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    screen.fill((200, 200, 200))
    button.render()
    if button.is_start():
        print ("start")
    pygame.display.update()

3.11 附效果截图

【Pygame实战】嗷大喵历险记之程序员吸猫指南:真的太上头了~

3.2 主程序

-*- coding: utf-8 -*-
import sys, time, random, math, pygame,locale
from pygame.locals import *
from MyLibrary import *

#&#x91CD;&#x7F6E;&#x706B;&#x7BAD;&#x51FD;&#x6570;
def reset_arrow():
    y = random.randint(270,350)
    arrow.position = 800,y
    bullent_sound.play_sound()

#&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6EDA;&#x52A8;&#x5730;&#x56FE;&#x7C7B;
class MyMap(pygame.sprite.Sprite):

    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.bg = pygame.image.load("background.png").convert_alpha()
    def map_rolling(self):
        if self.x < -600:
            self.x = 600
        else:
            self.x -=5
    def map_update(self):
        screen.blit(self.bg, (self.x,self.y))
    def set_pos(x,y):
        self.x =x
        self.y =y
#&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6309;&#x94AE;&#x7C7B;
class Button(object):
    def __init__(self, upimage, downimage,position):
        self.imageUp = pygame.image.load(upimage).convert_alpha()
        self.imageDown = pygame.image.load(downimage).convert_alpha()
        self.position = position
        self.game_start = False

    def isOver(self):
        point_x,point_y = pygame.mouse.get_pos()
        x, y = self. position
        w, h = self.imageUp.get_size()

        in_x = x - w/2 < point_x < x + w/2
        in_y = y - h/2 < point_y < y + h/2
        return in_x and in_y

    def render(self):
        w, h = self.imageUp.get_size()
        x, y = self.position

        if self.isOver():
            screen.blit(self.imageDown, (x-w/2,y-h/2))
        else:
            screen.blit(self.imageUp, (x-w/2, y-h/2))
    def is_start(self):
        if self.isOver():
            b1,b2,b3 = pygame.mouse.get_pressed()
            if b1 == 1:
                self.game_start = True
                bg_sound.play_pause()
                btn_sound.play_sound()
                bg_sound.play_sound()

def replay_music():
    bg_sound.play_pause()
    bg_sound.play_sound()

#&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6570;&#x636E;IO&#x7684;&#x65B9;&#x6CD5;
def data_read():
    fd_1 = open("data.txt","r")
    best_score = fd_1.read()
    fd_1.close()
    return best_score

#&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x63A7;&#x5236;&#x58F0;&#x97F3;&#x7684;&#x7C7B;&#x548C;&#x521D;&#x59CB;&#x97F3;&#x9891;&#x7684;&#x65B9;&#x6CD5;
def audio_init():
    global hit_au,btn_au,bg_au,bullent_au
    pygame.mixer.init()
    hit_au = pygame.mixer.Sound("exlposion.wav")
    btn_au = pygame.mixer.Sound("button.wav")
    bg_au = pygame.mixer.Sound("background.ogg")
    bullent_au = pygame.mixer.Sound("bullet.wav")
class Music():
    def __init__(self,sound):
        self.channel = None
        self.sound = sound
    def play_sound(self):
        self.channel = pygame.mixer.find_channel(True)
        self.channel.set_volume(0.5)
        self.channel.play(self.sound)
    def play_pause(self):
        self.channel.set_volume(0.0)
        self.channel.play(self.sound)

#&#x4E3B;&#x7A0B;&#x5E8F;&#x90E8;&#x5206;
pygame.init()
audio_init()
screen = pygame.display.set_mode((800,600),0,32)
pygame.display.set_caption("&#x55F7;&#x5927;&#x55B5;&#x5FEB;&#x8DD1;&#xFF01;")
font = pygame.font.Font(None, 22)
font1 = pygame.font.Font(None, 40)
framerate = pygame.time.Clock()
upImageFilename = 'game_start_up.png'
downImageFilename = 'game_start_down.png'
#&#x521B;&#x5EFA;&#x6309;&#x94AE;&#x5BF9;&#x8C61;
button = Button(upImageFilename,downImageFilename, (400,500))
interface = pygame.image.load("interface.png")

#&#x521B;&#x5EFA;&#x5730;&#x56FE;&#x5BF9;&#x8C61;
bg1 = MyMap(0,0)
bg2 = MyMap(600,0)
#&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7CBE;&#x7075;&#x7EC4;
group = pygame.sprite.Group()
group_exp = pygame.sprite.Group()
group_fruit = pygame.sprite.Group()
#&#x521B;&#x5EFA;&#x602A;&#x7269;&#x7CBE;&#x7075;
dragon = MySprite()
dragon.load("dragon.png", 260, 150, 3)
dragon.position = 100, 230
group.add(dragon)

#&#x521B;&#x5EFA;&#x7206;&#x70B8;&#x52A8;&#x753B;
explosion = MySprite()
explosion.load("explosion.png",128,128,6)
#&#x521B;&#x5EFA;&#x73A9;&#x5BB6;&#x7CBE;&#x7075;
player = MySprite()
player.load("sprite.png", 100, 100, 4)
player.position = 400, 270
group.add(player)

#&#x521B;&#x5EFA;&#x5B50;&#x5F39;&#x7CBE;&#x7075;
arrow = MySprite()
arrow.load("flame.png", 40, 16, 1)
arrow.position = 800,320
group.add(arrow)

#&#x5B9A;&#x4E49;&#x4E00;&#x4E9B;&#x53D8;&#x91CF;
arrow_vel = 10.0
game_over = False
you_win = False
player_jumping = False
jump_vel = 0.0
player_start_y = player.Y
player_hit = False
monster_hit = False
p_first = True
m_first = True
best_score = 0
global bg_sound,hit_sound,btn_sound,bullent_sound
bg_sound=Music(bg_au)
hit_sound=Music(hit_au)
btn_sound=Music(btn_au)
bullent_sound =Music(bullent_au)
game_round = {1:'ROUND ONE',2:'ROUND TWO',3:'ROUND THREE',4:'ROUND FOUR',5:'ROUND FIVE'}
game_pause = True
index =0
current_time = 0
start_time = 0
music_time = 0
score =0
replay_flag = True
#&#x5FAA;&#x73AF;
bg_sound.play_sound()
best_score = data_read()
while True:
    framerate.tick(60)
    ticks = pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        pygame.quit()
        sys.exit()

    elif keys[K_SPACE]:
        if not player_jumping:
            player_jumping = True
            jump_vel = -12.0

    screen.blit(interface,(0,0))
    button.render()
    button.is_start()
    if button.game_start == True:
        if game_pause :
            index +=1
            tmp_x =0
            if score >int (best_score):
                best_score = score
            fd_2 = open("data.txt","w+")
            fd_2.write(str(best_score))
            fd_2.close()
            #&#x5224;&#x65AD;&#x6E38;&#x620F;&#x662F;&#x5426;&#x901A;&#x5173;
            if index == 6:
                you_win = True
            if you_win:
                start_time = time.clock()
                current_time =time.clock()-start_time
                while current_time<5: screen.fill((200, 200, 200)) print_text(font1, 270, 150,"you win the game!",(240,20,20)) current_time="time.clock()-start_time" 320, 250, "best score:",(120,224,22)) 370, 290, str(best_score),(255,0,0)) 330, "this game 385, 380, str(score),(255,0,0)) pygame.display.update() pygame.quit() sys.exit() for i in range(0,100): element="MySprite()" element.load("fruit.bmp", 75, 20, 1) tmp_x +="random.randint(50,120)" element.x="tmp_x+300" element.y="random.randint(80,200)" group_fruit.add(element) start_time="time.clock()" while current_time<3: 250,game_round[index],(240,20,20)) game_pause="False" else: #更新子弹 if not game_over: arrow.x -="arrow_vel" < -40: reset_arrow() #碰撞检测,子弹是否击中玩家 pygame.sprite.collide_rect(arrow, player): explosion.position="player.X,player.Y" player_hit="True" hit_sound.play_sound() p_first: group_exp.add(explosion) p_first="False" player.x #碰撞检测,子弹是否击中怪物 dragon): monster_hit="True" m_first: m_first="False" dragon.x #碰撞检测,玩家是否被怪物追上 pygame.sprite.collide_rect(player, game_over="True" #遍历果实,使果实移动 e group_fruit: e.x collide_list="pygame.sprite.spritecollide(player,group_fruit,False)" score #是否通过关卡 -100: #检测玩家是否处于跳跃状态 player_jumping: jump_vel <0: elif>= 0:
                    jump_vel += 0.8
                player.Y += jump_vel
                if player.Y > player_start_y:
                    player_jumping = False
                    player.Y = player_start_y
                    jump_vel = 0.0

            #&#x7ED8;&#x5236;&#x80CC;&#x666F;
            bg1.map_update()
            bg2.map_update()
            bg1.map_rolling()
            bg2.map_rolling()

            #&#x66F4;&#x65B0;&#x7CBE;&#x7075;&#x7EC4;
            if not game_over:
                group.update(ticks, 60)
                group_exp.update(ticks,60)
                group_fruit.update(ticks,60)
            #&#x5FAA;&#x73AF;&#x64AD;&#x653E;&#x80CC;&#x666F;&#x97F3;&#x4E50;
            music_time = time.clock()
            if music_time   > 150 and replay_flag:
                replay_music()
                replay_flag =False
            #&#x7ED8;&#x5236;&#x7CBE;&#x7075;&#x7EC4;
            group.draw(screen)
            group_fruit.draw(screen)
            if player_hit or monster_hit:
                group_exp.draw(screen)
            print_text(font, 330, 560, "press SPACE to jump up!")
            print_text(font, 200, 20, "You have get Score:",(219,224,22))
            print_text(font1, 380, 10, str(score),(255,0,0))
            if game_over:
                start_time = time.clock()
                current_time =time.clock()-start_time
                while current_time<5: screen.fill((200, 200, 200)) print_text(font1, 300, 150,"game over!",(240,20,20)) current_time="time.clock()-start_time" 320, 250, "best score:",(120,224,22)) if score>int (best_score):
                        best_score = score
                    print_text(font1, 370, 290, str(best_score),(255,0,0))
                    print_text(font1, 270, 330, "This Game Score:",(120,224,22))
                    print_text(font1, 370, 380, str(score),(255,0,0))
                    pygame.display.update()
                fd_2 = open("data.txt","w+")
                fd_2.write(str(best_score))
                fd_2.close()
                pygame.quit()
                sys.exit()
    pygame.display.update()</5:></5:>

四、效果展示

4.1 动态视频展示——

嗷大喵历险记~

4.2 静态截图展示——

【Pygame实战】嗷大喵历险记之程序员吸猫指南:真的太上头了~

【Pygame实战】嗷大喵历险记之程序员吸猫指南:真的太上头了~

总结

萌萌的嗷大喵能不能逃离恶龙的追击,就看你们了哈~

噗~哈哈哈 咋感觉我最近的画风有点儿不对,都是一些奇奇怪怪可可爱爱的儿童画风!

【Pygame实战】嗷大喵历险记之程序员吸猫指南:真的太上头了~

🎯完整的免费源码(我主页pc段左侧自取或者私信我也可!)

你们的支持是我最大的动力!! 记得三连哦~mua 欢迎大家阅读往期的文章哦~

🎉往期游戏热门文章推荐:

项目1.8 人机五子棋游戏

Pygame实战:利用Python实现智能五子棋,实现之后发现我玩不赢它。

项目4.2 我的世界游戏

Pygame实战:方块连接世界,云游大好河山—《我的世界》已上线,确定不进来康康嘛?

项目4.4 剧情版游戏

【Python剧情版游戏】优美精致的画风甜甜的剧情、很难不让人上头啊?你get到了嘛

项目1.1 扫雷

Pygame实战:据说这是史上最难扫雷游戏,没有之一,你们感受下……

项目1.2 魂斗罗

Pygame实战:多年后”魂斗罗”像素风归来 不止是经典与情怀@全体成员

文章汇总——

Python—2021 |已有文章汇总 | 持续更新,直接看这篇就够了

【Pygame实战】嗷大喵历险记之程序员吸猫指南:真的太上头了~

Original: https://blog.csdn.net/weixin_55822277/article/details/121465168
Author: 顾木子吖
Title: 【Pygame实战】嗷大喵历险记之程序员吸猫指南:真的太上头了~

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/781659/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球