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 3 of 3

Thread: StdDraw keyboard controls uncontrollable

  1. #1
    Junior Member
    Join Date
    Jun 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation StdDraw keyboard controls uncontrollable

    - controlling two characters using WASD and IJKL
    - using StdDraw hasNextKeyTyped to check keyTyped against WASD/IJKL ascii codes
    - however characters move too quickly, cannot stop and do not respond to keyboard controls/typed keys after a while
    - any feedback/guidance/advice will be greatly appreciated, TIA

    /**
     * Description of PvH
     * May 25, 2017
     */
    import java.util.Scanner;
    import java.awt.Font;
    import java.awt.GraphicsEnvironment;
    import java.util.ArrayList;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Game {
        public void newGame(String command) {
            boolean diagon = false;
            double hx = 100.0, hy = 150.0, px = 15.0, py = 150.0;
            if (command.equals("play")) {
                // set canvas boundaries and graphics
                StdDraw.setXscale(0, 300); // set x-axis
                StdDraw.setYscale(0, 300); // set y-axis
                //double penrad = 0.005;
                boolean isPlaying = true;
                int hs = 0; // hs = hades' score, 
                int ps = 0; // ps = persephone's score
                // Figure h = new Figure("h"); //initialize characters
                // Figure p = new Figure("p");
                double d = 0.3;
     
                // initialize Flower objects
                int numOfFlowers = 100;
                // ArrayList<Double[][]> flowers = new ArrayList<Double[][]>(numOfFlowers);
                double[][] flowers = new double[numOfFlowers][2];
                double flwidth = 3.0;
                for (int i = 0; i < numOfFlowers; i++) 
                {
                    Flower f = new Flower();
                    f.setxy();
                    flowers[i][0] = f.getx(f);
                    flowers[i][1] = f.gety(f);
                }
                boolean[] keyStates = new boolean[8]; // first four for persephone, next four for hades
                while (isPlaying) // game loop
                {
                    if (diagon) System.out.println("isPlaying");
                    // if ((h.getx(h,"r") <= p.getx(p,"l")) | (p.getx(p,"r") <= h.getx(h,"l")) |  (p.gety(p,"b") <= (h.gety(h,"t"))) | (h.gety(h,"b") <= (p.gety(p,"t")))) isPlaying = false;
     
                    if (StdDraw.hasNextKeyTyped()) {  // allow user to hold and move at the same time; now you have to press repeatedly
                        int keyTyped = StdDraw.nextKeyTyped();
                        // PERSEPHONE CONTROL = WASD, HADES CONTROL = IJKL
     
                        // keyStates indices: 0 = pu, 1 = pd, 2 = pl, 3 = pr
                        //                    4 = hu, 5 = hd, 6 = hl, 7 = hr
                                        // fix directions and hitting wall, maKE IT STOP
     
                        if (keyTyped == 119) keyStates[0] = true; // p moves up
                        else                 keyStates[0] = false;
                        if (keyTyped == 115) keyStates[1] = true; // p moves down
                        else                 keyStates[1] = false;
                        if (keyTyped == 97)  keyStates[2] = true; // p moves left
                        else                 keyStates[2] = false;
                        if (keyTyped == 100) keyStates[3] = true; // p moves right
                        else                 keyStates[3] = false;
                        if (keyTyped == 105) keyStates[4] = true; // h moves up
                        else                 keyStates[4] = false;
                        if (keyTyped == 107) keyStates[5] = true; // h moves down
                        else                 keyStates[5] = false;
                        if (keyTyped == 106) keyStates[6] = true; // h moves left
                        else                 keyStates[6] = false;
                        if (keyTyped == 108) keyStates[7] = true; // h moves right    
                        else                 keyStates[7] = false;
                    }
     
                    // double nd = d/10;
                    if (keyStates[0] && (py+5 < 300-d)) py += d; // pu
                    if (keyStates[1] && (py-5 > d)) py -= d; // pd
                    if (keyStates[2] && (px-5 > d)) px -= d; // pl
                    if (keyStates[3] && (px+5 < 300-d)) px += d; // pr
                    if (keyStates[4] && (hy+5 < 300-d)) hy += d; // hu
                    if (keyStates[5] && (hy-5 > d)) hy -= d; // hd
                    if (keyStates[6] && (hx-5 > d)) hx -= d; // hl
                    if (keyStates[7] && (hx+5 < 300-d)) hx += d; // hr
     
                    // write cases for characters intercepting each other
                    // wall bounce back function should be refined
     
     
                    // draw characters
                    StdDraw.clear(StdDraw.WHITE);
                    //StdDraw.setPenColor(StdDraw.BLACK);
                    for (int i = 0; i < numOfFlowers; i++) 
                    { //draw flowers
                        // if hx and hy touch, then dont draw the flower. draw it otherwise
                        double fx = flowers[i][0];
                        double fy = flowers[i][1];
     
                        if ( (hx-5<=fx+3) | (hx+5 >= fx-3) | (hy+5 >= fy-3) | (hy-5 <= fy+3) ){
                            fx = -5;
                            fy = -5; //get rid of flowers. was going to use arraylist but this might be the most straightfoward way
                            hs++;
                        }
     
                        if ( (px-5<=fx+3) | (px+5 >= fx-3) | (py+5 >= fy-3) | (py-5 <= fy+3) ){
                            fx = -5;
                            fy = -5;
                            ps++;
                        }
                        StdDraw.setPenColor(StdDraw.FLOWER_COLOR);
                        StdDraw.circle(flowers[i][0],flowers[i][1],flwidth);
                    }
                    StdDraw.setPenColor(StdDraw.HADES_COLOR); // draw hades
                    StdDraw.filledSquare(hx,hy,5);
                    StdDraw.setPenColor(StdDraw.PERSEPHONE_COLOR); // draw persephone
                    StdDraw.filledSquare(px,py,5);
     
     
     
                    // display characters on screen; animate
                    StdDraw.show(1);
                }
            }
        }
        /**
         * empty constructor for Game class
         */
        public Game()
        {
        } 
    }

  2. #2
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: StdDraw keyboard controls uncontrollable

    characters move too quickly
    That is often because the moves are made in a loop and the loop runs very quickly. If the loop is not using the JVM EDT then you could add a call to the Thread sleep method to add a delay between each new position.

    I don't know anything about the Std... classes and methods and can't help with those. Is there a forum for the Std... classes where you can ask your questions?

  3. #3
    Junior Member
    Join Date
    Jun 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: StdDraw keyboard controls uncontrollable

    thank you so much for that bit of advice!! i unfortunately don't know any specific Std class forums but will look online

Similar Threads

  1. Get all controls names on a layout
    By gezim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 1st, 2014, 09:14 AM
  2. StdDraw Cannot be Resolved??
    By jbarcus81 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 28th, 2014, 05:47 PM
  3. Replies: 1
    Last Post: June 25th, 2013, 08:49 AM
  4. Problem with Controls because of buttons
    By SagiIs in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 29th, 2011, 12:11 AM
  5. Replies: 2
    Last Post: July 8th, 2011, 06:33 AM

Tags for this Thread