School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,148 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,726 people online right now. Registration is fast and FREE... Join Now!




How to use looping statements

 

How to use looping statements, How to use looping statements, Creating a Puyo Puyo Game do not unders

Riz 01

14 Oct, 2009 - 09:08 PM
Post #1

New D.I.C Head
*

Joined: 27 Aug, 2009
Posts: 24

Hi Guys im currently creating a puyo puyo game in JAVA and im nearly done ive been working on this for about 2 months im still new to java. Anway my question is, is that after my first two puyo images reach the bottom how would i go about having my next images load and then drop and then have that loop. I understand how to create a loop statement but i just don't understand what parameters i would need to put into it and what kind of loop statement i would need i.e. for loop, do loop etc... I really could use some help ive tried and gotten this far but im pretty stuck. I appreciate any help you can provide. Here is my code so far
CODE
public class Puyo {

    public int x, y, color;
    public Puyo(int c, int x2, int y2) {
        color = c;
        x = x2;
        y = y2;
      
  
  
    }
  
  
}


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

public class DemoTest extends JPanel {

    public static void main(String[] args) { {
    JFrame frame = new JFrame("Puyo Puyo");
    frame.setContentPane(new PuyoAnimation());  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setSize(200,453);
    frame.setResizable(false);
    frame.setVisible(true);
  }
}
}


CODE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;  
import javax.swing.event.*;
import java.util.Random;



public class PuyoLogic extends JPanel {

   public static final int puyoEmpty=0;

  final int board_height = 12;
  final int board_width = 6;
  final int image_height= 32;
  final int image_width = 32;
  final int MAX_X = board_width*image_width;
  final int MAX_Y = board_height*image_height;
  private static final int DELTA_Y = 2;
  private static final int TIMER_DELAY = 20;


  Image images[];
  Puyo puyolist[][];
  Puyo current[];
  Puyo droppingBalls[];
  Timer pptimer;
  Random myRand= new Random();
  boolean keyRight,keyLeft,keyUp;

  
     public PuyoLogic() {
     super();
      
        puyolist = new Puyo[board_width][board_height];
      
        current = new Puyo[2];
        current[0] = new Puyo(myRand.nextInt(4), (board_width * image_width /2), 0);
        current[1] = new Puyo(myRand.nextInt(4), (board_width * image_width /2)+image_width, 0);
      
    // current[2] = new Puyo(myRand.nextInt(4), (board_width * image_width /2), 0);
   //     current[3] = new Puyo(myRand.nextInt(4), (board_width * image_width /2)+image_width, 0);
      
    
     images = new Image[4];
     images[0] = Toolkit.getDefaultToolkit().getImage("puyo_yellow.png");
     images[1] = Toolkit.getDefaultToolkit().getImage("puyo_blue.png");
     images[2] = Toolkit.getDefaultToolkit().getImage("puyo_green.png");
     images[3] = Toolkit.getDefaultToolkit().getImage("puyo_red.png");
    
     setFocusable(true);
     PuyoMove puyo_move = new PuyoMove(); // Make a new video game KeyListener
     addKeyListener(puyo_move);
     setBackground(Color.BLACK);
    

      pptimer = new Timer(TIMER_DELAY, new TimerAction());
      pptimer.start();
     }  
        
     public void setAnimation(boolean OnandOff){
        if (OnandOff) {
            pptimer.start();  
        } else {
            pptimer.stop();  
        }
     }
    
    
    public void NewBlock() {
    int newblock;
    int i,j;

    for(i=0; i<4; i++)
        for(j=0; j<4; j++)
        //puyolist[i][j] = new Puyo[2];

    current[0].x=board_width/2-2;
    current[0].y=0;

    }    
    
    
    public void puyoBoundsRight(Puyo[] current){
    if ((current[0].x + image_width <= MAX_X - image_width) && (current[1].x + image_width <= MAX_X - image_width)){
    current[0].x += image_width;
    current[1].x += image_width;
  }
}
    public void puyoBoundsLeft(Puyo[] current){
    if ((current[0].x - image_width >= 0) && (current[1].x - image_width >= 0)){
    current[0].x -= image_width;
    current[1].x -= image_width;
  }
}  



   private class PuyoMove implements KeyListener {
        
     public void keyTyped(KeyEvent k){}
     public void keyReleased(KeyEvent k){}
     public void keyPressed(KeyEvent k){
        
        switch (k.getKeyCode()){
        
         case KeyEvent.VK_LEFT:
             keyLeft = true;
             break;
            
         case KeyEvent.VK_RIGHT:
             keyRight = true;
             break;
       }
     }
   }
  

        public void paintComponent(Graphics g){
        super.paintComponent(g);  
          
        g.drawImage(images[current[0].color],current[0].x,current[0].y,this);
        g.drawImage(images[current[1].color],current[1].x,current[1].y,this);
      
       // g.drawImage(images[current[2].color],current[2].x,current[2].y,this);
        //g.drawImage(images[current[3].color],current[3].x,current[3].y,this);
      
      
       }
    
        class TimerAction implements ActionListener {
      
        public void actionPerformed(ActionEvent e) {
      
          if (keyLeft){
          
          
            puyoBoundsLeft(current);
            keyLeft = false;
          
        }
          else if (keyRight) {  
            
            puyoBoundsRight(current);
            keyRight = false;
          
          }
          if (current[0].y + image_height <= MAX_Y && current[1].y + image_height  <= MAX_Y)
          {
             current[0].y += DELTA_Y;
             current[1].y += DELTA_Y;
          }
        else {
              
               setAnimation(false);
        }
          
           repaint();    
          }
       }      
   }


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


class PuyoAnimation extends JPanel {

   PuyoLogic pl;    
  
  public PuyoAnimation() {
  
       pl = new PuyoLogic();        
       JButton startButton = new JButton("Start");        
       JButton stopButton  = new JButton("Stop");
      
  
       startButton.addActionListener(new Start());
       stopButton.addActionListener(new Stop());
      
    
       JPanel button = new JPanel();
       button.setLayout(new FlowLayout());
        button.add(startButton);
        button.add(stopButton);
      
      
       this.setLayout(new BorderLayout());
       this.add(button, BorderLayout.SOUTH);
        this.add(pl,BorderLayout.CENTER);
    }
  
    class Start implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            pl.setAnimation(true);
        }
    }
  
  
    class Stop implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            pl.setAnimation(false);
        }
    }
}//endclass


This post has been edited by Riz 01: 14 Oct, 2009 - 09:08 PM

User is offlineProfile CardPM
+Quote Post


Aeternalis

RE: How To Use Looping Statements

15 Oct, 2009 - 04:58 AM
Post #2

D.I.C Regular
***

Joined: 13 Jul, 2009
Posts: 273



Thanked: 25 times
My Contributions
While this is technically a game.. the question is really about java syntax. Might have better luck in the java forum.
Don't know any Java.. sorry.

Good luck!
Aet

This post has been edited by Aeternalis: 15 Oct, 2009 - 05:02 AM
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: How To Use Looping Statements

15 Oct, 2009 - 06:18 PM
Post #3

lives.ToCode();
Group Icon

Joined: 18 Oct, 2008
Posts: 3,066



Thanked: 171 times
Dream Kudos: 775
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
Technically it looks like you are doing a game programming no-no in a way. From the looks of it you are using a timer control to handle the game. While this might seem like a good idea it usually isn't in practice. Games usually preform in a game loop. This is how a simple game loop works:

CODE

while (true)
{
    UpdateGameObjects();
    RenderGameObjects();
    HandleEvents();
    PreformTiming();
}


Each pass through the loop is considered one frame of the game. In each from of the game you update all of the objects in the game, render the objects of the game, handle system events and preform timing. In the preform timing part is where you would add in the timing to control the execution speed of the game so that it will run at the same speed on all systems.

I have not yet explored GUI applications in Java so I'm not sure how you would implement this.
User is offlineProfile CardPM
+Quote Post

Riz 01

RE: How To Use Looping Statements

16 Oct, 2009 - 09:09 AM
Post #4

New D.I.C Head
*

Joined: 27 Aug, 2009
Posts: 24

QUOTE(SixOfEleven @ 15 Oct, 2009 - 06:18 PM) *

Technically it looks like you are doing a game programming no-no in a way. From the looks of it you are using a timer control to handle the game. While this might seem like a good idea it usually isn't in practice. Games usually preform in a game loop. This is how a simple game loop works:

CODE

while (true)
{
    UpdateGameObjects();
    RenderGameObjects();
    HandleEvents();
    PreformTiming();
}


Each pass through the loop is considered one frame of the game. In each from of the game you update all of the objects in the game, render the objects of the game, handle system events and preform timing. In the preform timing part is where you would add in the timing to control the execution speed of the game so that it will run at the same speed on all systems.

I have not yet explored GUI applications in Java so I'm not sure how you would implement this.


Ah alright then i guess my question should really be would any one be able to help optimize my code in a way that it makes sense for a game?

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 03:57PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month