Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. /**
  2.  * Using graphics, timer, canvas, key events, and OOP, let's make pac man!
  3.  */
  4. //imports
  5. //Graphics, GUI, Keyboard Interaction
  6. import java.awt.*;
  7. import java.awt.geom.*;
  8. import java.awt.event.KeyEvent;
  9. import javax.swing.JFrame;
  10. //We know Random
  11. import java.util.Random;
  12. //Timer is for animations
  13. import java.util.Timer;
  14.  
  15. //PacGame is our interface for the game, window, gui, playing space
  16. public class PacGame extends Canvas {
  17.  
  18. //Declare some variables
  19. PacMan player;
  20.  
  21.     public PacGame() {
  22.         //adds a key event for keyboard interactions
  23.         enableEvents(java.awt.AWTEvent.KEY_EVENT_MASK);
  24.         requestFocus(); //makes sure the window is in focus
  25.        
  26.         //initialize our shapes
  27.         player = new PacMan();
  28.                
  29.         Timer t = new Timer(true);
  30.         t.schedule(new java.util.TimerTask()
  31.         {
  32.             public void run()
  33.             {
  34.                 doStuff();
  35.                 repaint();
  36.             }
  37.            
  38.         }, 10, 10);
  39.            
  40.     }
  41.    
  42.     public void paint(Graphics g) {
  43.         player.draw(g);
  44.     }
  45.    
  46.     //this method will run when a key event occurs
  47.    
  48.     public void processKeyEvent(KeyEvent e){
  49.        
  50.         if (e.getID() == KeyEvent.KEY_PRESSED)
  51.         {
  52.             if (e.getKeyCode() == KeyEvent.VK_W)
  53.             {
  54.                 player.direction = "up";
  55.             }
  56.             if (e.getKeyCode() == KeyEvent.VK_S)
  57.             {
  58.                 player.direction = "down";
  59.             }
  60.             if (e.getKeyCode() == KeyEvent.VK_A)
  61.             {
  62.                 player.direction = "left";
  63.             }
  64.             if (e.getKeyCode() == KeyEvent.VK_D)
  65.             {
  66.                 player.direction = "right";
  67.             }          
  68.         }
  69.         if (e.getID() == KeyEvent.KEY_RELEASED)
  70.         {
  71.             if (e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_S ||e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_D )
  72.             {
  73.                 player.direction = "";
  74.             }
  75.         }
  76.            
  77.     }
  78.        
  79.     public static void main(String[] args) {
  80.         JFrame win = new JFrame("Pac-Man");
  81.         win.setSize(1010,735);
  82.         win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  83.         //Pong canvas = ;
  84.         win.add(new PacGame());
  85.         win.setVisible(true);
  86.     }
  87.     //where stuff happens
  88.     public void doStuff() {
  89.        
  90.     }
  91.    
  92.    
  93.    
  94.     public boolean isFocusable() {return true;}
  95.  
  96.  
  97.  
  98.      
  99. }
  100.  
  101. class PacMan extends Canvas {
  102.     //TODO: ADD ANIMATION FRAMES
  103.     int speed, score, lives, x, y, w;
  104.     String direction; //in other games you might need a moveLR and moveUD - no need for pacman
  105.     Color body;
  106.     boolean isInvul;
  107.    
  108.     public PacMan() {
  109.         speed = 1;
  110.         body = Color.yellow;
  111.         direction = "";
  112.         score = 0;
  113.         lives = 3;
  114.         isInvul = false;
  115.         x = 100;
  116.         y = 100;
  117.         w = 150;
  118.     }
  119.    
  120.     public void draw(Graphics g) {
  121.         g.setColor(body);
  122.         drawCenteredCircle(g,x,y,w);
  123.     }
  124.    
  125.     public void drawCenteredCircle(Graphics g, int x, int y, int w) {
  126.      int a = x - (w/2);
  127.      int b = y - (w/2);
  128.      g.fillOval(a,b,w,w);
  129.     }
  130. }
  131.  
  132. class Wall extends Canvas {
  133.     public Wall() {
  134.        
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement