Picture It Very Basic Game Picture Pepper Original
Picture It Very Basic Game Picture Pepper
Original Game import java. util. Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System. in); String name= "Pepper"; // hard code name for example int place=1; //place is the position on the game board for(int turn=1; turn<=10; turn++) { int sum=dice. Throw(name); // returns value of 2 dice place=move. Player(name, place, sum); // returns new location of player } System. out. println("You are now on square "+place + " GAME IS DONE. "); } public static int dice. Throw(String name) { return 12; // your dice. Throw code will send back something better and print it. } public static int move. Player(String name, int start. Pt, int number. To. Move) { System. out. println("You are now on square " + (( number. To. Move + start. Pt ) %100)); return ( number. To. Move + start. Pt ) %100; }}
Board Creation - new class Create a class that has a method to draw your game. It needs to know the location of your piece. import javalib. worldimages. *; import java. awt. Color; public class My. World { public static World. Image draw. Game (int loc) { World. Image board = AImage. make. Rectangle(500, Color. yellow, Mode. solid); return board; } }
See the Board - change game Add a canvas: import javalib. worldcanvas. *; import java. util. Scanner; public class Game { public static void main() { World. Canvas game. Space = new World. Canvas(500, "game place") ; Scanner scan=new Scanner(System. in); String name= "Pepper"; // hard code name for example int place=1; //place is the position on the game board game. Space. show(); game. Space. draw. Image(My. World. draw. Game(place)); for(int turn=1; turn<=10; turn++) { int sum=dice. Throw(name); // returns value of 2 dice place=move. Player(name, place, sum); // returns new location of player } System. out. println("You are now on square "+place + " GAME IS DONE. "); }
Start Board Drawing import javalib. worldimages. *; import java. awt. Color; public class My. World { public static World. Image draw. Game (int loc) { World. Image board = AImage. make. Rectangle(500, Color. yellow, Mode. solid); // Create a vertical and horizontal line World. Image horiz. Line = AImage. make. Line(new Posn(0, 0), new Posn(500, 0)); World. Image vertical. Line = AImage. make. Line(new Posn(0, 0), new Posn(0, 500)); // See how it looks with the center of horizontal at 250, 50 and vertical at 50/250 board = board. place(horiz. Line, 250, 50); board = board. place(vertical. Line, 50, 250); return board; } }
Finish Board Picture import javalib. worldimages. *; import java. awt. Color; public class My. World { public static World. Image draw. Game (int loc) { World. Image board = AImage. make. Rectangle(500, Color. yellow, Mode. solid); World. Image horiz. Line = AImage. make. Line(new Posn(0, 0), new Posn(500, 0)); World. Image vertical. Line = AImage. make. Line(new Posn(0, 0), new Posn(0, 500)); for (int count = 50; count <= 500; count= count + 50){ board = board. place(horiz. Line, 250, count); board = board. place(vertical. Line, count, 250); } return board; } }
Add Player - Place anywhere import javalib. worldimages. *; import java. awt. Color; public class My. World { public static World. Image draw. Game (int loc) { World. Image board = AImage. make. Rectangle(500, Color. yellow, Mode. solid); World. Image horiz. Line = AImage. make. Line(new Posn(0, 0), new Posn(500, 0)); World. Image vertical. Line = AImage. make. Line(new Posn(0, 0), new Posn(0, 500)); for (int count = 50; count <= 500; count= count + 50){ board = board. place(horiz. Line, 250, count); board = board. place(vertical. Line, count, 250); } World. Image his. Piece = AImage. make. Circle (10, Color. blue, Mode. solid); World. Image game = board. place(his. Piece, loc) ; return game; } }
Put Player on correct square import javalib. worldimages. *; import java. awt. Color; public class My. World { public static World. Image draw. Game (int loc) { World. Image board = AImage. make. Rectangle(500, Color. yellow, Mode. solid); World. Image horiz. Line = AImage. make. Line(new Posn(0, 0), new Posn(500, 0)); World. Image vertical. Line = AImage. make. Line(new Posn(0, 0), new Posn(0, 500)); for (int count = 50; count <= 500; count= count + 50){ board = board. place(horiz. Line, 250, count); board = board. place(vertical. Line, count, 250); } World. Image his. Piece = AImage. make. Circle (10, Color. blue, Mode. solid); World. Image game = board. place(his. Piece, (loc%10)*50 -25, loc/10*50+25) ; return game; } }
Game redraw and slow down Add a canvas: import javalib. worldcanvas. *; import java. util. Scanner; public class Game { public static void main() { World. Canvas game. Space = new World. Canvas(500, "game place") ; Scanner scan=new Scanner(System. in); String name= "Pepper"; // hard code name for example int place=1; //place is the position on the game board game. Space. show(); game. Space. draw. Image(My. World. draw. Game(place)); for(int turn=1; turn<=10; turn++) { int sum=dice. Throw(name); // returns value of 2 dice place=move. Player(name, place, sum); // returns new location of player game. Space. draw. Image(My. World. draw. Game(place)); System. out. println( " Press Enter to continue. "); scan. next. Line(); } System. out. println("You are now on square "+place + " GAME IS DONE. "); }
Your Turn - Change your game Goal: Just want to move a piece around an empty board. Create a new class to draw called My. World import javalib. worldimages. *; import java. awt. Color; public class My. World { public static World. Image draw. Game (int loc) { World. Image board = AImage. make. Rectangle(500, Color. yellow, Mode. solid); return board; } }
Change your game to draw Use your homework code and fill in the red and blue parts, or use my code to start: import javalib. worldcanvas. *; import java. util. Scanner; public class Game { public static void main() { World. Canvas game. Space = new World. Canvas(500, "game place") ; Scanner scan=new Scanner(System. in); String name= "Pepper"; // hard code name for example int place=1; //place is the position on the game board game. Space. show(); game. Space. draw. Image(My. World. draw. Game(place)); for(int turn=1; turn<=10; turn++) { int sum=dice. Throw(name); // returns value of 2 dice place=move. Player(name, place, sum); // returns new location of player game. Space. draw. Image(My. World. draw. Game(place)); System. out. println( " Press Enter to continue. "); scan. next. Line(); } System. out. println("You are now on square "+place + " GAME IS DONE. "); } public static int dice. Throw(String name) { return 12; // your dice. Throw code will send back something better and print it. } public static int move. Player(String name, int start. Pt, int number. To. Move) { System. out. println("You are now on square " + (( number. To. Move + start. Pt ) %100)); return ( number. To. Move + start. Pt ) %100; }}
Code My. World draw. Game method Add ex: any shaped piece to your board - World. Image his. Piece = AImage. make. Circle (10, Color. blue, Mode. solid); Place the piece onto the board: World. Image game = board. place(his. Piece, loc) ; Return game instead now return game; Change the piece to move as you please by adjusting the piece's x and y
Summary Know how to use World. Canvas object Separated some drawing knowledge out of your game class ◦ Called a method in another class (like tester) Used scanner to slow down the game so you can see what happens Visualized how you can translate a location to a spot on a game board
- Slides: 13