Skip to content

Commit

Permalink
complete rewrite of key pressure detection
Browse files Browse the repository at this point in the history
prevents issues with double detection and no detection
  • Loading branch information
FoxtrotSierra6829 committed Jan 8, 2021
1 parent 6253912 commit 34fc01d
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 109 deletions.
Binary file modified Pushy Island/Bottle.class
Binary file not shown.
15 changes: 6 additions & 9 deletions Pushy Island/Bottle.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ public void act() {
setImage(image);
MyWorld.filled = true;
}
if ((!((Greenfoot.isKeyDown("W") | Greenfoot.isKeyDown("up") | Greenfoot.isKeyDown("S")
| Greenfoot.isKeyDown("down"))
& (Greenfoot.isKeyDown("D") | Greenfoot.isKeyDown("right") | Greenfoot.isKeyDown("A")
| Greenfoot.isKeyDown("left"))))) {
if (Greenfoot.isKeyDown("A") | Greenfoot.isKeyDown("left")) {
try {
if ("a".equals(MyWorld.key) | "left".equals(MyWorld.key)) {
if (canmoveleft()) {
setLocation(getX() - MyWorld.BlockSize, getY());
// Check if now in water
Expand All @@ -36,7 +33,7 @@ public void act() {
}
}
}
if (Greenfoot.isKeyDown("D") | Greenfoot.isKeyDown("right")) {
if ("d".equals(MyWorld.key) | "right".equals(MyWorld.key)) {
if (canmoveright()) {
setLocation(getX() + MyWorld.BlockSize, getY());
// Check if now in water
Expand All @@ -46,7 +43,7 @@ public void act() {
}
}
}
if (Greenfoot.isKeyDown("W") | Greenfoot.isKeyDown("up")) {
if ("w".equals(MyWorld.key) | "up".equals(MyWorld.key)) {
if (canmoveup()) {
setLocation(getX(), getY() - MyWorld.BlockSize);
// Check if now in water
Expand All @@ -56,7 +53,7 @@ public void act() {
}
}
}
if (Greenfoot.isKeyDown("S") | Greenfoot.isKeyDown("down")) {
if ("s".equals(MyWorld.key) | "down".equals(MyWorld.key)) {
if (canmovedown()) {
setLocation(getX(), getY() + MyWorld.BlockSize);
// Check if now in water
Expand All @@ -66,7 +63,7 @@ public void act() {
}
}
}

} catch (Exception e) {
}
checkbean();
}
Expand Down
Binary file modified Pushy Island/Box.class
Binary file not shown.
14 changes: 6 additions & 8 deletions Pushy Island/Box.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ public void act() {
}
}
// If not in water and exclude diagonal moves
if (!inwater & (!((Greenfoot.isKeyDown("W") | Greenfoot.isKeyDown("up") | Greenfoot.isKeyDown("S")
| Greenfoot.isKeyDown("down"))
& (Greenfoot.isKeyDown("D") | Greenfoot.isKeyDown("right") | Greenfoot.isKeyDown("A")
| Greenfoot.isKeyDown("left"))))) {
if (Greenfoot.isKeyDown("A") | Greenfoot.isKeyDown("left")) {
try {
if ("a".equals(MyWorld.key) | "left".equals(MyWorld.key)) {
if (canmoveleft()) {
setLocation(getX() - MyWorld.BlockSize, getY());
// Check if now in water
Expand All @@ -49,7 +46,7 @@ public void act() {
}
}
}
if (Greenfoot.isKeyDown("D") | Greenfoot.isKeyDown("right")) {
if ("d".equals(MyWorld.key) | "right".equals(MyWorld.key)) {
if (canmoveright()) {
setLocation(getX() + MyWorld.BlockSize, getY());
// Check if now in water
Expand All @@ -61,7 +58,7 @@ public void act() {
}
}
}
if (Greenfoot.isKeyDown("W") | Greenfoot.isKeyDown("up")) {
if ("w".equals(MyWorld.key) | "up".equals(MyWorld.key)) {
if (canmoveup()) {
setLocation(getX(), getY() - MyWorld.BlockSize);
// Check if now in water
Expand All @@ -73,7 +70,7 @@ public void act() {
}
}
}
if (Greenfoot.isKeyDown("S") | Greenfoot.isKeyDown("down")) {
if ("s".equals(MyWorld.key) | "down".equals(MyWorld.key)) {
if (canmovedown()) {
setLocation(getX(), getY() + MyWorld.BlockSize);
// Check if now in water
Expand All @@ -85,6 +82,7 @@ public void act() {
}
}
}
}catch (Exception e) {
}
}

Expand Down
Binary file modified Pushy Island/MyWorld.class
Binary file not shown.
47 changes: 40 additions & 7 deletions Pushy Island/MyWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;

public class MyWorld extends World {

static int speed = 40;
static int WorldWidth = 20;
static int WorldHeight = 12;
static int BlockSize = 75;
Expand All @@ -14,6 +14,9 @@ public class MyWorld extends World {
static int bean;
static boolean filled = false;
static boolean spring = false;
static String key;
int keycounter = 0;
int longpress = 3;
int levelnr = 1;
int highscorelevel = 1;
int maxlevel = 1;
Expand All @@ -30,11 +33,12 @@ public class MyWorld extends World {
public MyWorld() throws Exception {
// Create a new world with 1500x900 cells with a cell size of 1x1 pixels.
super(1500, 900, 1);
Greenfoot.setSpeed(34);
Greenfoot.setSpeed(speed);
filled = false;
spring = false;
bean = 0;
seastar = 0;
keycounter = 0;
new File((System.getProperty("user.home") + "/Documents/Pushy")).mkdirs(); // Create Pushy User folder if not
// existing
// get current game mode
Expand Down Expand Up @@ -296,6 +300,35 @@ else if (gamemode.equalsIgnoreCase(pushyisland)) {

/// ACT METHOD
public void act() {
// Get pressed key for all objects
String newkey = Greenfoot.getKey();
if ((keycounter) != 0) {
keycounter--;
}
if (newkey != null & keycounter == 0) {
key = newkey;
keycounter = longpress / 2;
} else if (keycounter == 0) {

if (Greenfoot.isKeyDown("w") | Greenfoot.isKeyDown("up")) {
key = "up";
keycounter = keycounter + longpress;
} else if (Greenfoot.isKeyDown("a") | Greenfoot.isKeyDown("left")) {
key = "left";
keycounter = keycounter + longpress;
} else if (Greenfoot.isKeyDown("s") | Greenfoot.isKeyDown("down")) {
key = "down";
keycounter = keycounter + longpress;
} else if (Greenfoot.isKeyDown("d") | Greenfoot.isKeyDown("right")) {
key = "right";
keycounter = keycounter + longpress;
} else {
key = null;
}
} else {
key = null;
}

// execute menu funcions when in menu
if (gamemode.equalsIgnoreCase(menu)) {
menu();
Expand All @@ -319,29 +352,29 @@ public void act() {

public void menu() {
// enter changes to Pushy Island/..
if (Greenfoot.isKeyDown("enter")) {
if ("enter".equals(key)) {
changemode(gotomode);
}
// escape changes to menu (currently: == reload)
if (Greenfoot.isKeyDown("escape")) {
if ("escape".equals(key)) {
changemode(menu);
}
}

public void PushyIsland() {
// escape changes to menu
if (Greenfoot.isKeyDown("escape")) {
if ("escape".equals(key)) {
changemode(menu);
}
}

public void win() {
// enter changes to menu
if (Greenfoot.isKeyDown("enter")) {
if ("enter".equals(key)) {
changemode(menu);
}
// escape changes to menu
if (Greenfoot.isKeyDown("escape")) {
if ("escape".equals(key)) {
changemode(menu);
}
}
Expand Down
Binary file modified Pushy Island/Pushy.class
Binary file not shown.
Loading

0 comments on commit 34fc01d

Please sign in to comment.