9 Tron - tron tron 1 get() get() Returns the color of one...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: tron tron 1 get() get() Returns the color of one pixel if(get(x,y) != color(0,0,0)) { //lots of java //lots } 2 How we could check How we could check for a crash with get() int x = 10; int y = 150; void setup() { size(300,300); size(300,300); background(0); background(0); fill(255); fill(255); ellipse(270,150,20,20); ellipse(270,150,20,20); } void draw() { if(get(x,y) != color(0,0,0)) //we crashed! if(get(x,y) { fill(255,0,0); fill(255,0,0); ellipse(150,150,250,250); ellipse(150,150,250,250); } else else { stroke(255,0,255); stroke(255,0,255); point(x,y); point(x,y); x++; x++; } } 3 How we could check How we could check for a crash with get() int x = 10; int y = 150; void setup() { size(300,300); size(300,300); background(0); background(0); fill(255); fill(255); ellipse(270,150,20,20); ellipse(270,150,20,20); } void draw() { if(get(x,y) != color(0,0,0)) //we crashed! if(get(x,y) { fill(255,0,0); fill(255,0,0); ellipse(150,150,250,250); ellipse(150,150,250,250); } else else { stroke(255,0,255); stroke(255,0,255); point(x,y); point(x,y); x++; x++; } } 4 Writing tron Writing tron Don't try to write the program all at once Get one detail working and then move on to the next thing Let's start by making the light trail 5 Writing tron Writing tron The next thing would be to make the direction change based on what's in key 6 Writing tron Writing tron Now, we'd probably want to use get() to see if we ran into our own light trail 7 Oops, the game is over as soon as it Oops, the game is over as soon as it started! The problem is that we aren't moving If we stay in the same place we'll put a dot on 8 a dot Keep moving Keep moving In setup() we can set key to get the dot moving In my program I used: key = 'd' Stopping is sort of the opposite If I've run into a light trail I'll set key to some unused value like: key = 'x' 9 Adding a computer opponent Adding a computer opponent Before I add the computer, let's clean up the code draw() is getting ugly let's move the human code into its own function 10 Adding a computer opponent Adding a computer opponent There, that's better Now we can use the human() function as a guide computer() will be similar 11 Adding a computer opponent Adding a computer opponent The computer opponent will need it's own set of variables to keep track of its location and direction 12 Adding a computer opponent Adding a computer opponent We will write a function that is very similar to human() called computer() In computer(), will use compX, compY, etc., instead of x & y 13 Adding a computer opponent Adding a computer opponent The problem now is that our computer won't turn We need to add code to make it "smart" Sometimes, this is called "AI" for artificial intelligence 14 Adding a computer opponent Adding a computer opponent In each of the four if statements of the if statements of the computer, we will look ahead 1 place to see if we are going to run into something If we are, we'll turn by changing the value of compDirection 15 for(int i = 0; i <= 3; i++) { if(i != 0 && i <2) if(i { print("A"); print("A"); } else if (i ==2 || i == 3) else { print("B"); print("B"); } else else { print("C"); print("C"); } } Practice Practice Quiz Question: Find the output 16 Ending the game Ending the game Here's a problem The Human crashes and the game displays "Human Loses" But, the game keeps going, and eventually the computer crashes and the game displays "Computer Loses!" 17 Ending the game Ending the game I need to stop the game as soon as one player loses, otherwise things will get messed up I'll create a boolean variable to do this 18 Ending the game Ending the game When a player crashes, I'll change the variable to show the game is over 19 Ending the game Ending the game In draw(), I'll make sure that the players only move is the game isn't over 20 Making the line thicker Making the line thicker We can make a bigger dot by making a rectangle instead of a point Since rects have a stroke and fill color, we have a should set both to the same color 21 Making the line thicker Making the line thicker Now there is some funny behavior Up and left are fine, but we crash as soon as we turn down or right! 22 Making the line thicker Making the line thicker Look carefully at rect(x,y,5,5); x and y are the coordinates of the top left corner 5 is the length of each side 23 Making the line thicker Making the line thicker If I take one step to the left (x--), there's no problem (x,y) lands on a black part of the screen But, if I move to the right. . . 24 Making the line thicker Making the line thicker After I take one step to the right (x++) (x,y) lands on part of the previous rectangle It's not black, so I crash and die : ( 25 Making the line thicker Making the line thicker Instead of moving by a distance of 1, I need to move by a distance of one more than the side length That way I don't land on a part of the previous rectangle 26 Making the line thicker Making the line thicker This is a great place for a variable We can call it dotSize (for example) Just make sure that the distance we move is ONE MORE THAN the side length When we "look ahead" we also need to use ONE MORE THAN the side length 27 Practice Quiz Questions Practice Quiz Questions 1. How many variables would be needed for a tron player that can change position and direction? 2. Write an if statement that checks to see if the position above a tron player (toward the top of the screen) is blocked. 3. The following program was intended to display 3 (the sum of the first two values in the array) but instead it generates an error. Use the int() function twice to fix the problem. String numbers = {"1", "2", "77"}; int numOne = numbers[0]; int numTwo = numbers[1]; println(numOne + numTwo); 4. What would the following program display. (Hint: what happens when you add Strings?) String numbers = {"1", "2", 28 "77"}; ...
View Full Document

This note was uploaded on 01/13/2012 for the course CS 12 taught by Professor Ma during the Spring '08 term at UC Davis.

Ask a homework question - tutors are online