-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminecraft-robots.py
147 lines (118 loc) · 4 KB
/
minecraft-robots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Raspberry Pi Minecraft 3d Robots
#
# Cobbled together in a few hours by ZeBadger (www.zebadger.com)
# with the help of Emzie, Chloe, Daniel and Sarah
#
# Credits to www.stuffaboutcode.com
#import the minecraft.py module from the minecraft directory
import minecraft.minecraft as minecraft
#import minecraft block module
import minecraft.block as block
#import time, so delays can be used
import time
#import random module to create random number
import random
#player class
class Player:
def __init__(self, mc):
ppos=mc.player.getPos()
self.alive = True
self.x=ppos.x
self.y=ppos.x
self.z=ppos.z
def wherePlayer(self,mc):
ppos=mc.player.getPos()
self.x=int(ppos.x)
self.y=int(ppos.y)
self.z=int(ppos.z)
print "p: x = " , self.x , " z = " , self.z , " y = " , self.y
#robot class
class Robot:
robotCount=0
numDeadRobots=0
def __init__(self, mc):
self.mc = mc
self.alive = True
self.x = random.randrange(-186,68)
self.y = 0
self.y2 = 1
self.z = random.randrange(-63,191)
Robot.robotCount += 1
def displayCount(self):
print "Number of robots = %d" % Robot.robotCount
def whereRobot(self):
print "r: x = " , self.x , " z = " , self.z , " y = " , self.y , " y2 = " , self.y2 , " alive = ", self.alive
def kill(self):
if (self.alive == True):
Robot.numDeadRobots += 1
self.alive = False
self.mc.setBlock(self.x,self.y,self.z,block.STONE)
self.mc.setBlock(self.x,self.y2,self.z,block.STONE)
def moveRobot(self,player):
if (self.alive == False):
return
self.mc.setBlock(self.x,self.y,self.z,block.AIR)
self.mc.setBlock(self.x,self.y2,self.z,block.AIR)
if (player.x > self.x):
self.x += 1
if (player.x < self.x):
self.x -= 1
if (player.y > self.y):
self.y += 1
self.y2 += 1
if (player.y < self.y):
self.y -= 1
self.y2 -= 1
if (player.z > self.z):
self.z += 1
if (player.z < self.z):
self.z -= 1
if (player.x == self.x) and ( (player.y == self.y) or (player.y == self.y2) ) and (player.z == self.z):
player.alive = False
self.mc.setBlock(self.x,self.y,self.z,block.IRON_BLOCK)
self.mc.setBlock(self.x,self.y2,self.z,block.NETHER_REACTOR_CORE)
#main program
if __name__ == "__main__":
mc = minecraft.Minecraft.create()
#constants
player = Player(mc)
numRobots=10
robot = []
for r in range (0, numRobots):
robot.append(Robot(mc))
robot[0].displayCount()
mc.postToChat("MCPI Robots! by ZeBadger (www.zebadger.com)")
mc.postToChat((""))
mc.postToChat(("I spy " + str(numRobots) + " robots coming to get you!"))
mc.postToChat(("They are unstoppable, except when they crash into"))
mc.postToChat(("each other. Can you survive?"))
time.sleep(5)
mc.postToChat(("3..."))
time.sleep(1)
mc.postToChat(("...2..."))
time.sleep(1)
mc.postToChat(("...1..."))
time.sleep(1)
mc.postToChat(("...Go!"))
time.sleep(1)
while True :
player.wherePlayer(mc)
for r in range (0, numRobots):
robot[r].moveRobot(player)
robot[r].whereRobot()
# Now check collisions
for c in range (0, numRobots):
if (c != r):
if (robot[r].x == robot[c].x) and ( (robot[r].y == robot[c].y) or (robot[r].y == robot[c].y2) ) and (robot[r].z == robot[c].z):
robot[r].kill()
robot[c].kill()
time.sleep(.3)
if (player.alive != True):
mc.postToChat("The robots got you!")
break
if (Robot.numDeadRobots == numRobots):
mc.postToChat("You win!")
break
for r in range (0, numRobots):
robot[r].kill()
mc.postToChat("Game Over")