Trying to rewrite Snake - Snake eats itself - java

I got a (to me, at least) very strange situation.
I am trying to rewrite snake, and moving is going very well, only the snake is eating itself, although I am removing 1 segment, and adding 1 (x + 1 - 1 = x?) but the snake disappears, while the Arraylist keeps the same size (printing it).
The function to calculate the position of the snake (I think that is causing it):
private void move() {
System.out.println(position.size());
Point toAdd = position.get(position.size() - 1);
position.remove(0);
if(dir == 1)
toAdd.y -= 5;
else if(dir == 2)
toAdd.x -= 5;
else if(dir == 3)
toAdd.x += 5;
else if(dir == 4)
toAdd.y += 5;
if(toAdd.x < 0) toAdd.x = 150;
else if(toAdd.x > 150) toAdd.x = 0;
if(toAdd.y < 0) toAdd.y = 150;
else if(toAdd.y > 150) toAdd.y = 0;
position.add(toAdd);
}
But, to make it a Short, Self Contained, Correct Example:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.*;
import java.util.ArrayList;
class Snake extends JPanel implements Runnable {
int x,y;
boolean horizontal;
ArrayList<Point> position = new ArrayList<Point>();
byte dir = 3;
public Snake(JFrame parent) {
for(int i = 0; i < 5; i++)
position.add(new Point(i*5 + 10, 10));
parent.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
byte change = dir;
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
change = 4;
break;
case KeyEvent.VK_UP:
change = 1;
break;
case KeyEvent.VK_LEFT:
change = 2;
break;
case KeyEvent.VK_RIGHT:
change = 3;
break;
}
changeDirection(change);
}
});
System.out.println("starting thread");
new Thread(this).start();
}
private void changeDirection(byte change) {
if(change != dir && (change - 2 != dir || change + 2 != dir)) {
dir = change;
}
}
public void run() {
for(int i = 0; i != -1; i++){
try {
move();
this.repaint();
Thread.sleep(1000);
}
catch (InterruptedException e) {
System.out.println("INTERRUPTED");
}
}
}
private void move() {
System.out.println(position.size());
Point toAdd = position.get(position.size() - 1);
position.remove(0);
if(dir == 1)
toAdd.y -= 5;
else if(dir == 2)
toAdd.x -= 5;
else if(dir == 3)
toAdd.x += 5;
else if(dir == 4)
toAdd.y += 5;
if(toAdd.x < 0) toAdd.x = 150;
else if(toAdd.x > 150) toAdd.x = 0;
if(toAdd.y < 0) toAdd.y = 150;
else if(toAdd.y > 150) toAdd.y = 0;
position.add(toAdd);
}
public void paintComponent(Graphics g) {
g.clearRect(0,0,150,150);
for(Point p : position)
g.drawRect(p.x, p.y, 5, 5);
}
public static void main(String[] args) {
Snake snake;
JFrame f = new JFrame("Snake");
f.setSize(150, 150);
f.add((snake = new Snake(f)));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.setVisible(true);
}
}
Movement was the first thing I tried to implement, I have not yet come to collision-detection/food, that's not the question here.
My question is:
Why is the snake shrinking while the arraylist isn't and how can I fix it, i.e. stop the snake from disappearing except for the front segment?

Ok. This is simple!
You are changing point that is already in the list position and at the end you add the same point to this list. You end up with more and more the same (by reference) elements with same positions.
You should create new instance of Point and add it to the list.
Point lastPoint = position.get(position.size() - 1);
Point toAdd = new Point(lastPoint.x, lastPoint.y);

Related

Snake Game: how not to make it possible for the fruit to spawn in the same position as the body

This is my first question and I apologize if it's not posed as it should be in advance.
So, I'm trying to make the fruits (I also call them apples in code) spawn on a position that is different from the snake body just like the title says. The problem is that I don't know how to check if the coordinates of the new fruit is not equal to each of the body parts of the snake.
public void newApple() {
tempX = random.nextInt(width/unitSize)*unitSize;
tempY = random.nextInt(height/unitSize)*unitSize;
for (int i = 0; i < bodyParts; i++) {
if (tempX != x[i] && tempY != y[i]){
appleX = tempX;
appleY = tempY;
}
else {
newApple();
}
}
}
This is what I've figured so far: I'm using 2 temp variables that will correspond to a random position in the frame and then I want to check if those are not equal to each position of the snake. The if condition inside the for loop just checks for x[0] and y[0] which are the coordinates of the head of my snake though.
This is what's inside the paintComponent() method to draw the apple.
//drawing apple
if (appleX != null && appleY != null) {
g.setColor(Color.red);
g.fillOval(appleX, appleY, unitSize, unitSize);
}
Down below is my entire 2 classes of the project if you may need it.
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createGame();
}
});
}
public static void createGame() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new MyPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
public class MyPanel extends JPanel implements ActionListener {
final int width = 600;
final int height = 600;
final int unitSize = 25;
final int maxNumberOfUnits = (width*height)/(unitSize*unitSize);
int[] x = new int[maxNumberOfUnits];
int[] y = new int[maxNumberOfUnits];
int bodyParts = 2;
Integer appleX, appleY, tempX, tempY;
int score = 0;
char direction = 'R';
boolean running;
int delay = 175;
Timer timer = new Timer(delay, this);
Random random = new Random();
Font font1 = new Font("Ink Free", Font.BOLD, 40);
Font font2 = new Font("Ink Free", Font.BOLD, 75);
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
MyPanel() {
setBackground(new Color(16, 16, 16));
setFocusable(true);
timer.start();
running = true;
newApple();
x[0] = 10*unitSize;
y[0] = 10*unitSize;
x[1] = 9*unitSize;
y[1] = 10*unitSize;
addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case 37:
if (direction !='R') {
direction = 'L';
}
break;
case 38:
if (direction !='D') {
direction = 'U';
}
break;
case 39:
if (direction !='L') {
direction = 'R';
}
break;
case 40:
if (direction !='U') {
direction = 'D';
}
break;
}
}
});
}
public void moveSnake() {
for (int i = bodyParts; i > 0; i--) {
x[i] = x[i-1];
y[i] = y[i-1];
}
switch (direction) {
case 'U':
y[0] -= unitSize;
break;
case 'D':
y[0] += unitSize;
break;
case 'L':
x[0] -= unitSize;
break;
case 'R':
x[0] += unitSize;
break;
}
}
public void newApple() {
tempX = random.nextInt(width/unitSize)*unitSize;
tempY = random.nextInt(height/unitSize)*unitSize;
for (int i = 0; i < bodyParts; i++) {
if (tempX != x[i] && tempY != y[i]){
appleX = tempX;
appleY = tempY;
}
else {
newApple();
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (running) {
/*
//drawing a matrix
for (int i = 1; i < height/unitSize; i++) {
g.drawLine(0, i*unitSize, width, i*unitSize);
}
for (int i = 1; i < width/unitSize; i++) {
g.drawLine(i*unitSize, 0, i*unitSize, height);
}
*/
//drawing apple
if (appleX != null && appleY != null) {
g.setColor(Color.red);
g.fillOval(appleX, appleY, unitSize, unitSize);
}
//drawing snake
g.setColor(Color.green);
for (int i = 0; i < bodyParts; i++) {
g.fillRect(x[i], y[i], unitSize, unitSize);
}
//drawing score
g.setColor(Color.red);
g.setFont(font1);
FontMetrics metrics1 = getFontMetrics(g.getFont());
g.drawString("Score: " + score, (width-metrics1.stringWidth("Score: " + score))/2, g.getFont().getSize());
}
else {
g.setColor(Color.red);
g.setFont(font1);
FontMetrics metrics1 = getFontMetrics(g.getFont());
g.drawString("Score: " + score, (width-metrics1.stringWidth("Score: " + score))/2, g.getFont().getSize());
g.setColor(Color.white);
g.setFont(font2);
FontMetrics metrics2 = getFontMetrics(g.getFont());
g.drawString("Game Over", (width-metrics2.stringWidth("Game Over"))/2, height/2);
}
}
public void checkApple() {
if (x[0]==appleX && y[0]==appleY) {
score++;
bodyParts++;
newApple();
}
}
public void checkCollisions() {
//body
for (int i = bodyParts; i > 0; i--) {
if (x[0] == x[i] && y[0] == y[i]) {
running = false;
}
}
if (x[0] < 0) {
running = false;
}
if (x[0] > width) {
running = false;
}
if (y[0] < 0) {
running = false;
}
if (y[0] > height) {
running = false;
}
if (!running) {
timer.stop();
}
}
#Override
public void actionPerformed(ActionEvent e) {
if (running) {
checkApple();
moveSnake();
checkCollisions();
}
repaint();
}
}
You don't need a recursive call. Method newApple() should not call itself. You need a loop.
Generate values for tempX and tempY.
For each part of the snake check whether tempX equals the snake body part x[i] and also tempY equals y[i].
If at least one part of the snake body equals the coordinates tempX and tempY, go back to step 1, above.
If none of the snake body parts equals the coordinates, assign the generated coordinates to the "apple".
This is method newApple()
public void newApple() {
boolean ok = false;
while (!ok) {
ok = true;
tempX = random.nextInt(width/unitSize)*unitSize;
tempY = random.nextInt(height/unitSize)*unitSize;
for (int i = 0; i < bodyParts; i++) {
if (tempX == x[i] && tempY == y[i]) {
ok = false;
break;
}
}
if (ok) {
appleX = tempX;
appleY = tempY;
}
}
}
I think you should have an array of all the location of body parts of snakes as any body part could do collision (As if the snake collide then it's game over).
You can use the same Array for fruit respawn just ensuring that it is not in the array.
You should verify whether tempX and tempY match the values in the x and y arrays and call newApple() if they do and break out of the loop.
This means, that if you reach the end of the loop, no matches were found. You can then safely set the apple's x and y coordinates:
public void newApple() {
tempX = random.nextInt(width/unitSize)*unitSize;
tempY = random.nextInt(height/unitSize)*unitSize;
for (int i = 0; i < bodyParts; i++) {
if (tempX == x[i] && tempY == y[i]){
newApple();
break;
}
if ( i == bodyParts - 1 ) {
appleX = tempX;
appleY = tempY;
}
}
}
If I were in your shoes, I would try to avoid recursion and create a separate function to calculate if a coordinate collides with the snake's body.
A basic idea would be like this:
newApple()
- randomize a new coordinate x,y
- while overlapsWithSnake(x,y)
- randomize a new coordinate x,y
overlapsWithSnake(x,y)
- iterate through all snake coordinates
- if coordinate is the same, return true
- return false
Now, I need to point out that the algorithm does not consider when there is no more free space in the screen (the end of the game).
Additionally, this algorithm takes more and more time to find an available coordinate as the snake's body grows. What you could do to improve that is the following:
newApple()
- assign the result of getAvailableSpaces() to a list
- if the list is empty, then is game over
- else, randomize a number between 0 and list size - 1 as p
- select the coordinate at position p of the list
getAvailableSpaces()
- create logic to return a list of all free coordinates
The problem is that you only check if the apple is in the first body parts index.
You need to complete the for loop before checking if the apple can be placed.
boolean applePlacementOk = true;
while(true){
applePlacementOk = true;
tempX = random.nextInt(width/unitSize)*unitSize;
tempY = random.nextInt(height/unitSize)*unitSize;
for (int i = 0; i < bodyParts; i++) {
if (tempX == x[i] && tempY == y[i]){
applePlacementOk = false;
break;
}
}
if(applePlacementOk){
break;
}
}
appleX = tempX;
appleY = tempY;
Edit: Sorry about SO. This should work better
Edit2: I notice it's essentially the same answer as Abra, now.

Finding cause of jitter in collision detection

I'm looking for help in finding the source of jitter when using collision detection.
I've implemented a java game (using eclipse and slick2d) and have been loosely following this guide:
http://katyscode.wordpress.com/2013/01/18/2d-platform-games-collision-detection-for-dummies
but of course changing bits where necessary to suit my game and java not cpp.
From the research I have done I think the root cause of my jitter is coming from rounding errors.
Despite that being my main suspect, I still haven't been able to identify where it's occurring.
Sorry if the indentation isn't quite right there, had a little trouble using the code block recognition.
Basically I'm creating variables in the class.
In init() I set up most of the resources.
In render() all the drawing takes place. Note the graphics translation so the camera follows the player.
In update I'm of course updating the position of the player according to user input, gravity and friction.
I also call my collision detection from this method.
Collision detection is working on a penetration resolution method.
(Yes I know I'm exhaustively comparing with every single world object. I will be improving my efficiency with AABB's when I have sorted more fundamental problems out. Like jitter!)
My method first calculates how much the player expects to move in each axis, then for each world object it checks for intersection with the players bounding points (floating point values that represent coordinates around the player). It checks this in each direction and uses the result to determine in which axis the collision occurred, if any, so that the appropriate action can be taken.
Sorry it's a put load of code, but it's collision detection after all, which isn't a small thing.
Here is my Play class where all the updating goes on for the game:
package GameFiles;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import java.awt.Rectangle;
//import java.awt.geom.Rectangle2D;
public class Play extends BasicGameState{
static int tileSize = 32;
File levelMap;
SpriteSheet spriteSheet;
int[][] currentMap;
Image[] groundTiles = new Image[4];
//List<Rectangle2D> levelBounds = new ArrayList<Rectangle2D>();
List<Rectangle> levelBounds = new ArrayList<Rectangle>();
Player player;
float playerX, playerY;
int dir;
float acc, mov, friction, gravity;
float runSpeed;
float maxAcc;
boolean inAir, jumping, keyDown;
boolean exitFlag;
int mapHeight, mapWidth;
float mapX, mapY;
float speedX, speedY;
int worldObjectCount;
int iterations;
public Play(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
playerX = Game.scrWidth/2;
playerY = Game.scrHeight - tileSize - tileSize;
player = new Player(playerX, playerY);
levelMap = new File("maps/lvl1.txt");
spriteSheet = new SpriteSheet("res/tiles/tilesets/DungeonCrawlTilesetBW.png", tileSize, tileSize);
try
{
currentMap = readMap(levelMap);
}
catch (IOException e)
{
System.out.println("Array size mismatch when copying arrays.");
e.printStackTrace();
}
levelBounds.clear();
for(int x = 0; x < mapWidth; x++)
{
for(int y = 0; y < mapHeight; y++){
if(currentMap[x][y] == 1){
levelBounds.add(new Rectangle(x*tileSize, Game.scrHeight - mapHeight*tileSize + y*tileSize, tileSize, tileSize));
//levelBounds.add(new Rectangle2D.Float(x*tileSize, Game.scrHeight - mapHeight*tileSize + y*tileSize, tileSize, tileSize));
System.out.println("Added new bounding box: " + (x*tileSize) + ", " + (Game.scrHeight - mapHeight*tileSize + y*tileSize) + ", " + tileSize);
}
}
}
worldObjectCount = levelBounds.size();
System.out.println("World object count: " + worldObjectCount);
groundTiles[0] = spriteSheet.getSubImage(4, 16);
groundTiles[1] = spriteSheet.getSubImage(13, 19);
dir = 1;
acc = 0.0f;
mov = 0.0f;
friction = 4f;
gravity = 4f;
runSpeed = 0.6f;
maxAcc = -1f;
inAir = false;
jumping = false;
keyDown = false;
exitFlag = false;
speedX = 0.0f;
speedY = 0.0f;
iterations = 3;
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
//determine cameraX and cameraY
float cameraX, cameraY;
cameraX = player.getX() - Game.scrWidth/2;
cameraY = player.getY() - (Game.scrHeight/2 - tileSize - tileSize);
g.translate(-cameraX, -cameraY);
player.render(g);
for(int x = 0; x < mapWidth; x++)
{
for(int y = 0; y < mapHeight; y++){
if(currentMap[x][y] == 1){
groundTiles[0].draw(x*tileSize, Game.scrHeight - mapHeight*tileSize + y*tileSize);
}
}
}
g.translate(cameraX, cameraY);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input = gc.getInput();
float secondsElapsed = delta/1000.0f;
checkCollisions(secondsElapsed);
player.setX((player.getX() + speedX));
player.setY((player.getY() - speedY));
//check inputs
checkKeyEvents(input);
//slow down / friction
if(!keyDown){
if(mov < 0)
mov += friction * secondsElapsed;
else
mov -= friction * secondsElapsed;
}
speedX = mov;
if (speedX > 0 && speedX < friction * secondsElapsed) speedX = 0;
if (speedX < 0 && speedX > -friction * secondsElapsed) speedX = 0;
//jump or fall
acc -= gravity * secondsElapsed;
if (acc < maxAcc){
acc = maxAcc;
}
speedY = acc;
//exit when exitFlag true
if(exitFlag){
gc.exit();
}
}
public void checkCollisions(float secondsElapsed){
boolean contactX = true, contactYbottom = true, contactYtop = true;
// Keep iterating the contact solver until the maximum number of iterations is reached
// or no collisions are detected
for (int iteration = 0; iteration < iterations && (contactX || contactYbottom || contactYtop); iteration++)
{
float nextMoveX = speedX * secondsElapsed;
float nextMoveY = speedY * secondsElapsed;
contactX = contactYbottom = contactYtop = false;
float projectedMoveX, projectedMoveY, originalMoveX, originalMoveY;
originalMoveX = nextMoveX;
originalMoveY = nextMoveY;
for (int o = 0; o < worldObjectCount && !contactX && !contactYbottom && !contactYtop; o++)
{
for (int dir = 0; dir < 6; dir++)
{
//top, bottom, left, left, right, right.
if (dir == 0 && nextMoveY < 0) continue;
if (dir == 1 && nextMoveY > 0) continue;
if (dir == 2 && nextMoveX > 0) continue;
if (dir == 3 && nextMoveX > 0) continue;
if (dir == 4 && nextMoveX < 0) continue;
if (dir == 5 && nextMoveX < 0) continue;
projectedMoveX = (dir >= 2? nextMoveX : 0);
projectedMoveY = (dir < 2? nextMoveY : 0);
float[][] collisionPoint = player.getBounds();
Rectangle curRect = new Rectangle(levelBounds.get(o).x, levelBounds.get(o).y, levelBounds.get(o).width, levelBounds.get(o).height);
//Rectangle2D curRect = levelBounds.get(o).getBounds2D();
while (curRect.contains(collisionPoint[dir][0] + projectedMoveX, collisionPoint[dir][1] + projectedMoveY)
|| curRect.intersects(collisionPoint[dir][0] + projectedMoveX, collisionPoint[dir][1] + projectedMoveY, 1, 1))
{
if (dir == 0) projectedMoveY += 0.05f; //top collision
if (dir == 1) projectedMoveY -= 0.05f; //bottom collision
if (dir == 2) projectedMoveX += 0.05f; //left collision
if (dir == 3) projectedMoveX += 0.05f;
if (dir == 4) projectedMoveX -= 0.05f; //right collision
if (dir == 5) projectedMoveX -= 0.05f;
}
if (dir >= 2 && dir <= 5)
nextMoveX = projectedMoveX;
if (dir >= 0 && dir <= 1)
nextMoveY = projectedMoveY;
}
if (nextMoveY > originalMoveY && originalMoveY != 0)
{
contactYtop = true;
}
if (nextMoveY < originalMoveY && originalMoveY != 0)
{
contactYbottom = true;
}
if (Math.abs(nextMoveX - originalMoveX) > 0.01f)
{
contactX = true;
}
if (contactX && contactYtop && speedY > 0)
speedY = nextMoveY = 0;
}
if (contactYbottom || contactYtop)
{
player.setY(player.getY() + nextMoveY);
speedY = 0;
acc = 0;
if (contactYbottom)
jumping = false;
}
if (contactX)
{
player.setX(player.getX() + nextMoveX);
speedX = 0;
mov = 0;
}
}//end collisions
}
public int[][] readMap(File level) throws IOException, SlickException{
BufferedReader br = new BufferedReader(new FileReader(level));
mapWidth = Integer.parseInt(br.readLine());
mapHeight = Integer.parseInt(br.readLine());
int[][] map = new int[mapWidth][mapHeight];
for(int row = 0; row < mapHeight; row++)
{
String line = br.readLine();
if(line == null || line.isEmpty())
{
System.out.println("Line is empty or null");
}
else
{
String[] tileValues = line.split(",");
for(int col = 0; col < mapWidth; col++)
{
map[col][row] = Integer.parseInt(tileValues[col]);
}
}
}
br.close();
return map;
}
public void checkKeyEvents(Input input){
//key input events
if(input.isKeyPressed(Input.KEY_DOWN)){
}
if(input.isKeyPressed(Input.KEY_UP)){
if(!jumping){
acc = 1f;
}
jumping = true;
}
if(input.isKeyDown(Input.KEY_LEFT) && !input.isKeyDown(Input.KEY_RIGHT)){
keyDown = false;
mov -= 0.006f;
if (mov < -runSpeed){
mov = -runSpeed;
}
}
if(input.isKeyDown(Input.KEY_RIGHT) && !input.isKeyDown(Input.KEY_LEFT)){
keyDown = false;
mov += 0.006f;
if (mov > runSpeed){
mov = runSpeed;
}
}
if(input.isKeyPressed(Input.KEY_ESCAPE)){
exitFlag = true;
}
}
public int getID(){
return 1;
}
}
Since I can't predict what more info a potential helper might need, I'll leave it at that for now, but of course I can provide any more info when/where needed.
Thanks,
J.

How do I get this square to bounce off of the walls?

So I'm trying to get this square to bounce off of the wall. I'm fairly new to coding, but I can't understand why this is happening. It seems to be bouncing very badly, as in it completely reverses the direction in which it hits, so it does not bounce logically.
The most frustrating problem though, is that it only bounces once. It bounces once off of the side and then when it encounters a second wall, it just goes off into the abyss.
Here is a snippet of code used to write it:
void moveTheBox() {
while (inside == true) {
if ((bigBoxX <= 0) || (bigBoxY <= 0) ||
(bigBoxX >= 600 - bigBoxSize) ||
(bigBoxY >= 600 - bigBoxSize)) {
bigBoxDeltaX = bigBoxDeltaX * -1;
bigBoxDeltaY = bigBoxDeltaY * -1;
while ((bigBoxX >= 0) || (bigBoxY >= 0) ||
(bigBoxX <= 600 - bigBoxSize) ||
(bigBoxY <= 600 - bigBoxSize)) {
bigBoxX = bigBoxX + bigBoxDeltaX;
bigBoxY = bigBoxY + bigBoxDeltaY;
repaint();
pause();
}
} else {
bigBoxX = bigBoxX + bigBoxDeltaX;
bigBoxY = bigBoxY + bigBoxDeltaY;
repaint();
pause();
}
}
}
Edit: I figured out 4 minutes after you posted that. I fixed the awkward bouncing and the 1 bounce issue.
Here is the final product:
void moveTheBox() {
int i = 0;
while(i == 0){
if ((bigBoxX <= 0) || (bigBoxX >= 600-bigBoxSize)){
bigBoxDeltaX = bigBoxDeltaX * -1;
while((bigBoxX >= 0) || (bigBoxY >=0) || (bigBoxX <= 600-bigBoxSize) || (bigBoxY <= 600 - bigBoxSize)){
bigBoxX = bigBoxX + bigBoxDeltaX;
bigBoxY = bigBoxY + bigBoxDeltaY;
repaint();
pause();
break;
}
}else if ((bigBoxY <= 0) || (bigBoxY >= 600-bigBoxSize)){
bigBoxDeltaY = bigBoxDeltaY * -1;
while((bigBoxX >= 0) || (bigBoxY >=0) || (bigBoxX <= 600-bigBoxSize) || (bigBoxY <= 600 - bigBoxSize)){
bigBoxX = bigBoxX + bigBoxDeltaX;
bigBoxY = bigBoxY + bigBoxDeltaY;
repaint();
pause();
break;
}
}else{
bigBoxX = bigBoxX + bigBoxDeltaX;
bigBoxY = bigBoxY + bigBoxDeltaY;
repaint();
pause();
}
}
}
import java.awt.*;
import java.util.Formatter;
import javax.swing.*;
/**
* One ball bouncing inside a rectangular box.
* All codes in one file. Poor design!
*/
// Extends JPanel, so as to override the paintComponent() for custom rendering codes.
public class BouncingBallSimple extends JPanel {
// Container box's width and height
private static final int BOX_WIDTH = 640;
private static final int BOX_HEIGHT = 480;
// Ball's properties
private float ballRadius = 200; // Ball's radius
private float ballX = ballRadius + 50; // Ball's center (x, y)
private float ballY = ballRadius + 20;
private float ballSpeedX = 3; // Ball's speed for x and y
private float ballSpeedY = 2;
private static final int UPDATE_RATE = 30; // Number of refresh per second
/** Constructor to create the UI components and init game objects. */
public BouncingBallSimple() {
this.setPreferredSize(new Dimension(BOX_WIDTH, BOX_HEIGHT));
// Start the ball bouncing (in its own thread)
Thread gameThread = new Thread() {
public void run() {
while (true) { // Execute one update step
// Calculate the ball's new position
ballX += ballSpeedX;
ballY += ballSpeedY;
// Check if the ball moves over the bounds
// If so, adjust the position and speed.
if (ballX - ballRadius < 0) {
ballSpeedX = -ballSpeedX; // Reflect along normal
ballX = ballRadius; // Re-position the ball at the edge
} else if (ballX + ballRadius > BOX_WIDTH) {
ballSpeedX = -ballSpeedX;
ballX = BOX_WIDTH - ballRadius;
}
// May cross both x and y bounds
if (ballY - ballRadius < 0) {
ballSpeedY = -ballSpeedY;
ballY = ballRadius;
} else if (ballY + ballRadius > BOX_HEIGHT) {
ballSpeedY = -ballSpeedY;
ballY = BOX_HEIGHT - ballRadius;
}
// Refresh the display
repaint(); // Callback paintComponent()
// Delay for timing control and give other threads a chance
try {
Thread.sleep(1000 / UPDATE_RATE); // milliseconds
} catch (InterruptedException ex) { }
}
}
};
gameThread.start(); // Callback run()
}
/** Custom rendering codes for drawing the JPanel */
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g); // Paint background
// Draw the box
g.setColor(Color.BLACK);
g.fillRect(0, 0, BOX_WIDTH, BOX_HEIGHT);
// Draw the ball
g.setColor(Color.BLUE);
g.fillOval((int) (ballX - ballRadius), (int) (ballY - ballRadius),
(int)(2 * ballRadius), (int)(2 * ballRadius));
// Display the ball's information
g.setColor(Color.WHITE);
g.setFont(new Font("Courier New", Font.PLAIN, 12));
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
formatter.format("Ball #(%3.0f,%3.0f) Speed=(%2.0f,%2.0f)", ballX, ballY,
ballSpeedX, ballSpeedY);
g.drawString(sb.toString(), 20, 30);
}
/** main program (entry point) */
public static void main(String[] args) {
// Run GUI in the Event Dispatcher Thread (EDT) instead of main thread.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Set up main window (using Swing's Jframe)
JFrame frame = new JFrame("A Bouncing Ball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new BouncingBallSimple());
frame.pack();
frame.setVisible(true);
}
});
}
}
Refer the tutorial

Jumping algorithm in Java

I have made a kind of gravity in a 2D Java game and I am attempting to create a jumping algorithm. I have had a look around and cannot seen to find any help for an algorithm which moves up and down in a wave.
Here is the code in Game.java:
package game;
import java.awt.*;
public class Game extends GameLoop{
public void init(){
setSize(864,480);
Thread th = new Thread(this);
th.start();
offscreen = createImage(864,480);
d = offscreen.getGraphics();
addKeyListener(this);
}
public void paint(Graphics g){
d.clearRect(0, 0, 864, 480);
d.drawImage(background, 0, 0, this);
d.drawImage(person, x, y, this);
g.drawImage(offscreen, 0, 0, this);
}
public void update(Graphics g){
paint(g);
}
}
And here is the code in GameLoop.java:
package game;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;
public class GameLoop extends Applet implements Runnable, KeyListener{
public int x, y;
public Image offscreen;
public Graphics d;
public boolean up, down, left, right;
public BufferedImage background, w1, w2, w3, w4, w5, w6, w7, w8, person, s1, s2;
public int counter, counter2;
public void run(){
x = 100;
y = 100;
try {
background = ImageIO.read(new File("background.png"));
w1 = ImageIO.read(new File("walk1.png"));
w2 = ImageIO.read(new File("walk2.png"));
w3 = ImageIO.read(new File("walk3.png"));
w4 = ImageIO.read(new File("walk4.png"));
w5 = ImageIO.read(new File("walk5.png"));
w6 = ImageIO.read(new File("walk6.png"));
w7 = ImageIO.read(new File("walk7.png"));
w8 = ImageIO.read(new File("walk8.png"));
s1 = ImageIO.read(new File("stancel.png"));
s2 = ImageIO.read(new File("stancer.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
person = s1;
while(true){
x = 100;
y = 100;
while(true){
if (y <= 304 && up != true){
y+=10;
}
counter++;
counter2++;
if (counter >= 20){
counter = 0;
}
if (counter >= 0 && counter <= 5 && right == true){
person = w1;
}
if (counter > 5 && counter < 10 && right == true){
person = w2;
}
if (counter >= 10 && counter <= 15 && right == true){
person = w3;
}
if (counter > 15 && right == true){
person = w4;
}
if (counter2 >= 20){
counter2 = 0;
}
if (counter2 >= 0 && counter2 <= 5 && left == true){
person = w5;
}
if (counter2 > 5 && counter2 < 10 && left == true){
person = w6;
}
if (counter2 >= 10 && counter2 <= 15 && left == true){
person = w7;
}
if (counter2 > 15 && left == true){
person = w8;
}
if (left == true){
x-=4;
}
if (right == true){
x+=4;
}
if (up == true){
y-=4;
}
if (down == true){
y+=4;
}
if ( x <= -10 ){
x = -10;
}
if ( y <= 0 ){
y = 0;
}
if ( x >= 824 ){
x = 824;
}
if ( y >= 304 ){
y = 304;
}
repaint();
try{
Thread.sleep(20);
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
//#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == 37){
left = true;
}
if(e.getKeyCode() == 38){
up = true;
}
if(e.getKeyCode() == 39){
right = true;
}
if(e.getKeyCode() == 40){
down = true;
}
}
//#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == 37){
left = false;
person = s2;
}
if(e.getKeyCode() == 38){
up = false;
}
if(e.getKeyCode() == 39){
right = false;
person = s1;
}
if(e.getKeyCode() == 40){
down = false;
}
}
//#Override
public void keyTyped(KeyEvent e) {
}
}
I think I need a counter , counter3 as a double, but I am not sure of an algorithm that will go up to a certain point on the y axis, then move down. Any help is appreciated.
One of the simplest wave functions is our old friend sine:
So for a jump motion, you would probably like the first half of a sine period. You could use java.util.Math, passing a sequence of numbers from 0 to 180 (how many depends on the number of frames you want the jump to last). e.g.:
Math.sin(0.0); // = 0.0
Math.sin(30.0); // = 0.5
Math.sin(60.0); // = 0.9
Math.sin(90.0); // = 1.0
Math.sin(120.0); // = 0.9
Math.sin(150.0); // = 0.5
Math.sin(180.0); // = 0.0
I'm not entirely sure by what you mean by "moves up and down in a wave".
But anyway, in real world physics, the altitude of an object in free-fall (ignoring air resistance) follows a parabola; i.e. it obeys a formula like:
altitude = maxAltitude - (acceleration * (t - timeOfMaxAltitude))^2
(There are other ways of formulating the laws of motion, but this approach is probably what you need for a simple game.)
A 1/2 sine wave looks rather like a truncated parabola, and could be used instead in a game. However, you need to realize that it is not an accurate simulation of real-world physics, especially for high jumps.
You can use a variable to represent the vertical velocity. When you jump, set the vertical velocity to a negative value. On each frame, add the vertical velocity to the vertical position, and add a constant to the velocity to simulate gravity. When there is something underneath, set the velocity back to 0. Experiment with the constant values to get an appropriate jump height.

memory game random color

Hey everybody I'm creating a memory game that uses a 4x4 2Darray.
I have the 4x4 filld with a pair of integers from 0-7 and they are randomly scattered. I want to assign a color to each pair, so when the mouse clicks over that square the assigned color will appear based on the integer and you will have to find the other integer based on it's corresponding matching color.
I've been running into some problems with this setColor method of mine. I'm going to include my whole code in case I messed up somewhere else and that was why. At the moment if I click every square once I use all 8 colors I assigned twice, but some of the colors don't match up to where the same integer is at on another tile. Also when I click the same tile multiple times it changes between 2-3 colors and I'm not sure what I'm doing wrong.
The parts I need advice on are the setColor method I have assigned and my logic behind it.
/*Sets the background of your memory board to black*/
public void init()
{
setSize(400,400);
setBackground(Color.BLACK);
buildBoard(4);
}
/*This is main in java applets
You may need to add (not
change) a couple things in this method
*/
public void paint(Graphics canvas)
{
if(firstRun) //for the first run we need to build our random board
{
print2DArray(board);
buildBoard(4);
firstRun = false;
}
else // once our board is built we will display the game
{
displayGame(canvas);
if (mouseClicked) // if the mouse has been clicked
{
displayHit(canvas);//find which box the user clicked
mouseClicked = false;
}
}
}
/*
DO NOT change this method
determines if the mouse has been pressed
sets x and y Mouse to the location of the mouse arrow
redraws the image
*/
public boolean mouseDown(Event e, int x, int y )
{
mouseClicked = true;
xMouse = x;
yMouse = y;
repaint();
return true;
}
/*DO NOT change this method
redraws the scene
*/
public void update ( Graphics g )
{
paint(g);
}
/*
pre: none
post: build an array that holds the memory values for a board of size x size
the board will hold two of each int from 0 to size randomly placed in the array
*/
public static void fillRect(Graphics g, int x, int y, int width, int length)
{
g.setColor(Color.BLACK);
width = 100;
length = 100;
x = (int)xMouse/100;
y = (int)yMouse/100;
}
public void buildBoard(int s)
{
int a = 4;
for (int row = 0; row < a; row++)
for (int column = 0; column < a; column++)
{
board[row][column] = count++ % 8;
}
for(int row = 0; row < 4; row++)
for(int column = 0; column < 4; column ++)
{
int x = (int)Math.floor(Math.random()*4);
int y = (int)Math.floor(Math.random()*4);
temp = board[row][column];
board[row][column] = board[x][y];
board[x][y] = temp;
}
}
public static void print2DArray(int[][] arr)
{
for (int row = 0; row < arr.length; row++)
{
for (int col = 0; col < arr[row].length; col++)
{
System.out.print(arr[row][col] + " ");
}
System.out.println();
}
}
public void displayGame(Graphics canvas)
{
canvas.setColor(Color.WHITE);
for(int i =0; i < 400; i+= WIDTH)
for(int j = 0; j < 400; j+= WIDTH)
canvas.drawRect(i, j, WIDTH, WIDTH);
}
/*
Pre: xMouse and yMouse have been initialized
Post: A circle is displayed in the correct box on the screen
Currently the circle is displayed at the mouse location
*/
public void displayHit(Graphics g)
{
/*int xGuess = (int)xMouse/100;
int yGuess = (int)yMouse/100;
board[xGuess][yGuess] = guess1;
int xGuess2 = (int)xMouse/100;
int yGuess2 = (int)yMouse/100;
board[xGuess2][yGuess2] = guess2;
if (guess1 == guess2)
{
setColor(g);
centerHit(xMouse, yMouse);
g.fillOval(xMouse, yMouse, 40, 40);
}
else
g.fillRect(guess1, guess2, width, length);
g.setColor(Color.BLACK);*/
setColor(g);
centerHit(xMouse, yMouse);
g.fillOval(xMouse, yMouse, 40, 40);
}
public void setColor(Graphics g)
{
centerClick(x1,y1);
//int x = xMouse;
//int y = yMouse;
colorIndex = board[row][column];
board[row][column] = board[x1][y1];
board[x1][y1] = colorIndex;
switch(colorIndex)
{
case 0: g.setColor(Color.RED);
break;
case 1: g.setColor(Color.GREEN);
break;
case 2: g.setColor(Color.BLUE);
break;
case 3: g.setColor(Color.ORANGE);
break;
case 4: g.setColor(Color.CYAN);
break;
case 5: g.setColor(Color.MAGENTA);
break;
case 6: g.setColor(Color.PINK);
break;
case 7: g.setColor(Color.YELLOW);
break;
}
}
public void centerHit(int centerX, int centerY)
{
{
if ((xMouse > 0) && (xMouse <=100))
xMouse = 33;
else if ((xMouse > 100) && (xMouse <=200))
xMouse = 133;
else if ((xMouse > 200) && (xMouse <=300))
xMouse = 233;
else if ((xMouse > 300) && (xMouse <=400))
xMouse = 333;
}
{
if ((yMouse > 0) && (yMouse <=100))
yMouse = 33;
else if ((yMouse > 100) && (yMouse <=200))
yMouse = 133;
else if ((yMouse > 200) && (yMouse <=300))
yMouse = 233;
else if ((yMouse > 300) && (yMouse <=400))
yMouse = 333;
}
}
public void centerClick(int centerX, int centerY)
{
{
if ((xMouse > 0) && (xMouse <=100))
x1 = 0;
else if ((xMouse > 100) && (xMouse <=200))
x1 = 1;
else if ((xMouse > 200) && (xMouse <=300))
x1 = 2;
else if ((xMouse > 300) && (xMouse <=400))
x1 = 3;
}
{
if ((yMouse > 0) && (yMouse <=100))
y1 = 0;
else if ((yMouse > 100) && (yMouse <=200))
y1 = 1;
else if ((yMouse > 200) && (yMouse <=300))
y1 = 2;
else if ((yMouse > 300) && (yMouse <=400))
y1 = 3;
}
}
}
to keep the colors from changing, change this code
colorIndex = board[row][column];
board[row][column] = board[x1][y1];
board[x1][y1] = colorIndex;
to
colorIndex = board[x1][y1];
the color matching issue is because youre building the board twice: first on the firstRun of the paint method where you print the array and then on the initmethod where the board gets overwritten so you see different values
to solve it you should build the board only once in the init method, and call print2DArray afterwards to check it, so
buildBoard(4);
print2DArray(board); //<-- add this line to verify colors
and you could omit the firstRun flag and all associated code.
comment or delete this code:
/*if(firstRun) //for the first run we need to build our random board
{
print2DArray(board);
buildBoard(4);
firstRun = false;
}
else // once our board is built we will display the game
{*/

Categories

Resources