stoneskin

learning coding with Scratch , Python and Java

Session 4: Python File , Module, Class and Introduce Github

Session 4.2 Python Object-Oriented Programming and Introduce github

1. OOP in Python

[Mission 4.9] Understand Inheritance of python

Inheritance is the process by which one class takes on the attributes and methods of another. Newly formed classes are called child classes, and the classes that child classes are derived from are called parent classes.

See the example of Animals classes and their parent-child relationship: inherit

Animal is the Top level class , and it has B

class Animal:
    def __init__(self,name,gender):
        self.name=name
        self.gender=gender
    def __str__(self) -> str:
        return "class={}: name:{} gender:{}".format(type(self).__name__,self.name,self.gender)

class Bird(Animal):
    def __init__(self,name,gender):
        Animal.__init__(self,name,gender)
    def flying(self):
        return "birds flying..."  

class Chicken(Bird):
    def __init__(self,name,gender):
        super().__init__(name,gender) #python 3 and upper you could use supper() to init parent without self
        self.gender=gender
    def flying(self):
        return "chicken {} can't fly..".format(self.name)

class CanadianGoose(Bird):
    def __init__(self,name,gender):
        Bird.__init__(self,name,gender)
    def swimming(self):
        return "Canadian Goose floating on the water.."

aHen = Chicken("egg hatcher","F")
aRooster = Chicken("big Foot","M")
aGoose = CanadianGoose("wild goose","unknown") 

print(aHen.flying())
print(aGoose.flying())
print(aGoose.swimming())
print(aRooster)

[Challenge] [mission 4.10] Rewrite your homeworks with Object Oriented style

  1. group your functions as methods of a Classes
  2. make a child class that has all methods of parents class, but has one or two new methods or same methods do different than parents
  3. please send your home work to stoneskin@gmail.com

[hint: you could check the below two options]

[mission 4.11] Build a tree and forest in Minecraft

Example of the tree
import math
from mcpi_e import block
from mcpi_e.vec3 import Vec3
import random
import time

class Tree:
    desc="Tree"
    woodId=17
    leavesId=18
    pos = Vec3(0,0,0)
    high=6

    def __init__(self,position,typeid=0):
        self.typeId=typeid
        self.pos=position

    def buildTree(self,mc):
        self.__buildTrunk(mc)
        self.__buildLeaves(mc)
    def __buildTrunk(self,mc):
        for h in range(self.high):
            mc.setBlock(self.pos.x,self.pos.y+h,self.pos.z,self.woodId,self.typeId )
    def __buildLeaves(self,mc):
        treeSize=3
        for i in range(0-treeSize,treeSize+1):
            for j in range(0-treeSize,treeSize+1):
                 mc.setBlock(self.pos.x+i,self.pos.y+self.high,self.pos.z+j,self.woodId,self.typeId )
                 mc.setBlock(self.pos.x+i,self.pos.y+self.high+1,self.pos.z+j,self.leavesId,self.typeId )  
        for i in range(0-treeSize+1,treeSize-1):
            for j in range(0-treeSize+1,treeSize-1):
                mc.setBlock(self.pos.x+i,self.pos.y+self.high+2,self.pos.z+j,self.leavesId,self.typeId )
        for i in range(0-treeSize,treeSize+1):
            for j in range(0-treeSize,treeSize+1):
                if(abs(i)>1 or abs(j)>1 or abs(i)==abs(j)):
                    mc.setBlock(self.pos.x+i,self.pos.y+self.high,self.pos.z+j,self.leavesId,self.typeId)

Build some child Tree Class
class BirchTree(Tree):
    def __init__(self,pos):
        self.desc="SpruceTree"
        super().__init__(pos,2) # spruce:1, birch:2, jungle:3

class BirchTree(Tree):
    def __init__(self,pos):
        self.desc="SpruceTree"
        super().__init__(pos,2) # spruce:1, birch:2, jungle:3



The code of test Tree inheritance
from mcpi_e.minecraft import Minecraft
from mcpi_e import block
from mcpi_e.vec3 import Vec3
import random

import time
from tree import *

serverAddress = "127.0.0.1"
playerName = "youracct"
pythonApiPort = 4711

mc=Minecraft.create(serverAddress,pythonApiPort,playerName)
#mc.settings.SHOW_DEBUG=True
pos=mc.player.getTilePos()
aTree=Tree(pos)
aTree.buildTree(mc)

#build a Birch tree
x=random.randint(-15, 15)
z=random.randint(-15, 15)
pos2=Vec3(pos.x+x,pos.y,pos.z+z)
anotherTree= BirchTree(pos2)
anotherTree.buildTree(mc)
Example of build a RandomTree
class RandomTree(Tree):
    treeList=[0,1,2,3] #oak:0, spruce:1, birch:2, jungle:3
    def __init__(self,pos):
        typeId= random.choice(self.treeList)
        self.desc="Random Tree type "+str(typeId)
        super().__init__(pos,typeId)

The code to test the tree

aRandomTree=RandomTree(pos2)
print(aRandomTree.desc)
aRandomTree.buildTree(mc)

Example of overload the parent method

#a tree will disappear after 10s
class GhostTree(RandomTree):
    def __init__(self,pos):
        super().__init__(pos)
    def clear(self,mc):
        mc.setBlocks(self.pos.x-3,self.pos.y,self.pos.z-3,self.pos.x+3,self.pos.y+self.high,self.pos.z+3,0)
    def buildTree(self,mc): #override the parent method
        super().buildTree(mc)
        time.sleep(10)
        self.clear(mc)

2 Learn and using Github for your code Version Control

2.1 What is version control

Version control system are software tools that help ou manage change of source code over time local

2.2 As a programmer, Why I need the Version Control System

2.3 What are Git and GitHub

github

2.4 How Git and GitHub works

gitbranch

2.5 Download and install git

git-download

2.6 Create a github account

2.7 Download and install a git GUI

2.8 Git command line

Use GUI client will save you time to remember those command line, but below is some git command line you can use when gui not available:

3 Use github to for your code and project

3.1 Create your repository on the github

3.2 Clone the repository to you local computer

3.3 Add code to your local repository folder

3.4 Stage and check in your code to local repository

3.5 push your change to the github

3.6 View your code on th github

After code be pushed, you can go to the github website to see your new code is be changed on the online github repository

4 Use Markdown and Github site to build your own website

4.1 understand the markdown file

you will see we add a README.md in our project

for how to use markdown, please check the Markdown Cheat Sheet

for how to use github site to build your website check here