Turtle drawing games
These arrays should go in the same section as the gridpos array, for organization's sake. We also need to declare a whole ton of variables - 4 for each box - which will be used later. We can do that just below the array initialization section. This saves line space and some time. This is the part where things start getting repetitive. Each section to place a box is almost exactly the same as the last.
First, make a new section for box1. First, we want to set its speed to 0 so that it draws as fast as possible. Then, we're going to set its color - in this example, I'm using 8B, because it's a nice brownish color. Then, we're going to go up to pick up the pen and go to a set coordinate. For now, let's just go to the top right grid square. To do this, we need to tell box1 to goto xlist[9],ylist[9].
Next, we're going to put down the pen and begin the fill. After that, we need to draw a square of side length 40, because our grid squares are side 50 and we have a margin of 5 on each side. This can be done with the same loop we used to draw the area, but instead of going forward we go forward Finally, we need to end the fill. If you run it now, you should see a brown box appear in the top right corner. That means it's worked! If it hasn't, see this code:. Now, you just need to do the same thing for boxes 2 and 3, but with a different value in the xlist[] and ylist[].
See what experimenting with it does to the position! Now, I know what you're thinking. That takes away some of the fun! Let's make a randomization aspect. First, up in the import section, we need to import random. This will let us use a very important function - random. Now, we need to create a new section above the drawing of box1 - let's do it directly below the grid section. This is where we're going to start using those variables we created.
First, we need to assign box1gridx. This will tell box1gridx to become a random integer between 0 and 9, so that it encompasses the whole of the array.
Then, do the same for box1gridy. Now, we need to assign box1x to a number in xlist[] based on box1gridx , and the same for box1y.
This is simple, based on what we know about arrays. Now, just for debug purposes, let's print the x and y positions to the console using print box1x,box1y. Finally, we need to change the goto positions when drawing box1 from xlist[9],ylist[9] to box1x,box1y.
If you did the randomization right, it should look like this:. Something you may see come up - very rarely, but it could happen - is multiple boxes in the same spot. Now, while this is technically OK, we don't want it because it limits the game's potential.
So, for box2 , we're going to use the same code as for box1 slightly modified to apply to box2 , but we're going to add something to it. We need to check to see if box2x is the same as box1x and if box2y is the same as box1y. If they are the same, then we need to give box2 a new position. We can do this by putting the whole of the randomization code for box2 in a while True: loop There are better ways to do this, but this is a simple way - you do NOT want to run this until you have put a break into it , then putting in an if statement.
Note that we need to check for both, because just one being the same is OK. Then, under that if it doesn't really matter what we put, so I just put a print statement saying that the placement of this box failed. What is very important is the else. We need to make sure we have an else after the if that contains the line break.
Otherwise, we will be stuck in this loop forever. So, before running, double check that you have this code for box2 's randomization:. Finally, let's fix box2 's goto so that it goes to box2x , box2y. Now, to do this for more boxes it's very similar, but you need to check the position against every single previous box's position. Fill in that randomization and its corresponding goto, and then press run. You should see all 3 boxes go to completely random spots each time!
Now, it's time to draw our player. This will eventually be moving around on the grid and collecting the boxes, but for now let's just try and draw it. To make it simple and easily distinguishable from the boxes, we're going to choose a different color and shape. To draw the circle, you could use turtle.
Because of this, we need a new array called playerposlist. The values in this will be 25 away from each value in gridpos , because our grid boxes are 50x So, we'll start at , then , , etc.
Next, we need to actually create our player turtle and hide it, both in their respective sections. Now, we need to create 2 variables to track player 's x position and y position in relation to the grid - playerx and playery. Let's set both of them to 4 for now. Example: penup. Explained in lesson: Controlling the Turtle and Pen. Example: pendown. Example: repeat 4 [ wait 10 fd 50]. Explained in lesson: Hi wait. Example: setwidth 4 fd Explained in lesson: The pen width.
Example: hideturtle. Example: ht wait st. Example: home. Example: setx Example: sety Example: setxy Example: sh Example: ARC 5. Example: ellipse 80 Example: pos. Explained in lesson: The turtle answer. Example: xcor. Parragon Books Teena Drawissimo Kids How Rise Of The Teenage Digital Art Te Tmnt Ninja Turtles M Why Ninja Turtles Is Coloring Ninja Turtl Tags: ninja , turtles , games. All rights to paintings and other images found on PaintingValley.
All the materials are intended for educational purposes only. If you consider that any of the materials violates your rights, and you do not want your material to be displayed on this website, please get in touch with us via "contact us" page and your copyrighted material will be immediately removed. Log in Sign up. Ninja Turtles Drawing Games. Like JPG. Like PNG. If you are interested in helping translate to other languages please contact me. You can issue commands and see the results graphically.
You can also get introduced to the concept of looping. There is an easy mode called "Command Wizard" that can be used to build commands if typing is not your thing. It has gained around installs so far, with an average rating of 3.
0コメント