Results 1 to 5 of 5

Thread: How to do this:

  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default How to do this:

    I made a little game in Swing, that when you press space it slowly lifts you 10 pixels per second, and then lowers you down. However, it lets people just press space repeatedly to stay at the top, therefore cheating. How do I stop that? Also, how would I make the ball rotate as it moves forward? I cannot just rotate with g2d.rotate() because if it rotates, say 90 degrees when you press space it will jump in THAT direction and not just up as it always should. Will provide code if necessary

    TL;DR how do I make the ball rotate repeatedly but always go up instead of in the direction it is facing?

    Thanks!
    Last edited by MW130; 02-06-2014 at 03:09 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,526
    Rep Power
    5

    Default Re: How to do this:

    Moving an object around and rotating an object are not related. You can move a rotating object in any direction. You can rotate an object a specific amount for each press of a key. You can rotate and move an object for each press of a key. So I am not certain what problems you are having. Have you successfully rotated an object in place yet? That is the first thing you should try. Then you can modify its location and keep it rotating. That is the next thing you should try.

    How to keep players from cheating, as you describe it is up to you. You can limit their moves per unit time. You can exponentially decrease their moving increment per unit time. You can weight the free fall increment more than the rate of increase. It's up to you.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: How to do this:

    Quote Originally Posted by jim829 View Post
    Moving an object around and rotating an object are not related. You can move a rotating object in any direction. You can rotate an object a specific amount for each press of a key. You can rotate and move an object for each press of a key. So I am not certain what problems you are having. Have you successfully rotated an object in place yet? That is the first thing you should try. Then you can modify its location and keep it rotating. That is the next thing you should try.

    How to keep players from cheating, as you describe it is up to you. You can limit their moves per unit time. You can exponentially decrease their moving increment per unit time. You can weight the free fall increment more than the rate of increase. It's up to you.

    Regards,
    Jim
    Hi Jim, I have gotten it to rotate in place. It is rotating every second. It is kind of uneven because I can't figure out the exact point of rotation, but that doesn't really matter. So yes I do have it rotating in place with g2d.rotate(x) x increments every second.

    To your second point: how do I limit the moves per unit frame?


    This is the code for the rotation::

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    public class Ball extends JComponent {
    	int ballX=60;
    	int ballY=645;
    	int ballDiameter=80;
    	int ballXspeed=10;
    	int ballYspeed=5;
    	int ballYdown=1;
    	int ballYup=1;
    	int ballY2speed=150;
    	int spacePress=0;
    	int blockX=400;
    	int blockY=680;
    	int blockXspeed=1;
    	int score=0;
    	int GameOverX=300;
    	int GameOverY=-50;
    	public int time=0;
    	public boolean spaceBar=false;
    	private BufferedImage smiley;
    	private BufferedImage sadface;
    	int rotateIt=0;
        int difference;
    	
    	public Ball() {
    		try{
    		smiley = ImageIO.read(new File(
    				"/Users/User/Downloads/smiley.png"));
    		sadface = ImageIO.read(new File(
    				"/Users/User/Downloads/sadface.png"));
    		}catch(Exception e){System.out.println("Could not find images");}
    	
    	}
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		Graphics2D g2d = (Graphics2D)g;
    		Rectangle rectangle = new Rectangle(50,50,50,50);
    		g.drawString(Integer.toString(time),50,50);
    		g.drawString("Score : "+Integer.toString(score),500,20);
    		g.setColor(Color.red);
    		g.fillRect(blockX,blockY,100,100);
    		g2d.fill(rectangle);
    		g.drawString("GAME OVER",GameOverX,GameOverY);
    		g2d.rotate(Math.toRadians(rotateIt),100,685);
    		g.drawImage(smiley,ballX, ballY, null );
    
    		repaint();
    	}
    	
    	
    		
    	public void rotate1(){
    		rotateIt++;
    	}
    	KeyListener k = new KeyListener(){
    		@Override
    		public void keyTyped(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
             int k = e.getKeyCode();
             if(k==KeyEvent.VK_SPACE){
            	 spaceBar=true;
            	 repaint();
             }
             if(k!=KeyEvent.VK_SPACE){
            	 spaceBar=false;
             }
             
    		}
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    		}
    
    	};
    	public void run(){
    
    while(ballY<645){
    	ballX=60;
    }
    		if(spaceBar==true){
            	ballYup=1;
    		    ballY-=ballYup;
    	    }
    		if(spaceBar==false){
    			ballYdown=1;
    			ballY+=ballYdown;
    
    		}
    		if(ballY<=480){
    			spaceBar=false;
    			ballYdown=1;
    			ballYup=0;
    			ballY+=ballYdown;
    
    		}
    		if(ballY>=645){
    			ballYdown=0;
    			ballYup=0;
    			ballY=645;
    		}
    		
    		if(blockX>0){
    			blockX-=blockXspeed;
    		}
    		if(blockX<=0){
    			blockX=850;
    			score++;
    		}
    		if(ballX+80>=blockX&&ballY+80>=blockY)
    		{
    			smiley=sadface;
    			blockXspeed=0;
    			ballYdown=0;
    			ballYup=0;
    			ballY=645;
    			GameOverY+=5;
    			if(GameOverY>350&&GameOverY<400){
    				GameOverY=375;
    			}
    		}
    		
    		
    	}
    }
    the run method is called every second

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,526
    Rep Power
    5

    Default Re: How to do this:

    I am not certain how you are locating the center of rotation. Usually I want the center of rotation to be the center of the object. So if I have a rectangle of size (w,h) and I want to draw it at (x, y) I do the following:

    Java Code:
    g2d.rotate(angle, x + w/2, y + h/2);
    g2d.drawRect(x, y, w, h);
    I'm done for the evening. If you attach the images I will check it out tomorrow.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: How to do this:

    Just set some sort of requirements that need to be met before they jump.
    With the right know how, anything is possible

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •