Skip to content

Instantly share code, notes, and snippets.

@oskopek
Last active August 29, 2015 14:06
Show Gist options
  • Save oskopek/b0735580d8d7f8ec642d to your computer and use it in GitHub Desktop.
Save oskopek/b0735580d8d7f8ec642d to your computer and use it in GitHub Desktop.
MKovar Game
package priklad;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.*;
public class Game extends GameEngine {
@Override
public void init() {
/*
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
if (e.getKeyCode() == KeyEvent.VK_UP) {
player.y -= MOVEPX*2;
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
player.y += MOVEPX*2;
}
}
});
*/
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
player.y = e.getY() - player.height/2;
}
});
cubes = new ArrayList<Rectangle>();
player = new Rectangle(200, 200, WIDTH, WIDTH);
setSize(GAME_WIDTH, GAME_HEIGHT);
offscreen = createImage(GAME_WIDTH, GAME_HEIGHT);
bufferGraphics = offscreen.getGraphics();
Thread th = new Thread(this);
th.start();
}
@Override
public void update(Graphics g) {
paint(g);
}
@Override
public void paint(Graphics g) {
bufferGraphics.clearRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
Color original = g.getColor();
bufferGraphics.setColor(Color.BLUE);
bufferGraphics.fillRect(player.x, player.y, player.width, player.height);
bufferGraphics.setColor(original);
for (Rectangle cube : cubes) {
bufferGraphics.fillRect(cube.x, cube.y, cube.width, cube.height);
}
g.drawImage(offscreen, 0, 0, this);
}
}
package priklad;
import java.applet.Applet;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.List;
public class GameEngine extends Applet implements Runnable {
Graphics bufferGraphics;
Image offscreen;
final int MOVEPX = 10;
final int SPEED = 90;
final int WIDTH = 50;
final Random random = new Random();
final static int GAME_WIDTH = 720;
final static int GAME_HEIGHT = 720;
public List<Rectangle> cubes;
public Rectangle player;
@Override
public void run() {
List<Rectangle> removeCubes = new ArrayList<Rectangle>();
for (int i = 0; true; i++) {
if (i % (100-SPEED) == 0) {
i = 0;
cubes.add(new Rectangle(GAME_WIDTH-WIDTH, random.nextInt(GAME_HEIGHT-WIDTH), WIDTH, WIDTH));
}
for (Rectangle cube : cubes) {
cube.x-=MOVEPX;
if (cube.x+WIDTH < 0) {
removeCubes.add(cube);
}
}
cubes.removeAll(removeCubes);
removeCubes.clear();
redraw();
}
}
private void sleep(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void redraw() {
if (isColliding()) {
bufferGraphics.clearRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
bufferGraphics.drawString("Game Over!", 200, 200);
getGraphics().drawImage(offscreen, 0, 0, this);
sleep(2000);
System.exit(0);
}
repaint();
sleep(20);
}
private boolean isColliding() {
for (Rectangle cube : cubes) {
if (player.intersects(cube)) {
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment