Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Donal Byrne

Member Since 24 May 2012
Offline Last Active Jul 08 2014 02:39 PM

Posts I've Made

In Topic: problem with java buffered image

11 June 2014 - 03:58 PM

ok so I got the image on the screen and did key inputs, but instead of moving cleanly it leaves behind a trail of the image. 

Its like its just cloning itself instead of moving.

 

It has to be something to do with the loop right?

package game3;

import java.awt.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.*;
import java.io.IOException;
import java.util.logging.*;
import java.util.logging.*;
import javax.swing.*;

/**,
 *
 * @author djb
 */
public class Game extends Canvas implements Runnable{
    
    //Player p;
    
    private boolean running = false;
    private Thread thread;
    
    BufferedImage player = null;
    
    int speed = 2;
    
    private Player p;
    
    public void init() 
    {
        
        BufferedImageLoader loader = new BufferedImageLoader();
        try{
            player = loader.loadImage("/images/p1Up.png");
        }catch(IOException e)
        {
            e.printStackTrace();
        }
        
        addKeyListener(new KeyInput(this));
        this.requestFocus();
        p = new Player(200,200,this);
    }
    
    private synchronized void start()
    {
        if(running)
            return;
        
        running = true;
        thread = new Thread(this);
        thread.start();
    }
    
    private synchronized void stop()
    {
        if(!running)
            return;
        
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.exit(1);
    }
    
    
    
    public void run() 
    { //game loop
        init();
        long lastTime = System.nanoTime();
        final double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        int updates = 0;
        int frames = 0;
        long timer = System.currentTimeMillis();
        
        while(running)
        {
            //game loop
          long now = System.nanoTime();
          delta+=(now - lastTime)/ns;
          lastTime = now;
          if(delta >= 1)
          {
              tick();
              updates++;
              delta--;
          }
          
          render();
          frames++;
         
          
          if(System.currentTimeMillis() - timer > 1000)
          {
              timer  += 1000;
              System.out.println(updates + "ticks, Fps " + frames);
              updates = 0;
              frames = 0;
              
          } 
          
        }
        stop();
        
    }
    
    private void tick()
    {
        p.tick();
        
    }
    
    private void render()
    {
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null)
        {
            createBufferStrategy(1);
            return;
        }
        
        Graphics g = bs.getDrawGraphics();
        //put objects between here and dispose to draw stuff
        
        
        
        //g.drawImage(player, 100, 100,32 ,32, this);
        
        p.render(g);
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        
        g.dispose();
        bs.show();      
        
    }
    
     public void keyPressed(KeyEvent e)
     {
         int key = e.getKeyCode();
         
         if(key==KeyEvent.VK_RIGHT){
             
             p.setX(p.getX() + 5*speed);
             
         }else if(key==KeyEvent.VK_LEFT){
             
             p.setX(p.getX() - 5*speed);
             
             
         }else if(key==KeyEvent.VK_UP){
             
             p.setY(p.getY() - 5*speed);
             
         }
         else if(key==KeyEvent.VK_DOWN){
             
            p.setY(p.getY() + 5*speed);
             
         }
     }
      public void keyReleased(KeyEvent e)
      {
          
      }
   
    
    public static void main(String[]args)
    {
        
        Game game = new Game();
        
        JFrame frame = new JFrame();
        frame.pack();
        frame.setSize(640,480);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(game);
        
        game.setFocusable(true);
        
        game.start();
        
        
    }

    

    
    
}


In Topic: problem with java buffered image

11 June 2014 - 01:52 PM

Yeah that sorted it out. Im such a idiot!

 

Yeah that makes sense, sorry about that. won't happen again.

 

Thanks a lot 


In Topic: Java class problem

03 June 2014 - 04:20 PM

what happens is that it shows a blank screen. No compile errors just a blank screen. Its supposed to show a race track with two squares(cars) moving around it using wsad or ijkl as directional controls. Its also supposed to have collision detection.

 

Like i said the screen goes blank when i add the Move classes


In Topic: Want to program for big developer. what should i be learning?

24 October 2012 - 07:34 AM

cool thanks :) also, when I graduate this year I'm planning to get my bachelors degree in computer science and then go on to do a masters. So by then I should have the programming knowledge. I just wana start designing/developing games now so Ill have some experience when i start job hunting

In Topic: Need help with java racing game

29 May 2012 - 01:12 PM

well its a lot better then it was, i can actually see the track now. only thing is that it only shows player1 (the blue square)

PARTNERS