猫史档案馆


好消息!Pygame库教程来啦!

用户:橙子nUcg橙子nUcg查看:56 回复:15 评论:56 创建时间:2021-09-20T11:53:51


以后,本人会持续发布一些关于Python的Pygame库的教程!

今天的内容是:Pygame库入门!!!

Pygame简介:

Pygame是Python中一个制作2D游戏的、功能强大的第三方库,在海龟编辑器客户端即可下载。这个库允许向游戏中添加角色(这里叫“精灵”),并建立窗口。

现在,我们来做一个最简单的基本操作:导入和初始化。

import pygame #导入Pygame
import sys #导入sys模块,退出窗口时会用到


pygame.init() #初始化Pygame

好了,这是最基本操作,以后必须经常用到。

当然,这个操作只是让你认识这个库,并不是真正的大型教程。

下节课,作者才会发布更多Pygame教程。拭目以待!

下节课预告:新建窗口,并实现按“X”退出

关注作者下次的教程贴哦!

再见!


回复

上一页1 页 / 共 1下一页
橙子nUcg橙子nUcg

下次贴预计这周四发布!

点赞1


评论


Python-happyyyyPython-happyyyy

e

 

点赞0


评论


橙子nUcg橙子nUcg

 

点赞1


评论


wrmdcxywrmdcxy

这也叫入门?初始个pygame谁不会?连游戏主循环都要放在下一章讲。。有必要劈开成那么多段来讲吗?这不是你咕的理由

点赞1


评论


形象长春形象长春

???????????就这????

点赞0


评论


Python-happyyyyPython-happyyyy

百度yyds

csdnyyds

菜鸟教程yyds

点赞2


评论


Albert钟Albert钟

pygame这玩意本身就用的不多了,连事件循环、面向对象都没有,贴主可以考虑下pgz、arcade,另外。。。。这个内容量也太小

点赞1


评论


lsk666666lsk666666

铁铁们啊,别喷了,别仗着自己懂得多就以为这里没有菜鸟啊,我遇上好几个连py语法都不会的和图形化人发的帖子,别喷了,这玩意肯定有存在的价值(至少对初学pygame的人比较方便因为这面的人很少有能看懂百度的)(python区用pygame做游戏的人数能和arcade用户整个五五开,还不算太冷门)

点赞2


评论


橙子nUcg橙子nUcg

不过这里真的有人连Python语法都不会吗?真的有人看不懂下面那个(开个玩笑)?

user = input('你会Python语法吗?(填“会”或“不会”)')
if(user == '会' or user == '废'):
    print('ddd')
elif(user == '不会' or user == '不废'):
    print('呃呃呃')

有人不会我服了。

点赞2


评论


Python-happyyyyPython-happyyyy

不会吧不会吧,都1202年了还有人不会Python语法

点赞2


评论


lsk666666lsk666666

anyway,我就是来看看,了解我的都知道我讨厌GUI开发

点赞1


评论


橙子nUcg橙子nUcg

发个和Python无关的东西,是@Python-happyyyy让我发的,神奇代码岛JAVAscript代码。下面有部分注释还没写,见谅。

//这是本人的第一个作品的代码,不喜勿喷!
const mms = world.querySelector('#密码锁-1'); //让密码锁-1交互
mms.enableInteract = true;
mms.interactHint = '密码锁';
mms.interactRadius = 4;

mms.onInteract(async({entity})=>{ //当发起交互
    const result = await entity.player.dialog({
        type: Box3DialogType.SELECT,
        title: '门锁',
        content: '本作品的作者是?',
        options: ['吉吉喵','搬砖喵','名字鬼知道','ExtBLO','橙子nUcg','MC玩家103','木叶龙1342'],
    });

    if(result.index === 4){ //如果选择正确
        world.say(`${entity.player.name}顺利通过第一关!`)
        entity.player.directMessage('正确!')
        entity.position.set(240,8,248) //逃走
        mms.enableInteract = false
    }
    else{ //否则
        entity.hurt(15) //伤害
        entity.player.directMessage('错误!')
    }
})

const rw = world.querySelector('#人物-1')
rw.enableInteract = true;
rw.interactRadius = 4;
rw.interactHint = `战友`;
rw.interactColor = new Box3RGBColor(1,1,1);//互动字体颜色

    rw.onInteract(async({entity}) => {//当发起交互
        entity.player.dialog({
            type: Box3DialogType.TEXT,   // 对话框的类型
            title: `战友`,               // 对话框标题
            content: `${entity.player.name},快去越狱吧!`,
        });
    })

    const mms1 = world.querySelector('#密码锁-2')
    mms1.enableInteract = true;
    mms1.interactRadius = 4;
    mms1.interactHint = `门锁`;

    mms1.onInteract(async({entity})=>{
        const r1 = await entity.player.dialog({
            type:Box3DialogType.SELECT,
            title:`门锁`,
            content:`月球距离地球?`,
            options:['8000千米','38万千米','42万千米','1089亿千米','13500万千米'],  
        })
        if(r1.index === 1){
            world.say(`${entity.player.name}顺利通过第二关!`)
            entity.player.directMessage(`正确!`)
            entity.position.set(219,8,235)
            mms1.enableInteract = false
        }
        else{
            entity.hurt(15)
            entity.player.directMessage(`错误!`)
        }
    })


    mms2 = world.querySelector('#密码锁-3');
    mms2.enableInteract = true;
    mms2.interactRadius = 4;
    mms2.interactHint = '门锁';

    mms2.onInteract(async({entity})=>{
        const r2 = await entity.player.dialog({
            type:Box3DialogType.SELECT,
            title:`门锁`,
            content:`圆周率四舍五入保留8位小数是?`,
            options:['3.14159269','3.14259269','3.14195238','3.14145235','3.14159265'],
        })
        if(r2.index === 4){
            world.say(`${entity.player.name}顺利通过隔离门!`)
            entity.player.directMessage(`正确!`)
            entity.position.set(203,8,223)
            mms2.enableInteract = false
        }
        else{
            entity.hurt(15)
            entity.player.directMessage(`错误!`)
        }
    })
    


world.onPlayerJoin(({entity})=>{ //当玩家进入地图
    entity.position.set(250,8,246)//出生地
    entity.enableDamage = true //允许伤害
    entity.things = [] //空物品列表
    entity.money = 10
    entity.atk = 0 //各种变量
    if(entity.player.name === '橙子nUcg'){ //欢迎作者
        world.say('欢迎作者橙子nUcg的到来!')
        entity.player.canFly = true //能飞
    }
    else if(entity.player.name === '小小瓢虫'||entity.player.name === '一只小虎喵'||entity.player.name === 'studentNo.1(学员一号)'){ //欢迎合作者
        world.say('欢迎合作者'+entity.player.name+'的到来!')
        entity.player.canFly = true //也能飞
    }
    entity.player.dialog({
        type:Box3DialogType.TEXT,
        content:`你,${entity.player.name},在超时空对决中,被外星人抓到了这个位于月球的监狱。`,
    });
    entity.player.dialog({
        type:Box3DialogType.TEXT,
        content:`你暂时只有10元钱。请你凭借智慧,破解门锁,拾取道具,击败外星人,逃回地球!`
    });
    entity.player.addWearable({
        bodyPart:Box3BodyPart.TORSO,
        mesh:'mesh/黄喵包.vb',
        orientation:new Box3Quaternion(0,1,0,0).rotateY(Math.PI/2),
        scale:new Box3Vector3(0.5,0.5,0.5),
        offset:new Box3Vector3(0,0,-0.45),
    });
});


world.onChat(({entity,message})=>{
    if(message==='关闭飞行'){
        if(entity.player.name==='橙子nUcg'||entity.player.name==='小小瓢虫'||entity.player.name === '一只小虎喵'||entity.player.name === 'studentNo.1(学员1号)'){
            entity.player.canFly = false
            world.say('成功!')
        }
        else{
            entity.hurt(5)
            entity.player.directMessage('喂,别吵!你又不是作者!')
        }
    }
    else if(message==='开启飞行')
        if(entity.player.name==='橙子nUcg'||entity.player.name==='小小瓢虫'||entity.player.name === '一只小虎喵'||entity.player.name === 'studentNo.1(学员1号)'){
            entity.player.canFly = true
            world.say('成功!')
        }
        else{
            entity.hurt(5)
            entity.player.directMessage('呃......')
        }
})
world.onPress(async({ button, entity }) => {
    if(!entity.isPlayer || entity.player.dead) return;
    if(button == 'action1'){
        const pl_1=await entity.player.dialog({
            type:Box3DialogType.SELECT,
            title:entity.player.name+"的状态",
            content:entity.player.name+"的状态:\n血量:"+entity.hp+"\n金钱:"+entity.money+"\n攻击力:"+entity.atk,
            options:['保存','道具','取消']
            
        });
        if(pl_1.index===0){
            return;
        }
        else if(pl_1.index===1){
            const pl_2 = await entity.player.dialog({
                type:Box3DialogType.SELECT,
                title:entity.player.name+'的道具',
                options:entity.things,
            })
        }
        else if(pl_1.index===2){
            if(entity.player.boxId===''){
                entity.player.dialog({
                    type:Box3DialogType.TEXT,
                    title:entity.player.name+'的状态',
                    content:'你是游客,无法保存!',
                })
            }
            else{
                //保存,先别动,要用SQL
            }
        }
    }
})

点赞1


评论


AlbertHackerAlbertHacker

【Albert--HackerAI智能评论】发生了什么[doge]

点赞0


评论


AlbertHackerAlbertHacker

【Albert--HackerAI智能评论】元芳,你怎么看

点赞0


评论


AlbertHackerAlbertHacker

【Albert--HackerAI智能评论】呦呵~awa

点赞0


评论