Detecting Key Presses in Java - java

I am making a game where there are two balls on the screen. One is computer controlled and moves by itself and the other you control with the left and right keys.
public class StartingPoint extends Applet implements Runnable, KeyListener
{
int x = 356;
int y = 74;
double dx = 5;
double dy = 6;
int radius = 20;
//private Image i;
//private Graphics doubleG;
int x2 = 0;
int y2 = 0;
double dx2 = 0;
double dy2 = 0;
int radius2 = 20;
boolean left = false;
boolean right = false;
#Override
public void init()
{
setSize(800, 600);
setVisible(true);
setFocusable(true);
System.out.println("Game Started!");
System.out.println("init");
}
#Override
public void start()
{
Thread thread = new Thread(this);
thread.start();
System.out.println("start");
}
#Override
public void run()
{
System.out.println("run");
while (true)
{
x += dx;
y += dy;
repaint(); //Goes to update method
if (x + dx > this.getWidth() -radius -1)
{
x = this.getWidth() - radius - 1;
dx = - dx;
}
else if (x + dx < 0 + radius)
{
x = 0 + radius;
dx = -dx;
}
else
{
x += dx;
}
if (y + dy > this.getHeight() -radius -1)
{
y = this.getHeight() - radius - 1;
dy = - dy;
}
else if (y + dy < 0 + radius)
{
y = 0 + radius;
dy = -dy;
}
else
{
y += dy;
}
System.out.println("New stuffs");
x2 += dx2;
y2 += dy2;
repaint(); //Goes to update method
if (x2 + dx2 > this.getWidth() -radius2 -1)
{
x2 = this.getWidth() - radius2 - 1;
dx2 = - dx2;
}
else if (x2 + dx2 < 0 + radius2)
{
x2 = 0 + radius2;
dx2 = -dx2;
}
else
{
x2 += dx2;
}
if (y2 + dy2 > this.getHeight() -radius2 -1)
{
y2 = this.getHeight() - radius2 - 1;
dy2 = - dy2;
}
else if (y2 + dy2 < 0 + radius2)
{
y2 = 0 + radius2;
dy2 = -dy2;
}
else
{
y += dy;
}
try
{
Thread.sleep(17);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public void keyPressed(KeyEvent e)
{
System.out.println("KeyPressed");
switch(e.getKeyCode())
{
case KeyEvent.VK_LEFT:
moveLeftPressedCode();
System.out.println("VK LEFT");
break;
case KeyEvent.VK_RIGHT:
moveRightPressedCode();
System.out.println("VK RIGHT");
break;
}
}
public void keyReleased(KeyEvent e2)
{
System.out.println("KeyReleased");
switch(e2.getKeyCode())
{
case KeyEvent.VK_LEFT:
moveLeftReleasedCode();
break;
case KeyEvent.VK_RIGHT:
moveRightReleasedCode();
break;
}
}
public void moveLeftPressedCode()
{
left = true;
System.out.println("");
}
public void moveLeftReleasedCode()
{
left = false;
}
public void moveRightPressedCode()
{
right = true;
}
public void moveRightReleasedCode()
{
right = false;
}
public void keyEventLeft()
{
if(dx2 >= -7)
{
while (left = true)
{
dx2 = - 7;
}
}
}
public void keyEventRight()
{
if(dx2 <= 7)
{
while (right = true)
{
//nothing yet
}
}
}
#Override
public void stop()
{
}
#Override
public void destroy()
{
}
#Override
public void paint(Graphics g)
{
g.setColor(Color.BLACK);
g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
g.setColor(Color.GREEN);
g.fillOval(x2 - radius2, y2 - radius2, radius2 * 2, radius2 * 2);
}
#Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
The problem that I am having is that when I press the left key (or right for that matter), the method doesn't get called and thus is not detecting the key press. How do I fix this or what do I need to add to be able to get the game to detect my key presses? -Thank you.

Related

Ball Collision - Collide with paddle

I'm trying to make a ball switch it's Y-Direction when it collides with the restrictions of the paddle. It is most likely a simple answer. Help would be appreciated. Thank you.
I labeled a couple of things to help.
public class GamePanel extends JPanel implements MouseMotionListener {
int Playerx;
int width;
int height;
// Ball Size
float radius = 20;
float diameter = radius * 2;
// Center of Call
float X = radius + 25;
float Y = radius + 10;
// Direction
float dx = 12;
float dy = 12;
GamePanel(){
setPreferredSize(new Dimension(1440, 900));
setFocusable(true);
setBackground(Color.BLACK);
addMouseMotionListener(this);
Thread thread = new Thread() {
public void run() {
while (true) {
width = getWidth();
height = getHeight();
X = X + dx ;
Y = Y + dy;
if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
}
if (Y - radius < 0) {
dy = -dy;
Y = radius;
}else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
}
if(X-radius>=Playerx && X-radius <- Playerx + 100 && Y+radius>=800 && Y+radius<=810){
dy = -dy;
Y = height - radius;
}//Statement Above is the issue area
repaint();
try {
Thread.sleep(25);
} catch (InterruptedException ex) {
}
}
}
};
thread.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
if(Playerx<=50 && Playerx>= 0){g.fillOval(10,800,100,10);}
if(Playerx>50 && Playerx<1390){g.fillOval(Playerx-50,800,100,10);}
if(Playerx>=1390 && Playerx<1440){g.fillOval(1340,800,100,10);}
g.setColor(Color.BLUE);
g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);
}
#Override
public void mouseDragged(MouseEvent e) {
}
#Override
public void mouseMoved(MouseEvent e) {
Playerx = e.getX();
repaint();
}
}
I've tried setting different restrictions and different Y-Directions but it doesn't seem to work. The Ball goes straight through the paddle as if it's not there.
I managed to fix it.
I realized that my Playerx was in the middle of the paddle making everything offset 50.
float X = radius;
float Y = radius;
and
public void run() {
while (true) {
counter++;
width = getWidth();
height = getHeight();
X = X + dx ;
Y = Y + dy;
if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
}
if (Y - radius < 0) {
dy = -dy;
Y = radius;
}else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
}if(X > Playerx-50 && X < Playerx+50 && Y+radius > paddleHeight && Y < 715){
dy = -dy;
Y = paddleHeight - radius;
}

Update method behaving different according to Entity array size.

I am making a college project and I am having trouble with my EntiyManager update method. This update system for some reason is making my bullets move faster according to my entity array size, if this array size is bigger than 1 the bullet will move faster if not the bullet will move normally.
I would like to have all my bullets moving normally.
note: b.getVel() and vel are always 2.
EntityManager methods:
public void update()
{
if(!handler.getPaused())
{
for(int i = 0; i < creatures.size(); i++)
{
//entities[i] at
Creatures c = creatures.get(i);
c.update();
if(!bullets.isEmpty())
{
for (int j = 0; j < bullets.size(); j++)
{
Bullet b = bullets.get(j);
//Hit detection;
if(c.getCollisionBounds(b.getVel(), 0).intersects(b.getCollisionBounds(0, 0)))
{
c.HP -= 5;
removeBullet(b);
}
System.out.println(b.getVel());
b.update();
}
}
else System.out.println("empty");
}
}
}
public void draw(Graphics g)
{
for(Entity se : staticEntities)
{
se.draw(g);
}
for(Bullet b : bullets)
{
b.draw(g);
}
for(Creatures c : creatures)
{
c.draw(g);
}
}
Bullet methods:
#Override
public void update()
{
if(vel > 0)
{
txRight = (int) (x + vel + bounds.x + bounds.width) / Tile.TILESIZE;
}
else
{
txLeft = (int) (x + vel + bounds.x) / Tile.TILESIZE;
}
if(collisionWithTile(txRight, (int) (y + bounds.y) / Tile.TILESIZE) ||
collisionWithTile(txRight, (int) (y + bounds.y + bounds.height) / Tile.TILESIZE) ||
collisionWithTile(txLeft, (int) (y + bounds.y) / Tile.TILESIZE) ||
collisionWithTile(txLeft, (int) (y + bounds.y + bounds.height) / Tile.TILESIZE))
{
handler.getLevel().getEntityManager().removeBullet(this);
}
x += vel;
}
#Override
public void draw(Graphics g)
{
g.setColor(Color.yellow);
g.fillRect((int) x, (int) y, width, height);
}
I now know why this is happening. It is updating twice the bullet.

Always a Collision

I created a tilemap and a player (Which is just a squiggle right now). I put the tilemap and the player on the screen, but the player is in the upper left corner, and is always stuck in the tile. I can't figure out how to:
Get it out of the corner and
stop the collision from happening because it's a non-blocked block.
A few things you need to know before the code:
The tilesheet is the sheet with the different tiles on it and it goes
[non-blocked, nonblocked]
[blocked, blocked]
Here's the collision part of my object superclass (I know it looks like a lot but after the beginning you can get the drift):
public void calculateCorners(double x, double y) {
int leftTile = (int)(x - cwidth / 2) / tileSize;
int rightTile = (int)(x + cwidth / 2 - 1) / tileSize;
int topTile = (int)(y - cheight / 2) / tileSize;
int bottomTile = (int)(y + cheight / 2) / tileSize;
int tl = tileMap.getType(topTile, leftTile);
int tr = tileMap.getType(topTile, rightTile);
int bl = tileMap.getType(bottomTile, leftTile);
int br = tileMap.getType(bottomTile, rightTile);
topLeft = tl == Tile.BLOCKED;
topRight = tr == Tile.BLOCKED;
bottomLeft = bl == Tile.BLOCKED;
bottomRight = br == Tile.BLOCKED;
}
public void checkTileMapCollision() {
currCol = (int) x / tileSize;
currRow = (int) y / tileSize;
x += velX;
y += velY;
calculateCorners(x, ydest);
if (velY < 0) {
if (topLeft || topRight) {
velY = 0;
y = currRow * tileSize + cheight / 2;
System.out.println("Collision");
} else {
y += velY;
}
}
if (velY > 0) {
if (bottomLeft || bottomRight) {
velY = 0;
falling = false;
y = (currRow + 1) * tileSize - cheight / 2;
System.out.println("Collision");
} else {
y += velY;
}
}
calculateCorners(xdest, y);
if (velX < 0) {
if (topLeft || bottomLeft) {
velX = 0;
System.out.println("Collision");
x = currCol * tileSize + cwidth / 2;
} else {
x += velX;
}
}
if (velX > 0) {
if (topRight || bottomRight) {
velX = 0;
System.out.println("Collision");
x = (currCol + 1) * tileSize - cwidth / 2;
System.out.println("Collision");
} else {
x += velX;
}
}
}
And here's the most important stuff in my Player class:
public void update() {
System.out.println("update");
getNextPosition();
checkTileMapCollision();
setPosition((int) xtemp, (int) ytemp);
}
public void setLeft(boolean b) {
left = b;
}
public void setRight(boolean b) {
right = b;
}
public void getNextPosition() {
if (left) {
velX = -2;
System.out.println("Left");
} else if (right) {
velX = 2;
System.out.println("Right");
}
}
public void draw(Graphics2D g) {
x += velX;
y += velY;
g.drawImage(spriteSheet, (int) x, (int) y, null);
}
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
public void setVel(int velX, int velY) {
this.velX = velX;
this.velY = velY;
}
If you need anything else, just tell me and I'll give it to you.

Flickering issues with a pong game

Hello I am having these flickering issues with this pong game, and was hoping someone else could steer me in the right direction. been at this for like an hour or 2 and can not figure it out.
It would be greatly appreciated ?.? like the ball and score are the worst and disappear almost completely when I embed the game.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Pong extends Applet implements MouseMotionListener, KeyListener {
int doubleb,i,my,bx,by,px,py,compx,compy,width,height,speedx,speedy,bwidth,bheight,pwidth,pheight,score;
boolean started;
private Timer timer1;
#Override
public void init() {
setSize(800,500); //setting screan size, declaring background color and some of my listeners
width = getSize().width;
height = getSize().height;
setBackground(Color.black);
pheight = 120;
pwidth = 15;
bheight = 30;
bwidth = 30;
addKeyListener(this); //listers used for the mouse and keybored input
addMouseMotionListener(this);
px = 35;
compx = width - 35 - pwidth;
newgame();
timer1 = new Timer(10,new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) { //momvement, physics, collusion
height = getSize().height;
width = getSize().width;
bx += speedx;
by += speedy;
if (by <= 0 || by + bheight >= height) {
speedy = -speedy;
}
if (bx <= px + pwidth && by + bheight >= py && by <= py + pheight && bx > px) {
speedx = -speedx;
++score;
}
if (bx + bwidth >= compx && by + bheight >= compy && by <= compy + pheight && bx < compx + pwidth) {
speedx = -speedx;
}
if (speedx < 0) {
if (compy + pheight / 2 != height / 2) {
if (compy + pheight / 2 > height / 2) {
compy -= -speedx;
}
else {
compy += -speedx;
}
}
}
else {
if (by + bheight / 2 <= compy + pheight / 2) {
compy -= speedx;
}
else {
compy += speedx;
}
}
if (compy < 0) {
compy = 0;
}
if (compy + pheight > height) {
compy = height - pheight;
}
if (bx + bwidth < 0) {
py = height / 2 - pheight / 2;
timer1.stop();
started = false;
}
repaint();
}
});
}
#Override
public void mouseMoved(MouseEvent e) {
if (started) {
my = e.getY();
if (my + pheight / 2 > height) {
my = height - pheight / 2;
}
if (my < pheight / 2) {
my = pheight / 2;
}
py = my - pheight / 2;
repaint();
}
}
#Override
public void mouseDragged(MouseEvent e) { }
#Override
public void paint(Graphics g) {
Font font1 = new Font("Arial", Font.BOLD, 18); //set font
Font font2 = new Font("Arial", Font.BOLD,40); //set font
g.setColor(Color.white);
g.drawRect(0,0,width - 1,height - 1);
g.fillRect(px,py,pwidth,pheight);
g.fillRect(compx,compy,pwidth,pheight);
g.setFont(font1);
g.drawString("Score: " + score,20,20); //paints the sorce
if (started) {
g.fillArc(bx,by,bwidth,bheight,0,360);
}
else {
g.setFont(font2); //set font
g.setColor(Color.green); //set color
g.drawString("Pong",width / 2 - 46,height / 2 - 16); //draws text to the screen
g.setFont(font1);
g.drawString("Press 's' to start",width / 2 - 69,height / 2 + 30);
}
}
public void newgame() {
py = height / 2 - pheight / 2;
compy = py;
bx = width / 2 - bwidth / 2;
by = height / 2 - bheight / 2;
speedx = 10;
speedy = 10;
score = 0;
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == 's') {
started = true;
newgame();
timer1.start();
}
}
#Override
public void keyTyped(KeyEvent e) { }
#Override
public void keyReleased(KeyEvent e) { }
}
you might want to look into double buffering
Basically, the issue you're having is that you clear the screen in order to redraw it. However, before the Graphics objects is completely finished updating, it gets drawn to the screen. This results in a half finished screen, and a noticeable flickering effect.
The fix to this is to instead draw your image to a seperate Graphics object first, then when it is done, swap that Graphics object with the one being displayed.
more information can be found here

Restarting millis in processing error

Having a massive problem with this piece of code. I'm working with Java in processing.
I've created a game where users must guide a character away from objects.
All the objects, health system and score system are based on mills().
Once the game ends we need to reset millis(), resetting the objects, score and health system.
I have searched implemented advice from friends and previously asked questions on here but the advice differs very slightly. I'm assuming it's something that I can't see.
I would really appreciate your help with this, I only ever use this site as a last resort not just when I'm feeling lazy.
//these are used to set the times which the games increases difficulty
//int timeDelay = 30000;
int delayOne = 2000;
int delayTwo = 5000;
int delayThree = 80000;
int delayFour = 90000;
int display = 2000;
//for collisions
float[] xpos = new float[6];
float[] ypos = new float[6];
//timer counts how many millis() each game lasts for
int timeStamp = 5000;
int timer;
int timer2 = millis() - timer;
//always at zero
//outputting score at the end of a game
int score;
int start;
//trying to get lives working
boolean lost = false;
//variable to store & output gale force when giving score
int Gale = 0;
//Changing length rectangle
float rX = 350.0;
float x1 = 20;
float y1 = 20;
float w1 = 100;
float h1 = 30;
//DECLARE OBJECTS JELLY CLASS
Jelly myObject;
Jelly myObject1;
Jelly myObject2;
Jelly myObject3;
//GENTLEMAN CLASS
gentleMan mygentleMan;
//LOLLY CLASS
Lolly myCow;
Lolly myCow1;
//PImages
PImage loader;
PImage bg;
PImage uh;
PImage bg1;
PImage bolt;
PImage over;
void setup()
{
bg=loadImage("backy1.png");
bg1 = loadImage("backy.png");
over = loadImage("over.png");
PFont L = loadFont("Lobster1.3-48.vlw");
textFont( L, 16);
size(400, 600);
smooth();
//begin = millis();
imageMode(CENTER);
//INITIALISE
myObject = new Jelly(320, 500);
myObject1 = new Jelly(150, 200);
// myObject2 = new Jelly(550, 500);
//myObject3 = new Jelly(300, 100);
mygentleMan = new gentleMan(200, 300);
//myObject.run();
//myObject1.run();
//myObject2.run();
myCow = new Lolly(400, 250);
myCow1 = new Lolly(150, 350);
timer = millis();
}
void draw()
{
start = 0;
//because we have image mode set to center for collisions
//we have to divide the height & width of the screen by 2 for hte image to fit
image(bg, 200, 300);
if (millis() >= start + delayOne)
{
image(bg1, 200, 300);
}
//CALL FUNCTIONALITY
myObject.run();
myObject.put_in_array(0);
myObject1.run(); // this one is going top to bottom
myObject1.put_in_array(1);
// myObject2.run();
//myObject2.put_in_array(2);
// myObject3.run();
//myObject3.put_in_array(3);
myCow.run();
myCow.put_in_array(4);
myCow1.run();
myCow1.put_in_array(5);
mygentleMan.run();
//health bar
fill(161, 221, 16);
noStroke();
rect(10, 24, rX, 10);
if(rX <= 100)
{
fill(221, 59, 16);
rect(10, 24, rX, 10);
}
else
if(rX <= 200)
{
fill(221, 137, 16);
rect(10, 24, rX, 10);
}
if(rX == 5.0)
{
lost = true;
noLoop();
// lives = lives - 1;
image(over, width/2, height/3);
fill(255);
text("Your Score Is: " + timer, width/2.7, height/2);
text("Gale Force Is; " + Gale, width/2.7, height/1.8);
score = timer;
}
//For Loop detecting collisions between mygentleMan & objects
for (int i=0; i < 6; i++) {
if (xpos[i] > 150 && xpos[i] < 250 && ypos[i] > (mygentleMan.y-58) && ypos[i] < (mygentleMan.y+58))
{
// text("collision", 200, 300);
bolt = loadImage("bolt.png");
image(bolt, xpos[i], ypos[i]);
rX = rX - 1;
}
//outputting score on screen # at all times
fill(255);
text("Score: " + timer, 320, 20);
}
//timer which will be score counter essentially
timer = millis();
//text(timer, 20, 20);
//moving the man up the screen if button is pressed, if not he levitates downward
if (keyPressed)
{
mygentleMan.y -= mygentleMan.moveY;
mygentleMan.moveY += 0.4;
}
else
{
mygentleMan.y += mygentleMan.moveY;
mygentleMan.moveY += 0.2;
}
fill(255);
text("Health", 20, 20);
if(mousePressed)
{
if(timer2 > timeStamp)
{
println("tit");
mygentleMan.y = height/2;
loop();
}
}
}
//class for first objects that move into the screen
class Jelly
{
//GLOBAL VARIABLES
float x = 0;
float y = 0;
float speedX = 1.8;
float speedY = 1.8;
float speedX2 = 2.1;
float speedY2 = 2.1;
float speedX3 = 2.2;
float speedY3 = 2.2;
PImage jelly = loadImage("jelly.png");
PImage hat = loadImage("hat.png");
PImage gale = loadImage("g1.png");
PImage force = loadImage("force.png");
PImage news = loadImage("news.png");
//CONSTRUCTOR
Jelly(float _x, float _y)
{
x = _x;
y = _y;
}
//FUNCTIONS
void run()
{
display();
move();
bounce();
image(force, 330, 550);
if (millis() >= start + delayOne)
{
display();
moveFast();
bounceFast();
image(gale, 280, 560);
Gale = 1;
if (start + delayOne + display >= millis())
{
image(news, 200, 300);
}
}
if (millis() >= start +delayTwo)
{
display();
moveFaster();
bounceFaster();
image(gale, 310, 560);
Gale = 2;
if (start + delayTwo + display >= millis())
{
image(news, 200, 300);
}
}
}
void bounce()
{
if ( x > width)
{
speedX = speedX * -1; //multiply by -1 to make it bounce
}
if ( x < 0)
{
speedX = speedX * -1;
}
if ( y > height)
{
speedY = speedY * -1;
}
if ( y < 0)
{
speedY = speedY * -1;
}
}
void bounceFast()
{
if ( x > width)
{
speedX2 = speedX2 * -1; //multiply by -1 to make it bounce
}
if ( x < 0)
{
speedX2 = speedX2 * -1;
}
if ( y > height)
{
speedY2 = speedY2 * -1;
}
if ( y < 0)
{
speedY2 = speedY2 * -1;
}
}
void bounceFaster()
{
if ( x > width)
{
speedX3 = speedX3 * -1; //multiply by -1 to make it bounce
}
if ( x < 0)
{
speedX3 = speedX3 * -1;
}
if ( y > height)
{
speedY3 = speedY3 * -1;
}
if ( y < 0)
{
speedY3 = speedY3 * -1;
}
}
void move()
{
x = x + speedX;
y = y + speedY;
}
void moveFast()
{
x = x + speedX2;
y = y + speedY2;
}
void moveFaster()
{
x = x + speedX3;
y = y + speedY3;
}
void put_in_array(int a)
{
xpos[a] = x;
ypos[a] = y;
}
void display()
{
image(hat, x, y);
}
}
//class for gentleman that floats
class gentleMan
{
//GLOBAL VARIABLES
float y = 400;
float x = 400;
float moveY;
//PImage umbrella;
PImage umbrella = loadImage("dafuq.png");
PImage over = loadImage("over.png");
//CONSTRCUTOR --- PIECES OF INFO PROVDE TO BUILD CLASS -- INTIIALIZE VARIBALE
gentleMan(float _x, float _y)
{
y = _y;
x = _x;
moveY = 2;
}
//FUNCTIONS
void run()
{
display();
keyReleased();
bounce();
// collision();
}
void display()
{
image(umbrella, x, y);
}
void keyReleased()
{
mygentleMan.moveY = 4;
}
void bounce()
{
if ( y < 0)
{
y = 0;
}
if (y > height)
{
//score = millis();
lost = true;
noLoop();
// lives = lives - 1;
image(over, width/2, height/3);
text("Your Score Is: " + timer, width/2.7, height/2);
text("Gale Force Is; " + Gale, width/2.7, height/1.8);
}
}
}
class Lolly
{
//GLOBAL VARIABLES
float x = 0;
float y = 0;
float speedX = 2;
float speedY = 2;
float speedX1 = 2.1;
float speedY1 = 2.1;
float speedX2 = 2.3;
float speedY2 = 2.3;
PImage cow = loadImage("cow.png");
//CONSTRUCTOR
Lolly(float _x, float _y)
{
x = _x;
y = _y;
}
//FUNCTIONS
void run()
{
// display();
//move();
//bounce();
if (millis() >= start + delayThree)
{
display();
moveFast();
bounceFast();
}
if (millis() >= start +delayFour)
{
display();
moveFaster();
bounceFaster();
}
}
void put_in_array(int a)
{
xpos[a] = x;
ypos[a] = y;
}
void bounce()
{
if ( x > width)
{
speedX = speedX * -1; //multiply by -1 to make it bounce
}
if ( x < 0)
{
speedX = speedX * -1;
}
if ( y > height)
{
speedY = speedY * -1;
}
if ( y < 0)
{
speedY = speedY * -1;
}
}
void bounceFast()
{
if ( x > width)
{
speedX1 = speedX1 * -1; //multiply by -1 to make it bounce
}
if ( x < 0)
{
speedX1 = speedX1 * -1;
}
if ( y > height)
{
speedY1 = speedY1 * -1;
}
if ( y < 0)
{
speedY1 = speedY1 * -1;
}
}
void bounceFaster()
{
if ( x > width)
{
speedX2 = speedX2 * -1; //multiply by -1 to make it bounce
}
if ( x < 0)
{
speedX2 = speedX2 * -1;
}
if ( y > height)
{
speedY2 = speedY2 * -1;
}
if ( y < 0)
{
speedY2 = speedY2 * -1;
}
}
void move()
{
x = x + speedX;
y = y + speedY;
}
void moveFast()
{
x = x + speedX1;
y = y + speedY1;
}
void moveFaster()
{
x = x + speedX2;
y = y + speedY2;
}
void display()
{
image(cow, x, y);
}
}//end of cow class
void mousePressed()
{
}
Your question is not at all clear, but it sounds like you should be able to wrap System.currentTimeMillis() in an object that maintains a starting point and returns the offset. Something like
public class Millis
{
long start;
public Millis() { this.reset(); }
public void reset() { this.start = System.currentTimeMillis(); }
public long getMillis() { return System.currentTimeMillis() - start; }
}
You create an instance of this class at startup
Millis timer = new Millis();
then call reset() to set it back to zero at the beginning of each game. Everywhere in your code you currently have millis() you would have timer.getMillis()
Use a custom timer class
class Timer {
int savedTime; // When Timer started
int totalTime; // How long Timer should last
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
// Starting the timer
void start() {
// When the timer starts it stores the current time in milliseconds.
savedTime = millis();
}
// The function isFinished() returns true if 5,000 ms have passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Check how much time has passed
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}//end class
Then to call
firstTimer = new Timer(50000);
And then in the draw to check
if (firstTimer.isFinished()) {
//do sopemthing
//then restart timer
firstTimer.start();
}
else
{
// do soemthing else for the rest of the time....
}

Categories

Resources