Picture It Picture It Very Basic Game Very Basic Game Picture Picture Pepper
Jan 05, 2016
Picture It Picture It Very Basic Game PictureVery Basic Game PicturePepper
Original GameOriginal Gameimport 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=diceThrow(name); // returns value of 2 dice place=movePlayer(name, place, sum); // returns new location of player } System.out.println("You are now on square "+place + " GAME IS DONE."); } public static int diceThrow(String name) { return 12; // your diceThrow code will send back something better and print it. } public static int movePlayer(String name, int startPt, int numberToMove) {
System.out.println("You are now on square " + (( numberToMove + startPt ) %100));
return ( numberToMove + startPt ) %100; } }
Board Creation - new class 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 MyWorld {public static WorldImage drawGame (int loc) { WorldImage board =
AImage.makeRectangle(500,500, Color.yellow, Mode.solid);
return board; } }
See the Board - change See the Board - change gamegame Add a canvas:import javalib.worldcanvas.*;import java.util.Scanner;public class Game { public static void main() { WorldCanvas gameSpace = new WorldCanvas(500,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 gameSpace.show(); gameSpace.drawImage(MyWorld.drawGame(place)); for(int turn=1; turn<=10; turn++) { int sum=diceThrow(name); // returns value of 2 dice place=movePlayer(name, place, sum); // returns new location
of player } System.out.println("You are now on square "+place + " GAME IS
DONE."); }
Start Board DrawingStart Board Drawing
import javalib.worldimages.*;import java.awt.Color;public class MyWorld {public static WorldImage drawGame (int loc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow,
Mode.solid); // Create a vertical and horizontal line WorldImage horizLine = AImage.makeLine(new Posn(0,0),new
Posn(500,0)); WorldImage verticalLine = AImage.makeLine(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(horizLine,250,50); board = board.place(verticalLine,50,250); return board; } }
Finish Board PictureFinish Board Picture
import javalib.worldimages.*;import java.awt.Color;public class MyWorld {public static WorldImage drawGame (int loc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow,
Mode.solid); WorldImage horizLine = AImage.makeLine(new Posn(0,0),new
Posn(500,0)); WorldImage verticalLine = AImage.makeLine(new Posn(0,0),new
Posn(0,500)); for (int count = 50; count <= 500; count= count + 50){ board = board.place(horizLine,250,count); board = board.place(verticalLine,count,250); }
return board; } }
Add Player - Place Add Player - Place anywhereanywhereimport javalib.worldimages.*;import java.awt.Color;public class MyWorld {public static WorldImage drawGame (int loc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow,
Mode.solid); WorldImage horizLine = AImage.makeLine(new Posn(0,0),new
Posn(500,0)); WorldImage verticalLine = AImage.makeLine(new Posn(0,0),new
Posn(0,500)); for (int count = 50; count <= 500; count= count + 50){ board = board.place(horizLine,250,count); board = board.place(verticalLine,count,250); } WorldImage hisPiece = AImage.makeCircle (10, Color.blue, Mode.solid); WorldImage game = board.place(hisPiece, loc, loc) ; return game; } }
Put Player on correct Put Player on correct squaresquareimport javalib.worldimages.*;import java.awt.Color;public class MyWorld {public static WorldImage drawGame (int loc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow,
Mode.solid); WorldImage horizLine = AImage.makeLine(new Posn(0,0),new
Posn(500,0)); WorldImage verticalLine = AImage.makeLine(new Posn(0,0),new
Posn(0,500)); for (int count = 50; count <= 500; count= count + 50){ board = board.place(horizLine,250,count); board = board.place(verticalLine,count,250); } WorldImage hisPiece = AImage.makeCircle (10, Color.blue, Mode.solid); WorldImage game = board.place(hisPiece, (loc%10)*50-25,
loc/10*50+25) ;return game; } }
Game redraw and slow Game redraw and slow downdown Add a canvas:import javalib.worldcanvas.*;import java.util.Scanner;public class Game { public static void main() { WorldCanvas gameSpace = new WorldCanvas(500,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 gameSpace.show(); gameSpace.drawImage(MyWorld.drawGame(place)); for(int turn=1; turn<=10; turn++) { int sum=diceThrow(name); // returns value of 2 dice place=movePlayer(name, place, sum); // returns new location of player gameSpace.drawImage(MyWorld.drawGame(place)); System.out.println( " Press Enter to continue."); scan.nextLine(); } System.out.println("You are now on square "+place + " GAME IS DONE."); }
Your Turn - Change your Your Turn - Change your gamegameGoal: Just want to move a piece around an
empty board. Create a new class to draw called MyWorldimport javalib.worldimages.*;import java.awt.Color;public class MyWorld {public static WorldImage drawGame (int loc) { WorldImage board =
AImage.makeRectangle(500,500, Color.yellow, Mode.solid);
return board; } }
Change your game to drawChange 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() {
WorldCanvas gameSpace = new WorldCanvas(500,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
gameSpace.show();
gameSpace.drawImage(MyWorld.drawGame(place));
for(int turn=1; turn<=10; turn++)
{ int sum=diceThrow(name); // returns value of 2 dice
place=movePlayer(name, place, sum); // returns new location of player
gameSpace.drawImage(MyWorld.drawGame(place));
System.out.println( " Press Enter to continue.");
scan.nextLine();
}
System.out.println("You are now on square "+place + " GAME IS DONE.");
}
public static int diceThrow(String name) {
return 12; // your diceThrow code will send back something better and print it.
}
public static int movePlayer(String name, int startPt, int numberToMove) {
System.out.println("You are now on square " + (( numberToMove + startPt ) %100));
return ( numberToMove + startPt ) %100;
} }
Code MyWorld drawGame Code MyWorld drawGame methodmethodAdd any shaped piece to your board -
ex:WorldImage hisPiece = AImage.makeCircle (10, Color.blue, Mode.solid);
Place the piece onto the board:WorldImage game = board.place(hisPiece, loc, loc) ;
Return game instead nowreturn game;
Change the piece to move as you please by adjusting the piece's x and y
SummarySummaryKnow how to use WorldCanvas
objectSeparated 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 happensVisualized how you can translate a
location to a spot on a game board