Jump to content

  • Log In with Google      Sign In   
  • Create Account


Donal Byrne

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

Topics I've Started

problem with java buffered image

11 June 2014 - 12:57 PM

I am trying to increase performance with buffered images. I have made a game loop that has increased my fps but i also want buffered images, however these aren't working.

 

its supposed to show a black background and print the amount of ticks and fps in the console

package game3;

import java.awt.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.image.*;
import java.util.logging.*;
import java.util.logging.*;
import javax.swing.*;

/**,
 *
 * @author djb
 */
public class Game extends Canvas implements Runnable{
    
    private boolean running = false;
    private Thread thread;
    
    private BufferedImage image = new BufferedImage(640,480,BufferedImage.TYPE_INT_RGB);
    
    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
        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()
    {
        
    }
    
    private void render()
    {
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null)
        {
            createBufferStrategy(3);
            return;
        }
        
        Graphics g = bs.getDrawGraphics();
        //put objects between here and dispose to draw stuff
        
        g.drawImage(image, 0, 0, 630, 480, this);
        
       g.dispose();
        bs.show();
        
        
        
    }
    
    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);
        
        game.start();
        
        
    }

    
    
}

could really use some help, thanks


Java class problem

03 June 2014 - 02:54 PM

Everything was fine until i added in the Move classes.No idea what to do, any help would be appreciated


package g1p1;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;


public class G1P1 extends JFrame {
        
    //screen size
    final int WIDTH = 900, HEIGHT = 650;
    
    //keep track of speed
    double p1Speed = 0.5, p2Speed = 0.5;
   
    //direction var
    final int UP=0, DOWN = 1, RIGHT=2, LEFT=3;
    
    //PLAYERS DIRECTION
    int p1Direction = UP;
    int p2Direction = UP;
    
    //gives perameters of objects
    //border
      Rectangle left = new Rectangle(0,0,50,HEIGHT);  
      Rectangle right = new Rectangle(850,0,50,HEIGHT);
      Rectangle top = new Rectangle(0,0,WIDTH, HEIGHT/9);   
      Rectangle bottom = new Rectangle(0,600,WIDTH,100);
      Rectangle center = new Rectangle(200,(int)((HEIGHT/9)*2.5),575,(HEIGHT/9)*4);
     
      
      //OBSTACLES
      Rectangle obstacle = new
            Rectangle(250,0,100,140);
      Rectangle obstacle2 = new
           Rectangle(300,400,WIDTH/10,150);
      Rectangle obstacle3 = new
            Rectangle(2*(WIDTH/3),(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);
     Rectangle obstacle4 = new
            Rectangle(450,525,100,100);
      Rectangle obstacle5 = new
            Rectangle(WIDTH/2,125,WIDTH/30,HEIGHT/4);
      
      //FINISH LINE
      Rectangle finish = new Rectangle(50,(HEIGHT/2)-HEIGHT/9,(int)((WIDTH/9)*1.5), HEIGHT/70);
      
      
      //p1 car
      Rectangle p1 = new Rectangle(70,330,WIDTH/30,WIDTH/30);
      
      //p2 car
      Rectangle p2 = new Rectangle(150,395,WIDTH/30,WIDTH/30);
      
      
      //STARTING LINE FOR OUTER CAR
      Rectangle lineO = new
            Rectangle(50,HEIGHT/2,(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
      
      //STARTING LINE FOR INNER PLAYER
      Rectangle lineI = new Rectangle(125,(HEIGHT/2)+(HEIGHT/10),(int)((WIDTH/9)*1.5)/2, HEIGHT/140);
      
      //THE CONSTRUCTOR - makes the screen
      public G1P1()
      {
          //creates JFrame
          super("radical racing");
          setSize(WIDTH,HEIGHT);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setVisible(true);
          
        /*  Move1 m1 = new Move1();
          Move2 m2 = new Move2();
          m1.start();
          m2.start();*/
      }
      
      //THIS WILL DRAW objects of the game
      public void paint(Graphics g)
      {
          super.paint(g);
          
          //draw the background
          g.setColor(Color.DARK_GRAY);
          g.fillRect(0,0,WIDTH,HEIGHT);
          
          //make the border green
          g.setColor(Color.GREEN);
          
          //USING THE RECTANGLES TO DRAW IT
          g.fillRect(left.x,left.y,left.width,left.height);
          g.fillRect(right.x,right.y,right.width,right.height);
          g.fillRect(top.x,top.y,top.width,top.height);
          g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
          g.fillRect(center.x,center.y,center.width,center.height);
          
          g.setColor(Color.GREEN);
          g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
          g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
          g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
          g.fillRect(obstacle4.x,obstacle4.y,obstacle4.width,obstacle4.height);
          g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);
          
          //set starting line to white
          g.setColor(Color.WHITE);
          
          //DRAW THE STARTING LINE
          g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height);
          g.fillRect(lineI.x,lineI.y,lineI.width,lineI.height);
          
          //SET COLOR OF FINISH LINE TO YELLOW
          g.setColor(Color.YELLOW);
          //DRAW FINISH LINE
          g.fillRect(finish.x,finish.y,finish.width,finish.height);
          
          //SET COLOR OF P1 TO BLUE
          g.setColor(Color.blue);
          g.fill3DRect(p1.x, p1.y, p1.width, p1.height,true);
          
          //SET COLOR OF P2 TO red
          g.setColor(Color.red);
          g.fill3DRect(p2.x, p2.y, p2.width, p2.height,true);
          
      }
      
      //add speed + movement
      private class Move1 extends Thread implements KeyListener
      {
          @Override
          public void run()
          {
              //wake up key listener
              addKeyListener(this);
              
              while(true)
              {
                  try
                  {
                      //refresh screen
                      repaint();
                      
                      //check for collision on walls, if so, make it slower
                      if(p1.intersects(left)||p1.intersects(right)||
                         p1.intersects(top) ||p1.intersects(bottom)||
                         p1.intersects(obstacle)||p1.intersects(obstacle2)
                         ||p1.intersects(obstacle3)||p1.intersects(obstacle4)
                         ||p1.intersects(obstacle5)|| p1.intersects(p2))
                      {
                          p1Speed =-4;
                      }
                      
                      //if it hits the center do the same 
                      if(p1.intersects(center))
                      {
                          p1Speed =-2.5;
                      }
                      
                      //increase speed
                      if(p1Speed<=5)
                      {
                        p1Speed+=0.2;
                      }
                      
                      //move player based on direction
                      if(p1Direction==UP)
                      {
                          p1.y-=(int)p1Speed;
                      }
                       if(p1Direction==DOWN)
                      {
                          p1.y+=(int)p1Speed;
                      }
                       if(p1Direction==LEFT)
                      {
                          p1.x-=(int)p1Speed;
                      }
                        if(p1Direction==LEFT)
                      {
                          p1.x+=(int)p1Speed;
                      }
                          
                       //delay refresh
                       Thread.sleep(75);
                      
                                        
                  }
                  
                  catch(Exception e)
                        {
                           break;   
                        }
              }
          }
          
            //IMPLEMENT METHOD FROM KEY LISTENER
          @Override
      public void keyPressed(KeyEvent event)
      {
          
      }
      
      //IMPLEMENT METHOD FROM KEY LISTENER
          @Override
      public void keyReleased(KeyEvent event)
      {
          
      }
      
      //IMPLEMENT METHOD FROM KEY LISTENER
          @Override
      public void keyTyped(KeyEvent event)
      {
         if(event.getKeyChar()=='a') 
         {
             p1Direction = LEFT;
         }
         if(event.getKeyChar()=='d') 
         {
             p1Direction = RIGHT;
         }
         if(event.getKeyChar()=='s') 
         {
             p1Direction = DOWN;
         }
         if(event.getKeyChar()=='w') 
         {
             p1Direction = UP;
         }
      }
   }
      
       //add speed + movement
      private class Move2 extends Thread implements KeyListener
      {
          @Override
          public void run()
          {
              //wake up key listener
              addKeyListener(this);
              
              while(true)
              {
                  try
                  {
                      //refresh screen
                      repaint();
                      
                      //check for collision on walls, if so, make it slower
                      if(p2.intersects(left)||p2.intersects(right)||
                         p2.intersects(top) ||p2.intersects(bottom)||
                         p2.intersects(obstacle)||p2.intersects(obstacle2)
                         ||p2.intersects(obstacle3)||p2.intersects(obstacle4)
                         ||p2.intersects(obstacle5)|| p2.intersects(p1))
                      {
                          p2Speed =-4;
                      }
                      
                      //if it hits the center do the same 
                      if(p2.intersects(center))
                      {
                          p2Speed =-2.5;
                      }
                      
                      //increase speed
                      if(p2Speed<=5)
                      {
                        p2Speed+=0.2;
                      }
                      
                      //move player based on direction
                      if(p2Direction==UP)
                      {
                          p2.y-=(int)p1Speed;
                      }
                       if(p2Direction==DOWN)
                      {
                          p2.y+=(int)p1Speed;
                      }
                       if(p1Direction==LEFT)
                      {
                          p1.x-=(int)p1Speed;
                      }
                        if(p2Direction==LEFT)
                      {
                          p2.x+=(int)p1Speed;
                      }
                          
                       //delay refresh
                       Thread.sleep(75);
                      
                                        
                  }
                  
                  catch(Exception e)
                        {
                           break;   
                        }
              }
          }
          
                //IMPLEMENT METHOD FROM KEY LISTENER
          @Override
      public void keyPressed(KeyEvent event)
      {
          
      }
      
      //IMPLEMENT METHOD FROM KEY LISTENER
          @Override
      public void keyReleased(KeyEvent event)
      {
          
      }
      
      //IMPLEMENT METHOD FROM KEY LISTENER
          @Override
      public void keyTyped(KeyEvent event)
      {
         if(event.getKeyChar()=='j') 
         {
             p2Direction = LEFT;
         }
         if(event.getKeyChar()=='l') 
         {
             p2Direction = RIGHT;
         }
         if(event.getKeyChar()=='k') 
         {
             p2Direction = DOWN;
         }
         if(event.getKeyChar()=='i') 
         {
             p2Direction = UP;
         }
      }
   }
      
   
      
      
      public static void main(String[]args)
      {
          new G1P1();
      }
    }
    







    


How to make a simple unity2d inventory system

22 May 2014 - 11:29 AM

Trying to make a VERY simple inventory for unity2D. I would like to be able to pick up objects, drop them and have them fill up an inventory screen.

 

Can be made in either JS or C#

 

Thanks


how to animate enemies in cocos2d

04 July 2013 - 06:28 AM

ok so i just finished Ray Wenderlichs tutorial on how to make a simple game for iPhone. I have started to expand on it by adding in new graphics,sounds etc. I have been trying to add animation to the enemies but it hasn’t been working out

if you aren’t familiar with the tutorial you can find the finished project at the bottom of this page.

http://www.raywenderlich.com/25806/harder-monsters-and-more-levels-how-to-make-a-simple-iphone-game-with-cocos2d-2-x-part-3

I’ve already made my animation sprites, put them through texture packer and all that but any animation methods i try to integrate into the project doesn’t work.

any help would be great.

thanks


How should I prepare for a programming career ?

24 October 2012 - 09:19 AM

Ok so im in my last year of high school. Im planning to get a degree in computer science and then hopefully get a masters after that. By then I should be a very capable programmer (or at least thats what I'm assuming)
I want to start making some games and possibly publishing them on websites like kongregate/miniclip/newgrounds but not sure what way to go about it. Ive made a handful of simple games with Unity3d. I picked that because I thought it would be a better choice in the long run but now I'm not so sure. Ive also learned javaScript and C#.

If I just want to make some fun games to add to my portfolio should I stick with Unity or would it be better to use something like Flash, because it seems like a lot of successful indie games are made with it and the games I want to make right now are more 2d games then 3d. Im just wondering if making complete 3d games would be better experience and look better on a portfolio. Also I'm doing all these projects solo which makes me think flash might be easier.

Or is there a better engine to use? Ive heard good things about XNA and UDK.

As you can see I'm very conflicted and not sure what to do. My end goal is to make a couple of simple, fun indie games to show that i have experience so I can eventually get a good programing job at a AAA company like Ubisoft or Bethesda etc

Any advice from experience members of the gaming industry would be really helpful.

Thanks a lot

PARTNERS