今天,我为大家带来了最喜欢的MineCraft————————编程(看到这个词语以后情绪是不是十分低落,还以为是做MineCraft)呢。没关系,我们今天做的是:用pygame画出MineCraft小鸡!

还是推荐你看看之前的两篇,回顾以前的知识:

python-pygame小游戏之球球大作战

python-pygame作品之黑客帝国代码雨

新人提醒:

每隔1次,我会提醒新人一次,新人需注意!没有安装过pygame模块的需要(必须!)在cmd里导入pygame模块哦~方法如下:

打开运行,输入cmd,然后输入以下代码
pip install pygame

---------------------------------------------------开始写代码了!---------------------------------------------------------

一、初始化

还是老办法,先导入模块,再初始化。(背景最好不是白色,这样鸡的样子就显现不出来了)

# import pygame
import pygame as pg

# init program
pg.init()
screen = pg.display.set_mode((1000, 500))
screen.fill((0, 0, 255))

二、函数、方法和类的定义

首先写好画线条的方法,这样以后画线更方便了!

# def "line()", it is very useful
def line(color, start, end, size):
    pg.draw.line(screen, color,start,end,size)

小回顾:这和之前球球大作战里的circle函数是不是很像?(需要复制的不用复制这段。)

# def circle
def circle(color, point, r, size):
    pg.draw.circle(screen, color, point, r, size)

忘记的可以回去看哦~

--------------------------------------------------------------------------------------------------------------------------------

然后定义好画法类,方便画鸡。

class creatures():
    # init creatures
    # must use "for" range to control line
    # it is very difficult to control
    def __init__(self, x, y, color):
        for i in range(len(color)):
            for j in range(len(color[i])):
                # use "line()"
                # it is difficult here
                # j need plus x,i need plus y(do not write "+=")
                line( color[i][j], (x + j*50, y +i*50), (x + j*50+50,y ++ i*50), 50)

三、主程序运行

接下来要运行主程序了!首先要做一个二维列表,里面放上每行的颜色(注意使用RGB颜色)

# use "line()" and "creatures()" to draw a minecraft:chicken:
# these are the chicken program
# use RGB method
b = (0, 0, 0)
w = (255,255,255)
y = (255, 200, 0)
z = (120, 99, 0)
r = (255, 0, 0)
g = (245, 245, 245)

# use two-dimensional list
color = [[b, w, w, b],
         [y, y, y, y],
         [z, z, z, z],
         [w, r, r, w],
         [g, r, r, g]]

然后实例化类,画出小鸡,别忘了dispaly(更新)一下程序!

# build a chicken
chicken = creatures(100, 100, color)

# display and update program
pg.display.update()

一个小鸡就呈现在你面前了!是不是很简单(当然你刚学的话会有那么一点难)

四、完整代码

奉上完整代码:

# import pygame
import pygame as pg

# init program
pg.init()
screen = pg.display.set_mode((1000, 500))
screen.fill((0, 0, 255))

# def "line()", it is very useful
def line(color, start, end, size):
    pg.draw.line(screen, color,start,end,size)

# and build class creatures
class creatures():
    # init creatures
    # must use "for" range to control line
    # it is very difficult to control
    def __init__(self, x, y, color):
        for i in range(len(color)):
            for j in range(len(color[i])):
                # use "line()"
                # it is difficult here
                # j need plus x,i need plus y(do not write "+=")
                line( color[i][j], (x + j*50, y +i*50), (x + j*50+50,y ++ i*50), 50)

# use "line()" and "creatures()" to draw a minecraft:chicken:
# these are the chicken program
# use RGB method
b = (0, 0, 0)
w = (255,255,255)
y = (255, 200, 0)
z = (120, 99, 0)
r = (255, 0, 0)
g = (245, 245, 245)

# use two-dimensional list
color = [[b, w, w, b],
         [y, y, y, y],
         [z, z, z, z],
         [w, r, r, w],
         [g, r, r, g]]

# build a chicken
chicken = creatures(100, 100, color)

# display and update program
pg.display.update()

五、效果图

 六、知识总结

看,pygame是不是很厉害呢?它还可以画出MineCraft的小鸡呢(虽然弱了点,但总归是一个程序啊!)?

python和pygame都很简单,很实用。快来用用吧!

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐