1 / 13

Picture It Very Basic Game Picture

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

yeva
Download Presentation

Picture It Very Basic Game Picture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Picture It Very Basic Game Picture Pepper

  2. 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++) { intsum=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 intdiceThrow(String name) { return 12; // your diceThrow code will send back something better and print it. } public static intmovePlayer(String name, intstartPt, intnumberToMove) { System.out.println("You are now on square " + (( numberToMove + startPt ) %100)); return ( numberToMove + startPt ) %100; } }

  3. 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 WorldImagedrawGame (intloc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow, Mode.solid); return board; } }

  4. See the Board - change game • Add a canvas: import javalib.worldcanvas.*; import java.util.Scanner; public class Game { public static void main() { WorldCanvasgameSpace = 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."); }

  5. Start Board Drawing import javalib.worldimages.*; import java.awt.Color; public class MyWorld { public static WorldImagedrawGame (intloc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow, Mode.solid); // Create a vertical and horizontal line WorldImagehorizLine = AImage.makeLine(new Posn(0,0),new Posn(500,0)); WorldImageverticalLine = 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; } }

  6. Finish Board Picture import javalib.worldimages.*; import java.awt.Color; public class MyWorld { public static WorldImagedrawGame (intloc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow, Mode.solid); WorldImagehorizLine = AImage.makeLine(new Posn(0,0),new Posn(500,0)); WorldImageverticalLine = 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; } }

  7. Add Player - Place anywhere import javalib.worldimages.*; import java.awt.Color; public class MyWorld { public static WorldImagedrawGame (intloc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow, Mode.solid); WorldImagehorizLine = AImage.makeLine(new Posn(0,0),new Posn(500,0)); WorldImageverticalLine = 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); } WorldImagehisPiece = AImage.makeCircle (10, Color.blue, Mode.solid); WorldImage game = board.place(hisPiece, loc, loc) ; return game; } }

  8. Put Player on correct square import javalib.worldimages.*; import java.awt.Color; public class MyWorld { public static WorldImagedrawGame (intloc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow, Mode.solid); WorldImagehorizLine = AImage.makeLine(new Posn(0,0),new Posn(500,0)); WorldImageverticalLine = 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); } WorldImagehisPiece = AImage.makeCircle (10, Color.blue, Mode.solid); WorldImage game = board.place(hisPiece, (loc%10)*50-25, loc/10*50+25) ; return game; } }

  9. Game redraw and slow down • Add a canvas: import javalib.worldcanvas.*; import java.util.Scanner; public class Game { public static void main() { WorldCanvasgameSpace = 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."); }

  10. 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 WorldImagedrawGame (intloc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow, Mode.solid); return board; } }

  11. 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() { WorldCanvasgameSpace = 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 intdiceThrow(String name) { return 12; // your diceThrow code will send back something better and print it. } public static intmovePlayer(String name, intstartPt, intnumberToMove) { System.out.println("You are now on square " + (( numberToMove + startPt ) %100)); return ( numberToMove + startPt ) %100; } }

  12. Code MyWorlddrawGame method • Add any shaped piece to your board - ex: WorldImagehisPiece = 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

  13. 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

More Related