I'm making a game where you jump on circles and if there is no circle you mustn't jump on it, if you jump you die. Now I'm making circles to dis/appear and to show warning when will they disappear. I already tried to code something, but this is how my game looks:
If you could help me finish what I started I would be very thankful to you! My code:
package com.pitcher654.main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.util.Random;
import javax.swing.Timer;
import com.pitcher654.main.Game.STATE;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = -7800496711589684767L;
public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;
private Thread thread;
private boolean running = false;
private static final int NUM_STONES = 5; // you can change the # of the stones here
private boolean[] visible = new boolean[NUM_STONES];
private boolean[] warning = new boolean[NUM_STONES];
private long[] changeTimes = new long[NUM_STONES];
private Random r;
private Handler handler;
//private HUD hud;
private Menu menu;
public enum STATE {
Menu,
Help,
Game
};
public STATE gameState = STATE.Menu;
public Game() {
handler = new Handler();
menu = new Menu(this, handler);
this.addKeyListener(new KeyInput(handler));
this.addMouseListener(menu);
new Window(WIDTH, HEIGHT, "My game", this);
//hud = new HUD();
r = new Random();
//if(gameState == STATE.Game) {
if(gameState == STATE.Game) { //handler.addObject(new Player(100, 200, ID.Player));
for(int i=0; i<NUM_STONES; i++){
visible[i] = true;
warning[ i ] = false;
//System.out.println("it works");// each stone will be visible
changeTimes[i] = System.currentTimeMillis() + r.nextInt(10000) + 3000; // every stone will disappear in less than 10 seconds
//}
}
}
//handler.addObject(new Player(100, 200, ID.Player));
//handler.addObject(new BasicEnemy(100, 200, ID.BasicEnemy));
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop() {
try {
thread.join();
running = false;
}catch(Exception ex) { ex.printStackTrace(); }
}
public void run()
{
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(running)
{
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >=1)
{
tick();
delta--;
}
if(running)
render();
frames++;
if(System.currentTimeMillis() - timer > 1000)
{
timer += 1000;
//System.out.println("FPS: "+ frames);
frames = 0;
}
}
stop();
}
private void tick() {
handler.tick();
//hud.tick();
if(gameState == STATE.Game) {
Random r = new Random();
long now = System.currentTimeMillis();
int b = 0;
//System.out.println(now);
long extraSeconds = 500;
for(int i=0; i<NUM_STONES; i++){
for(int j = 0; j < NUM_STONES; j++) {
if(visible[ i ]) b++;
}
//if(b < 2) continue;
if(now > changeTimes[ i ] - 1400 && now <= changeTimes[ i ]) {
System.out.println("Usli u zeleno");
warning[ i ] = !warning[ i ];
}else if(now > changeTimes[i]){
//System.out.println(changeTimes[ i ]);
//System.out.println("The time has come!");// if the time has come
if(visible[i]) {
changeTimes[i] = now + extraSeconds + r.nextInt(5000);
if(b < 2) {;
visible[ i ] = !visible[ i ];
changeTimes[ i ] += 5000;
}
}// every stone will be invisible up to five seconds
else {
changeTimes[i] = now + extraSeconds + r.nextInt(10000); //System.out.println("Uslo u kad nije visible"); // every stone will be visible again up to 10 seconds
}
visible[i] = !visible[i];
//System.out.println(visible[ i ]);// switch the visibility state
}else if (now > changeTimes[ i ] + 1000) {
System.out.println("Usli u zeleno");
}
b = 0;
}
}else if(gameState == STATE.Menu) {
menu.tick();
}
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
long now = System.currentTimeMillis();
g.setColor(new Color(87, 124, 212));
g.fillRect(0, 0, WIDTH, HEIGHT);
if(gameState == STATE.Game) {
g.setColor(new Color(209, 155, 29));
for(int i = 0; i < 5; i++) {
g.fillOval(80 + (100 * i), 325, 70, 20);
}
if(gameState == STATE.Game) {
for(int i = 0; i < NUM_STONES; i++) {
if(visible[i]) g.setColor(new Color(209, 155, 29));
else if(warning[ i ]) g.setColor(Color.black);
else g.setColor(new Color(107, 155, 170)); //System.out.println("change blue color!");
g.fillOval(80 + (100 * i), 325, 70, 20);
}
}
}else if(gameState == STATE.Menu || gameState == STATE.Help){
menu.render(g);
}
handler.render(g);
if(gameState == STATE.Game) {
}
//hud.render(g);
g.dispose();
bs.show();
}
public static int clamp(int var, int min, int max) {
if(var >= max)
return var = max;
else if(var <= max)
return var = min;
else
return var;
}
public static void main(String[] args) {
new Game();
}
}
Aucun commentaire:
Enregistrer un commentaire