Download Picture It Very Basic Game Picture

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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=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
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 game
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 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 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 anywhere
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);
}
WorldImage hisPiece = AImage.makeCircle (10, Color.blue, Mode.solid);
WorldImage game = board.place(hisPiece, loc, loc) ;
return game;
} }
Put Player on correct square
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);
}
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 down
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 game
Goal: Just want to move a piece around an empty
board.
 Create a new class to draw called MyWorld
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;
} }

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()
{
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 method

Add 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 now
return game;

Change the piece to move as you please
by adjusting the piece's x and y
Summary
Know how to use WorldCanvas 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

Related documents