Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Mario Type Game Code, Gravity Does not work, error is in "jumping" method

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Mario Type Game Code, Gravity Does not work, error is in "jumping" method

    //imports following packages from java
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Image.*;
    //extends the JFrame and allows the program to listen to keyboard input
    public class RectangleandMovement extends JFrame implements KeyListener{
    //creates an array of rectangles for the character
    //jump counter
    int jc=10;
    Rectangle imageAr[];
    //rectangles for bounds of player and fireball
    Rectangle playerHitbox,fireballHitbox;
    //Rectangle rect1;
    //creates a true/false value to check if the rectangle is being intersected
    boolean intersectChecker = false;
    //boolean variables for keyboard input
    boolean up,left,right,shoot,shotTaken = false;
    //creates an array for the running images
    Image runPics[],shootPics[];
    //creates images relating to the character
    Image background1,background2,idle,run,jump,jump1,fireba ll;
    //private variables so they are only visible in this class
    private Image dbOffScreen;
    private Graphics dbGraphics;
    //sets counter as an int,used to switch between the different images
    int counter,counter2;
    //creakes xposition for background
    int backgroundXpos;
    //sets and initiallizes player x and y positions
    int playerXpos, playerYpos;
    int fireballXpos, fireballYpos;
    //ENEMY VARIABLES//
    //creates images for the enemy
    Image skeleton[],skeletonIdle;
    //creates variables for x and y position of the skeleton
    int skeletonXpos, skeletonYpos;
    //creates a rectangle around the skeleton
    Rectangle skeletonHitbox;
    //constructor
    public RectangleandMovement(){
    super("");
    //sets the title
    setTitle("Mage Runner");
    //once again sets the value of the x and y coordinates
    playerXpos = 50;
    playerYpos = 250;

    //creates 8 rectangles for the run animation
    imageAr = new Rectangle [8];
    //creats 8 images for run animation
    runPics = new Image [8];
    shootPics = new Image [8];
    //sets the background to an image
    background1 = new ImageIcon("mrss.png").getImage();
    background2 = new ImageIcon("mrss1.png").getImage();
    //sets the image for the idle picture
    idle = new ImageIcon("idle1.png").getImage();
    //this is the initial picture, where run is equal to the idle picture
    run = idle;
    fireball = new ImageIcon("fireball1.png").getImage();
    playerHitbox = new Rectangle();
    playerHitbox.setSize(75,100);
    //creates a loop for the run animation so the array can hold the 8 pictures
    for (int i=0; i < imageAr.length;i++){
    // int h,w;
    // h = runPics[i].getHeight();
    // w = runPics[i].getWidth();
    imageAr[i] = new Rectangle(50,250,80,100);
    runPics[i] = new ImageIcon("run"+i+".png").getImage();
    shootPics[i] = new ImageIcon("attack"+i+"png").getImage();
    }
    //sizes the array of the skeleton image
    skeleton = new Image [6];
    //creates loop to add skeleton images
    for (int i = 0; i < skeleton.length; i++){
    skeleton[i] = new ImageIcon("skeleRun" + i + ".png").getImage();
    }
    skeletonIdle = new ImageIcon("skeleIdle1.png").getImage();
    skeletonXpos = 300;
    skeletonYpos = 250;
    skeletonHitbox = new Rectangle();
    skeletonHitbox.setSize(58,100);
    //checks for intersection (will be implemented later)
    intersectChecker = false;
    //adds key listener
    addKeyListener(this);
    //sets screen size
    setSize(700,400);

    //sets visible to true
    setVisible(true);
    }



    //double buffered to reduce lag
    public void paint(Graphics g){
    dbOffScreen = createImage(700,400);
    dbGraphics = dbOffScreen.getGraphics();
    paintComponent(dbGraphics);
    g.drawImage(dbOffScreen,0,0,this);
    }



    //this method paints the images on the jframe
    public void paintComponent(Graphics g){
    super.paint(g);
    //calls graphics 2d
    Graphics2D g2D = (Graphics2D)g;
    //rectangle for character
    playerHitbox.setLocation(playerXpos,playerYpos);
    skeletonHitbox.setLocation(skeletonXpos,skeletonYp os);

    //draws the background and moves it backwords as the player moves forward
    backgroundXpos = 50-playerXpos;
    g2D.drawImage(background1,backgroundXpos,0,null);
    g2D.drawImage(background2,600-playerXpos,0,null);
    //draws characters
    g2D.drawImage(run,playerXpos,playerYpos,null);
    if (shotTaken){
    g2D.drawImage(fireball, fireballXpos, fireballYpos, null);
    }
    g2D.drawImage(skeletonIdle, skeletonXpos, skeletonYpos, null);
    if (intersectChecker == true){
    System.out.println("BOOM");
    }
    //repaints
    repaint();
    }


    public boolean checkCollision(){
    if(playerHitbox.intersects(skeletonHitbox)){
    intersectChecker = true;
    }
    return intersectChecker;
    }



    public static void main(String[] args) {
    new Main();
    }



    //this method recieves boolean variables and then changes the x and y coordinates accordingly and is supposed to create animations based on user input
    public void run(){
    //if user presses right arrow the following code executes
    if (right == true){
    for (int i = 0; i<8; i++){
    run = new ImageIcon("run" + counter + ".png").getImage();
    if (counter2 == 50){
    counter++;
    counter2 = 0;
    }
    counter2++;
    if (counter == 8){
    counter = 1;
    }
    }
    }
    if (left == true){
    for (int i = 0; i<8; i++){
    run = new ImageIcon("Lrun" + counter + ".png").getImage();
    if (counter2 == 50){
    counter++;
    counter2 = 0;
    }
    counter2++;
    if (counter == 8){
    counter = 1;
    }
    }
    }
    if (playerXpos<20){
    playerXpos = playerXpos + 5;
    }
    if(playerXpos>650){
    playerXpos = playerXpos -5;
    }
    if (left == true){
    playerXpos = playerXpos - 2;
    }
    if (right == true){
    playerXpos = playerXpos + 2;
    }
    if (up == true){

    }
    /*
    boolean posY = false;
    if (right = true){
    jump = new ImageIcon("jump1.png").getImage();
    run = jump;
    ///when up is true//
    for (int i=0;i<16;i++){
    playerYpos -= (int)(5+ (-0.3 *i));
    if (playerYpos > 250){
    posY = true;
    up=false;
    }
    }

    }

    if (posY == true){
    while ((playerYpos != 250) && (up =! true)){
    playerYpos +=1;
    }
    }
    */
    // if (left = true){
    // jump1 = new ImageIcon("jump2.png").getImage();
    // run = jump1;
    // }
    repaint();

    }
    //if (down == true){
    // playerYpos = playerYpos + 10;
    //}




    public void jumping(){
    boolean posY = false;
    if (up = true){
    jump = new ImageIcon("jump1.png").getImage();
    run = jump;
    ///when up is true//

    if (jc>-10){
    for (int i=0;i<20;i++){
    playerYpos += (int)((jc *Math.abs(jc))*-0.5);
    jc= jc - 1;//(int)(-5);//+ (0.3 *i));
    repaint();
    try{
    Thread.sleep(10);
    }
    catch(InterruptedException ex){
    Thread.currentThread().interrupt();
    }
    }
    if (playerYpos > 350){
    posY = true;
    up=false;
    }

    if (posY == true){
    while ((playerYpos != 350) && (up =! true)){
    playerYpos -=1;
    }
    }
    //repaint();
    }
    }
    }




    public void shootFire(){
    if (shoot == true){

    for (int i = 0; i<400; i++){
    run = new ImageIcon("attack" + counter + ".png").getImage();
    if (counter2 == 50){
    counter++;
    counter2 = 0;
    }
    counter2++;
    if (counter == 8){
    counter = 1;
    }
    }
    }
    if (shotTaken){
    for (int i = 0; i < 50; i++){
    fireballXpos=fireballXpos + 1 ;
    repaint();
    }
    }
    repaint();
    }



    public void keyPressed(KeyEvent ke){
    if (ke.getKeyCode()==KeyEvent.VK_LEFT){
    left = true;
    }
    if (ke.getKeyCode()==KeyEvent.VK_UP){
    up = true;
    jumping();
    }
    if (ke.getKeyCode()==KeyEvent.VK_RIGHT){
    right = true;
    }
    if (ke.getKeyCode()==KeyEvent.VK_SHIFT){
    if (fireballHitbox == null){
    shoot = true;
    if (shoot){
    fireballXpos = (playerXpos + 75);
    fireballYpos = (playerYpos + 20);
    fireballHitbox = new Rectangle(fireballXpos, fireballYpos, 48,27);
    shotTaken = true;
    }
    }
    shootFire();
    if(checkCollision()){
    intersectChecker = true;
    }
    }

    run();
    }



    public void keyReleased(KeyEvent ke){
    if (ke.getKeyCode()==KeyEvent.VK_LEFT){
    left = false;
    run = idle;
    }
    if (ke.getKeyCode()==KeyEvent.VK_UP){
    up = false;

    }
    if (ke.getKeyCode()==KeyEvent.VK_RIGHT){
    right = false;
    run = idle;
    }
    if (ke.getKeyCode()==KeyEvent.VK_SHIFT){
    shoot = false;
    if (fireballXpos >= 60){
    fireballHitbox = new Rectangle(0,0,0,0);
    shotTaken = false;
    shoot = true;
    repaint();
    }
    }
    }



    public void keyTyped(KeyEvent ke){
    }

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Mario Type Game Code, Gravity Does not work, error is in "jumping" method

    Please explain what the code does when executed. What does "not work" mean?

    The declaration for the Main class is missing.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  2. Replies: 1
    Last Post: July 16th, 2014, 04:16 AM
  3. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  4. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  5. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM