theArgoII
Posts: 7
Joined: Fri Jan 02, 2015 9:27 pm

test for in mcpi

Sun Mar 15, 2015 7:16 pm

So if you possibly saw the last post a while ago it was about making two function work together, well I did that but now I was wondering if in the minecraftpi that you can test for a block using python. I'm real stuck so please help soon. :?

User avatar
RST8
Posts: 64
Joined: Tue Nov 25, 2014 1:57 pm

Re: test for in mcpi

Mon Mar 16, 2015 2:05 pm

Are you just after what type of block ?

Code: Select all

 if (mc.getBlock(x, y, z) == block.COBBLESTONE.id):
    # do something
If you need to know extra information, e.g. the colour of a wool block, then take a look at getBlockWithData().

Joe

theArgoII
Posts: 7
Joined: Fri Jan 02, 2015 9:27 pm

Re: test for in mcpi

Mon Mar 16, 2015 2:33 pm

I'm mainly going for the gold block

User avatar
RST8
Posts: 64
Joined: Tue Nov 25, 2014 1:57 pm

Re: test for in mcpi

Mon Mar 16, 2015 3:01 pm

theArgoII wrote:I'm mainly going for the gold block
gold is
GOLD_BLOCK = Block(41)
so:

Code: Select all

if (mc.getBlock(x, y, z) == block.GOLD_BLOCK.id):
or you could do:

Code: Select all

if (mc.getBlock(x, y, z) == 41):
Joe

theArgoII
Posts: 7
Joined: Fri Jan 02, 2015 9:27 pm

Re: test for in mcpi

Mon Mar 16, 2015 3:03 pm

Okay, I tried that but it didn't do anything.

User avatar
RST8
Posts: 64
Joined: Tue Nov 25, 2014 1:57 pm

Re: test for in mcpi

Mon Mar 16, 2015 3:10 pm

theArgoII wrote:Okay, I tried that but it didn't do anything.
Are you sure that a block has been hit?

Here's some code that you might want to try out. In your game you probably want to be regularly checking to see if a block is hit

Code: Select all

import mcpi.minecraft as minecraft
import time

def checkBlockIsHit():
    # This routine checks to see if the player hit any blocks with the sword
    blockHits = mc.events.pollBlockHits()
    # Were any blocks hit ?
    # If so blockHits will contain a list of them, so we look at each one in turn
    if (blockHits):
        for blockHit in blockHits:
            # find out where the block we hit is
            x, y, z = blockHit.pos
            msg = "x:" + str(x) + ", y:" + str(y) + ", z:" + str(z)
            # What is the block number
            blockType = mc.getBlock(x, y, z)
            mc.postToChat("You hit a block number " + str(blockType) + "  at " + msg)
    else:
        print ("No blocks hit.")


mc = minecraft.Minecraft.create()
mc.postToChat("Draw some blocks, then right click with the sword")

for i in range(0, 500):
    checkBlockIsHit()
    time.sleep(0.5)

theArgoII
Posts: 7
Joined: Fri Jan 02, 2015 9:27 pm

Re: test for in mcpi

Mon Mar 16, 2015 3:13 pm

Oh, I think I see the problem . I was looking for a command to check if a gold block is at curtain cords.

User avatar
RST8
Posts: 64
Joined: Tue Nov 25, 2014 1:57 pm

Re: test for in mcpi

Mon Mar 16, 2015 3:44 pm

theArgoII wrote:Oh, I think I see the problem . I was looking for a command to check if a gold block is at curtain cords.
getblock(x, y, z) does check if a block is at a the specific coordinate.
If you try the following, then a gold block is drawn at 10,10,10, blockType will equal 41, which is a gold block

Code: Select all

import mcpi.minecraft as minecraft
import mcpi.block as block
mc.setBlock(10,10,10,block.GOLD_BLOCK.id)
blockType = mc.getBlock(10, 10, 10)
if (blockType == block.GOLD_BLOCK.id):
  # code will execute, 41 will be posted to the chat window
  mc.postToChat(str(blockType))


theArgoII
Posts: 7
Joined: Fri Jan 02, 2015 9:27 pm

Re: test for in mcpi

Mon Mar 16, 2015 4:49 pm

This is what I've been working on. Along with the game over, if you have any pointers then feel free to share.

Code: Select all

elif (chat== "/flappybird"):
		mc.postToChat("WARNING: THIS GAME WILL LAG DUE TO THE FUNCTIONALITY OF THE PIPES AND BIRD. IF YOU DO     NOT WISH TO CONTINUE PLEASE DO CONTROL + C IN THE TERMONAL TO QUIT")
		gameover= "false"
		score= 1
		mc.setBlocks(115,14,127,127,0,112,0)
		mc.setBlocks(124,4,113,122,4,115,22)
		mc.setBlock(123,5,116,41)
		mc.setBlocks(127,2,127,117,2,127,2)
		mc.setBlocks(127,1,127,117,0,127,3)
		mc.setBlock(123,8,127,41)
		mc.setBlocks(127,0,127,127,13,127,49)
		mc.setBlocks(126,13,127,117,13,127,49)
		mc.setBlocks(116,13,127,116,0,127,49)
		mc.setBlocks(117,0,127,126,0,127,49)
		mc.player.setPos(123.4,5,113.3)
		pos= mc.player.getPos()
		pos.y= pos.y + 3
		time.sleep(5)
		mc.postToChat("How to play:")
		time.sleep(2)
		mc.postToChat("Right click the gold block with a sword to go up (Goes down on its own; like the real thing)")
		mc.postToChat("to avoid the pipes.")
		time.sleep(3)
		mc.postToChat("Starts in...")
		mc.postToChat("3...")
		time.sleep(1)
		mc.postToChat("2...")
		time.sleep(1)
		mc.postToChat("1...")
		time.sleep(1)
		mc.postToChat("GO!")
		py= pos.y
		blockHits= mc.events.pollBlockHits()
		y1= "false"
		while(gameover== "false"):
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			score= score + 0.5
			mc.setBlocks(117,12,127,117,7,127,35,5)
			mc.setBlock(118,7,127,35,5)
			mc.setBlocks(117,4,127,117,3,127,35,5)
			mc.setBlock(118,4,127,35,5)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			time.sleep(1)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			mc.setBlocks(118,12,127,118,7,127,35,5)
			mc.setBlock(119,7,127,35,5)
			mc.setBlocks(118,4,127,118,3,127,35,5)
			mc.setBlock(119,4,127,35,5)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			time.sleep(1)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			mc.setBlocks(119,12,127,119,7,127,35,5)
			mc.setBlock(120,7,127,35,5)
			mc.setBlocks(119,4,127,119,3,127,35,5)
			mc.setBlock(120,4,127,35,5)
			mc.setBlock(117,7,127,35,5)
			mc.setBlock(117,4,127,35,5)
			mc.setBlocks(117,12,127,117,8,127,0)
			mc.setBlock(117,3,127,0)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			time.sleep(1)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			mc.setBlocks(120,12,127,120,7,127,35,5)
			mc.setBlock(121,7,127,35,5)
			mc.setBlocks(120,4,127,120,3,127,35,5)
			mc.setBlock(121,4,127,35,5)
			mc.setBlock(118,7,127,35,5)
			mc.setBlock(118,4,127,35,5)
			mc.setBlocks(118,12,127,118,8,127,0)
			mc.setBlock(118,3,127,0)
			mc.setBlock(117,7,127,0)
			mc.setBlock(117,4,127,0)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			time.sleep(1)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			mc.setBlocks(121,12,127,121,7,127,35,5)
			mc.setBlock(122,7,127,35,5)
			mc.setBlocks(121,4,127,121,3,127,35,5)
			mc.setBlock(122,4,127,35,5)
			mc.setBlock(119,7,127,35,5)
			mc.setBlock(119,4,127,35,5)
			mc.setBlocks(119,12,127,119,8,127,0)
			mc.setBlock(119,3,127,0)
			mc.setBlock(118,7,127,0)
			mc.setBlock(118,4,127,0)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			time.sleep(1)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			time.sleep(1)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			mc.setBlocks(122,12,127,122,7,127,35,5)
			mc.setBlock(123,7,127,35,5)
			mc.setBlocks(122,4,127,122,3,127,35,5)
			mc.setBlock(123,4,127,35,5)
			mc.setBlock(120,7,127,35,5)
			mc.setBlock(120,4,127,35,5)
			mc.setBlocks(120,12,127,120,8,127,0)
			mc.setBlock(120,3,127,0)
			mc.setBlock(119,7,127,0)
			mc.setBlock(119,4,127,0)
			mc.setBlock(117,5,127,35,5)
			mc.setBlock(117,8,127,35,5)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			time.sleep(1)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			mc.setBlocks(123,12,127,123,7,127,35,5)
			mc.setBlock(124,7,127,35,5)
			mc.setBlocks(123,4,127,123,3,127,35,5)
			mc.setBlock(124,4,127,35,5)
			mc.setBlock(121,7,127,35,5)
			mc.setBlock(121,4,127,35,5)
			mc.setBlocks(121,12,127,121,8,127,0)
			mc.setBlock(121,3,127,0)
			mc.setBlock(120,7,127,0)
			mc.setBlock(120,4,127,0)
			blockHits=mc.events.pollBlockHits()
			if blockHits:
				for blockHit in blockHits:
					if(mc.getBlock(blockHit.pos)== 41):
						y1= "true"
			if (y1 == "true"):
				py = py + 1	
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py-1,127,0)
				time.sleep(0.5)
				y1= "false"
			elif (y1== "false"):
				py = py - 1
				time.sleep(0.5)
				mc.setBlock(123,py,127,41)
				mc.setBlock(123,py+1,127,0)
			mc.setBlock(118,5,127,35,5)
			mc.setBlock(118,8,127,35,5)
			mc.setBlocks(117,5,127,117,3,127,35,5)
			mc.setBlocks(117,12,127,117,9,127,35,5)
THIS IS WHAT I HAVE AS OF NOW. IT SPAWNS IN TWO PIPES AND LETS THE BIRD MOVE BUT I NEED THE TEST FOR BLOCK COMMAND TO USE IN THE GAME OVER FUNCTION.
THIS IS LIKE FLAPPYBIRD SO IT ALSO NEEDS TO GAMEOVER WHEN IT HITS A PIPE. IF YOU WANT TO SEE IT IN ACTION PUT IT IN AND SEE WHAT IT DOES BUT YOULL NEED TO CTRL + C OUT OF IT./code]

User avatar
RST8
Posts: 64
Joined: Tue Nov 25, 2014 1:57 pm

Re: test for in mcpi

Mon Mar 16, 2015 6:23 pm

OK, longish post coming up.
I've never actually played flappy bird, but I think I can see what you are trying to do. However, your code isn't showing up properly so there's no indentation, making it harder to figure out and I can't easily run it without guessing the indentation.

Flappy bird might appear to be a simple game, but there is a fair amount going on code wise and it's fair chunk of work to try and emulate it in Minecraft.

I think you need to step back a bit and spend some time looking at coding loops, you've got lots of repeated cut and pasted code, making the program very long and hard to read and fix.
Lets start with something very simple like your count down :

Code: Select all

mc.postToChat("Starts in...")
mc.postToChat("3...")
time.sleep(1)
mc.postToChat("2...")
time.sleep(1)
mc.postToChat("1...")
time.sleep(1)
mc.postToChat("GO!")
If we put that into a loop

Code: Select all

mc.postToChat("Starts in...")
for i in range (3,0,-1):
    mc.postToChat(str(i) + "...")
    time.sleep(1)
mc.postToChat("GO!")
In your code, if you decided you wanted to extend the counter to 5 seconds, you'd be cutting and pasting more code. With the loop version, you would just change the "3" to "5". In this simple case you might think it isn't worth the effort, but looking at your code, you've got whole sections which are doing the same thing and a bug in one section will be repeated all down the code.

You've several of the following
y1 = "true"
if (y1 == "true"):
Whilst this will work, use the constants True and False would be less error prone

Code: Select all

y1 = True
if (y1):
   # Code if True, if (y1) is short form for if (y1 == True)
For the false case:

Code: Select all

y1 = False
if (y1==False):
   # code if false
At the moment all the coordinates are hard coded, rather than changing according to a loop, making it harder to change. What you also need is a collision detection routine, which compares the coordinates of the bird with the coordinates of the pipes being drawn. The game will also be the same every time you run it.

The code really needs breaking down into small clearly defined chunks.
There should be a function for checking if the player has hit the gold block, which is called on a regular basis. One way of doing this is to use threads, but I suspect that's only going to further confuse you at the moment.

Whilst it's no fun throwing away code you've worked on, to get your current code to a point where it's going to run is going to be a lot more work and the result will be fragile.
I know that's probably not the answer you were hoping for, but I'm not sure myself or someone else getting the code to run in it's current form is going to help you just yet.

I have a bunch of Minecraft code up on:
https://github.com/joedeller/pymine
If you look at the whackamole.py game, you can see that it's a fair amount of code even for a relatively simple game and it uses threads for moving the moles and checking if something is hit. There are distinct parts of the code that do specific jobs and even then, it's still pretty messy and could do with cleaning up.

I hope I haven't put you off, keep at it, but to move forward sometimes you've really got to step back.

Joe

Return to “Python”