Guest User

java

a guest
Feb 6th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class Game extends JFrame{
  6.    
  7.     Rectangle hero = new Rectangle(0, 0, 50, 50);
  8.     int score = 0;
  9.     int gX, gY;
  10.     long startTime, endTime;
  11.     int seconds;
  12.    
  13.     public static void main(String[] args){
  14.         new Game();
  15.         }
  16.        
  17.        
  18.     public Game(){
  19.        
  20.        
  21.         setSize(400,400); //Size
  22.         setTitle("Pick'n Stick"); //Name of the game
  23.         setDefaultCloseOperation(3);
  24.         setVisible(true);
  25.         setResizable(false);
  26.        
  27.         addKeyListener(new Input());
  28.        
  29.         moveGobbler();
  30.  
  31.        
  32.         hero.x = 50;
  33.         hero.y = 50;
  34.        
  35.         startTime = System.currentTimeMillis();
  36.        
  37.        
  38.     }
  39.     public void paint(Graphics g){
  40.        
  41.         if(hero.intersects(gX,gY,20,20)){
  42.            
  43.             endTime = System.currentTimeMillis();
  44.             seconds = (int) (endTime - startTime) / 1000;
  45.            
  46.             moveGobbler();
  47.             score += 100 - seconds;
  48.             startTime = endTime;
  49.         }
  50.        
  51.         Image offScreen = createImage(getWidth(), getHeight());
  52.         draw(offScreen.getGraphics());
  53.        
  54.         g.drawImage(offScreen, 0, 0, null);
  55.        
  56.        
  57.     }
  58.     public void draw(Graphics g)  //COLLOR AND FORMS
  59.     {
  60.        
  61.         g.setColor(Color.GREEN); //Color of the Background
  62.         g.fillRect(0, 0, 400, 400);
  63.        
  64.         g.setColor(Color.YELLOW);
  65.         g.fillOval(gX,gY,20,20);
  66.        
  67.         g.setColor(Color.BLUE); //Color of the "hero
  68.         g.fillRect(hero.x,  hero.y, 50, 50);
  69.        
  70.         g.setColor(Color.BLACK); //Score
  71.         g.drawString("Score; " + score, 25, 55);
  72.                
  73.         repaint();
  74.     }
  75.     private void moveGobbler()
  76.     {
  77.        
  78.         gX = (int) (Math.random() * 400);
  79.         gY = (int) (Math.random() * 400);
  80.        
  81.         if(gY < 30)
  82.             gY = 30;
  83.         else if(gY > 380)
  84.             gY = 380;
  85.            
  86.         if (gX > 380)
  87.             gX = 380;
  88.        
  89.     }
  90.     private class Input implements KeyListener
  91.     {
  92.  
  93.         @Override
  94.         public void keyPressed(KeyEvent e) {   
  95.             int keyCode = e.getKeyCode();
  96.            
  97.             if(keyCode == e.VK_UP){
  98.                 if(hero.y > 25)
  99.                     hero.y -= 7;
  100.                 else
  101.                     hero.y = 25;
  102.             }
  103.            
  104.             if(keyCode == e.VK_DOWN){
  105.                 if(hero.y < 350)
  106.                     hero.y += 7;
  107.                 else
  108.                     hero.y = 350;
  109.             }
  110.            
  111.             if(keyCode == e.VK_LEFT){
  112.                 if(hero.x > 0)
  113.                     hero.x -= 7;
  114.                 else
  115.                     hero.x = 0;
  116.             }
  117.             if(keyCode == e.VK_RIGHT){
  118.                 if(hero.x < 350)
  119.                     hero.x += 7;
  120.                 else
  121.                     hero.x = 350;
  122.             }  
  123.            
  124.         }
  125.        
  126.         @Override
  127.         public void keyReleased(KeyEvent arg0) {
  128.    
  129.            
  130.         }
  131.  
  132.         @Override
  133.         public void keyTyped(KeyEvent arg0) {
  134.            
  135.         }
  136.        
  137.        
  138.        
  139.     }
  140.        
  141. }
Advertisement
Add Comment
Please, Sign In to add comment