Advertisement
MrTra1tor

Why doesn't my hero show up ingame?

Oct 31st, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.89 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. import java.awt.event.*;
  4. import java.awt.*;
  5. import java.util.ArrayList;
  6.  
  7.  
  8. public class Game extends JFrame{
  9.    
  10.     Image packy;
  11.     Rectangle hero = new Rectangle(0, 0, 50,50);
  12.     int score = 0;
  13.     long startTime, endTime;
  14.     int seconds;
  15.     int x, y;
  16.     boolean hit = false;
  17.    
  18.     ArrayList<Gobbler> gobs = new ArrayList<Gobbler>();
  19.    
  20.     public void loadImages(){
  21.         packy = new ImageIcon("packy.png").getImage();
  22.     }
  23.    
  24.     public static void main(String[] args){
  25.         new Game();
  26.        
  27.     }
  28.    
  29.     public Game(){
  30.        
  31.         setSize(1280,720);
  32.         setTitle("Lawnmower 3000");
  33.         setDefaultCloseOperation(3);
  34.         setVisible(true);
  35.         setResizable(false);
  36.        
  37.         addKeyListener(new Input());
  38.        
  39.         addGobbler(5000);
  40.        
  41.         hero.x = 50;
  42.         hero.y = 50;
  43.        
  44.         loadImages();
  45.        
  46.         startTime = System.currentTimeMillis();
  47.         animationLoop();
  48.        
  49.     }
  50.    
  51.     public void addGobbler(int amount){
  52.        
  53.         for(int i = 0; i < amount; i++)
  54.             gobs.add(new Gobbler());
  55.        
  56.     }
  57.    
  58.     public void paint(Graphics g){
  59.        
  60.         if(hit){
  61.            
  62.             endTime = System.currentTimeMillis();
  63.             seconds = (int) (endTime - startTime) / 1000;
  64.            
  65.             score += 1337 - seconds;
  66.             startTime = endTime;
  67.             hit = false;
  68.            
  69.         }
  70.            
  71.         Image offScreen = createImage(getWidth(), getHeight());
  72.         draw(offScreen.getGraphics());
  73.        
  74.         g.drawImage(offScreen, 0, 0, null);
  75.        
  76.        
  77.     }
  78.    
  79.     public void draw(Graphics g){
  80.        
  81.         g.setColor(Color.GRAY);
  82.         g.fillRect(0, 0, 1280, 720);
  83.        
  84.         g.setColor(Color.GREEN);
  85.         for(int i = 0; i < gobs.size(); i++){
  86.             int x = gobs.get(i).getX();
  87.             int y = gobs.get(i).getY();
  88.             int width = gobs.get(i).rect.width;
  89.             int height = gobs.get(i).rect.height;
  90.            
  91.             g.fillOval(x,  y,  width,  height);
  92.             gobs.get(i).collision();
  93.            
  94.         }
  95.        
  96.         g.drawImage(packy, hero.x, hero.y, null);
  97.        
  98.         g.setColor(Color.BLACK);
  99.         g.drawString("Score:" + score, 25, 55);
  100.        
  101.         repaint();
  102.        
  103.     }
  104.    
  105.    
  106.    
  107.     private void setX(int x){
  108.        
  109.         this.x = x;
  110.        
  111.     }
  112.    
  113.     private void setY(int y){
  114.        
  115.         this.y = y;
  116.        
  117.     }
  118.    
  119.     public void update(){
  120.        
  121.         hero.x += x;
  122.         hero.y += y;
  123.        
  124.         if(hero.x < 0)
  125.             hero.x = 0;
  126.             else if(hero.x > 1230)
  127.                 hero.x = 1230;
  128.        
  129.         if(hero.y < 30)
  130.             hero.y = 30;
  131.         else if(hero.y > 670)
  132.             hero.y = 670;
  133.        
  134.     }
  135.    
  136.     public void animationLoop(){
  137.        
  138.         try{
  139.             while(true){
  140.                 update();
  141.                 Thread.sleep(15);
  142.             }
  143.         }
  144.         catch(InterruptedException e){
  145.            
  146.         }
  147.        
  148.     }
  149.    
  150.     private class Input implements KeyListener{
  151.        
  152.         @Override
  153.         public void keyPressed(KeyEvent e) {
  154.            
  155.        
  156.             int keyCode = e.getKeyCode();
  157.            
  158.             if(keyCode == e.VK_W){
  159.                
  160.                 setY(-5);
  161.                
  162.             }
  163.            
  164.             if(keyCode == e.VK_S){
  165.                
  166.                 setY(5);
  167.                
  168.             }
  169.            
  170.             if(keyCode == e.VK_A){
  171.                
  172.                 setX(-5);
  173.                
  174.             }
  175.            
  176.             if(keyCode == e.VK_D){
  177.                
  178.                 setX(5);
  179.             }
  180.            
  181.            
  182.         }
  183.        
  184.         @Override
  185.         public void keyReleased(KeyEvent e) {
  186.            
  187.             int keyCode = e.getKeyCode();
  188.            
  189.             if(keyCode == e.VK_W){
  190.                
  191.                 setY(0);
  192.                
  193.             }
  194.            
  195.             if(keyCode == e.VK_S){
  196.                
  197.                 setY(0);
  198.                
  199.             }
  200.            
  201.             if(keyCode == e.VK_A){
  202.                
  203.                 setX(0);
  204.                
  205.             }
  206.            
  207.             if(keyCode == e.VK_D){
  208.                
  209.                 setX(0);
  210.             }
  211.            
  212.         }
  213.        
  214.         @Override
  215.         public void keyTyped(KeyEvent e) {
  216.            
  217.         }
  218.     }
  219.    
  220.     private class Gobbler{
  221.        
  222.         Rectangle rect;
  223.        
  224.         public Gobbler(){
  225.            
  226.             rect = new Rectangle(0, 0, 20, 20);
  227.             moveGobbler();
  228.            
  229.         }
  230.        
  231.         public int getX(){
  232.             return rect.x;
  233.         }
  234.        
  235.         public int getY(){
  236.             return rect.y;
  237.         }
  238.        
  239.         private void moveGobbler(){
  240.            
  241.             rect.x = (int) (Math.random() * 1280);
  242.             rect.y = (int) (Math.random() * 720);
  243.            
  244.             if(rect.y < 30)
  245.                 rect.y = 30;
  246.             else if(rect.y > 700)
  247.                 rect.y = 700;
  248.            
  249.             if(rect.x > 1260)
  250.                 rect.x = 1260;
  251.             else if(rect.x < 7)
  252.                 rect.x = 7;    
  253.         }
  254.        
  255.         public void collision(){
  256.            
  257.             if(rect.intersects(hero)){
  258.                
  259.                 moveGobbler();
  260.                 hit = true;
  261.                
  262.             }
  263.            
  264.         }
  265.        
  266.     }
  267.  
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement