Welcome, Guest. Please login or register.

Author Topic: [Java] Snake Game  (Read 2401 times)

0 Members and 1 Guest are viewing this topic.

Offline Blackllama

[Java] Snake Game
« on: March 07, 2013, 10:21:53 PM »
So I made a little snake game forever ago. Felt like posting this just because this board gets so few posts.

Use wasd to move.

Here's the jar:
http://www.mediafire.com/?fknij99oaz234vw

Here's the code:
Component.java
Code: [Select]
import java.applet.Applet;
import java.awt.*;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.Timer;

public class Component extends Applet implements Runnable{

static boolean isRunning = false;

public static Dimension size  = new Dimension(Game.BOARD_SIZE.width*Game.DOT_SIZE,Game.BOARD_SIZE.height*Game.DOT_SIZE);
public static Dimension realSize = new Dimension(0,0);

public static JFrame frame;
private static Image screen;

public static final Color BACKGROUND = new Color(153,231,255);

public static Game game;
public static Timer timer;
public static KeyListener listener = new Listening();


public Component(){
setPreferredSize(size);
addKeyListener(listener);
}


public static void main(String[] args){
Component component = new Component();

frame = new JFrame();
frame.add(component);
frame.pack();

realSize = new Dimension(frame.getWidth(), frame.getHeight());

frame.setTitle("Sexy Snake");
frame.setPreferredSize(size);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
component.start();

}

public void start(){
requestFocus();

game = new Game();


new Thread(this).start();
isRunning = true;
}

public void stop(){
isRunning = false;
}

public void tick(){
if(frame.getWidth() != realSize.width || frame.getHeight() != realSize.height){
frame.pack();
}
game.tick();
}

public void render(){
Graphics g = screen.getGraphics();
g.setColor(BACKGROUND);
g.fillRect(0, 0, size.width, size.height);
setBackground(BACKGROUND);

game.render(g);
g = getGraphics();
g.drawImage(screen, 0, 0, size.width, size.height, 0, 0, size.width, size.height, null);
g.dispose();
}

public void run(){
screen = createVolatileImage(size.width,size.height);
while(isRunning){
tick();
render();
try{
Thread.sleep(110);
}catch(Exception e){
System.out.println(e);
}
}
}
}
Game.java
Code: [Select]
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;

public class Game{

public static final Dimension BOARD_SIZE = new Dimension(25, 25);
public static final int DOT_SIZE = 10;
public static final int TOTAL_DOTS = BOARD_SIZE.width * BOARD_SIZE.height;

public static final int RIGHT = 0;
public static final int UP = 1;
public static final int LEFT = 2;
public static final int DOWN = 3;

public static Random r = new Random();

public static final Color HEAD_COLOR = new Color(0, 0, 0);
public static final Color SEGMENT_COLOR = new Color(255, 255, 255);
public static final Color FOOD_COLOR = new Color(100, 100, 100);

public static int dir = UP;
public static int prevDir;

public int[] xSegmentCords = new int[TOTAL_DOTS];
public int[] ySegmentCords = new int[TOTAL_DOTS];
public static int snakeSize = 5;
public Point foodCords = new Point(0, 0);

public Game(){
xSegmentCords[0] = (int) Math.round(BOARD_SIZE.width*.75);
ySegmentCords[0] = (int) Math.round(BOARD_SIZE.height*.75);
spawnFood();
}

public void spawnFood(){
boolean xSame = false;
boolean ySame = false;
int x = r.nextInt(BOARD_SIZE.width);
int y = r.nextInt(BOARD_SIZE.height);

for(int xCord: xSegmentCords){
if(xCord == x){
xSame = true;
}
}
for(int yCord: ySegmentCords){
if(yCord == y){
ySame = true;
}
}
if(!(ySame && xSame)){
foodCords.x = x;
foodCords.y = y;
}else{
spawnFood();
}
}

public void gameOver(){
Component.isRunning = false;
}

public boolean isCollidingWithFruit(Point p1, Point p2){
if(p1.x == p2.x && p1.y == p2.y){
return true;
}else{
return false;
}
}

public void isCollidingWithTail(){

for(int i = snakeSize; i > 0; i--){

if((xSegmentCords[0] == xSegmentCords[i]) && (ySegmentCords[0] == ySegmentCords[i])){
gameOver();
}
}

switch(dir){
case RIGHT:
if(!(xSegmentCords[0] + 1 < BOARD_SIZE.width)){
gameOver();
}
break;
case UP:
if(!(ySegmentCords[0] - 1 < BOARD_SIZE.height)){
gameOver();
}
break;
case LEFT:
if(!(xSegmentCords[0] - 1 < BOARD_SIZE.width)){
gameOver();
}
break;
case DOWN:
if(!(ySegmentCords[0] + 1 < BOARD_SIZE.height)){
gameOver();
}
break;
}
}

public void tick(){
isCollidingWithTail();
// moves segments
for(int i = snakeSize; i > 0; i--){
xSegmentCords[i] = xSegmentCords[(i - 1)];
ySegmentCords[i] = ySegmentCords[(i - 1)];
}
// moves head
switch(dir){
case RIGHT:
if(xSegmentCords[0] + 1 < BOARD_SIZE.width){
for(int i = 1; i < snakeSize; i++){
if(isCollidingWithFruit(new Point(xSegmentCords[0] + 1, ySegmentCords[0]), new Point(xSegmentCords[i], ySegmentCords[i]))){
gameOver();
break;
}
}
xSegmentCords[0]++;
}else{
gameOver();
}
break;
case UP:
if(ySegmentCords[0] - 1 >= 0){
for(int i = 1; i < snakeSize; i++){
if(isCollidingWithFruit(new Point(xSegmentCords[0], ySegmentCords[0] - 1), new Point(xSegmentCords[i], ySegmentCords[i]))){
gameOver();
break;
}
}
ySegmentCords[0]--;
}else{
gameOver();
}
break;
case LEFT:
if(xSegmentCords[0] - 1 >= 0){
for(int i = 1; i < snakeSize; i++){
if(isCollidingWithFruit(new Point(xSegmentCords[0] - 1, ySegmentCords[0]), new Point(xSegmentCords[i], ySegmentCords[i]))){
gameOver();
break;
}
}
xSegmentCords[0]--;
}else{
gameOver();
}
break;
case DOWN:
if(ySegmentCords[0] + 1 < BOARD_SIZE.height){
for(int i = 1; i < snakeSize; i++){
if(isCollidingWithFruit(new Point(xSegmentCords[0], ySegmentCords[0] + 1), new Point(xSegmentCords[i], ySegmentCords[i]))){
gameOver();
break;
}
}
ySegmentCords[0]++;
}else{
gameOver();
}
break;
}
if(isCollidingWithFruit(new Point(xSegmentCords[0], ySegmentCords[0]), foodCords)){
spawnFood();
snakeSize++;
}
}

public void render(Graphics g){
// head
g.setColor(HEAD_COLOR);
g.fillRect(xSegmentCords[0] * DOT_SIZE, ySegmentCords[0] * DOT_SIZE, DOT_SIZE, DOT_SIZE);
// segments
g.setColor(SEGMENT_COLOR);
for(int i = 1; i < snakeSize; i++){
g.fillRect(xSegmentCords[i] * DOT_SIZE, ySegmentCords[i] * DOT_SIZE, DOT_SIZE, DOT_SIZE);
}
// food
g.setColor(FOOD_COLOR);
g.fillRect(foodCords.x * DOT_SIZE, foodCords.y * DOT_SIZE, DOT_SIZE, DOT_SIZE);

}
}
Listening.java
Code: [Select]
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Listening implements KeyListener{

public static final int RIGHT = 0;
public static final int UP = 1;
public static final int LEFT = 2;
public static final int DOWN = 3;

public void keyPressed(KeyEvent e){
int key = e.getKeyCode();

switch(key){
case KeyEvent.VK_W:
Game.prevDir = Game.dir;
Game.dir = UP;
break;
case KeyEvent.VK_A:
Game.prevDir = Game.dir;
Game.dir = LEFT;
break;
case KeyEvent.VK_S:
Game.prevDir = Game.dir;
Game.dir = DOWN;
break;
case KeyEvent.VK_D:
Game.prevDir = Game.dir;
Game.dir = RIGHT;
break;
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}

Conjoint Gaming [Game On]

[Java] Snake Game
« on: March 07, 2013, 10:21:53 PM »

 


* ShoutBox!

Refresh History
  • Careful what you post. Forum rules still apply in the shoutbox!
  • Pyro: happy birthday Tyber
    July 14, 2024, 01:55:16 PM
  • Finniespin: More of a Patrick Hernandez guy - Born to be Alive
    June 16, 2024, 09:14:27 AM
  • Sly: Aaron Hernandez was a legend of a man
    June 14, 2024, 01:46:02 AM
  • Finniespin: how? what?
    June 01, 2024, 06:28:43 PM
  • Inject OH 4: Lost my discord rip
    May 30, 2024, 12:14:57 AM
  • Finniespin: yoooooooo!
    April 03, 2024, 05:32:48 PM
  • Coreybush11: bump
    April 01, 2024, 10:59:48 PM
  • Finniespin: wut
    March 28, 2024, 05:01:26 PM
  • Inject OH 4: And yes thank you very much Finnie! You are a champion xP
    March 22, 2024, 05:59:12 PM
  • Inject OH 4: Hey abrys agreed we should.
    March 22, 2024, 05:59:02 PM
  • Finniespin: Totally not a spam-bot, also totally not a limited time offer
    March 03, 2024, 05:25:06 PM
  • Finniespin: Anyone looking for a datacenter technician job? We got postings available around US and Europe (EMEA)
    February 17, 2024, 07:23:12 AM
  • Finniespin: https://www.google.com/about/careers/applications/jobs/results/118336179041903302-data-center-technician-global-server-operations
    February 17, 2024, 07:20:25 AM
  • Finniespin: Gave Inject 50 euros to pay for fees ^^
    February 17, 2024, 07:17:42 AM
  • xXArbysOvenMittXx: we gotta get the crew together again one day and hang, add me on discord: ogarbies
    February 12, 2024, 06:52:07 PM
  • xXArbysOvenMittXx: yo who is still paying for this lol
    February 12, 2024, 06:19:47 PM
  • Klondor: wow CG still lives, mind blown
    February 09, 2024, 05:13:53 AM
  • Finniespin: The website is back online!!!
    February 06, 2024, 03:17:05 PM
  • Inject OH 4: xD
    January 14, 2024, 05:55:44 PM
  • Finniespin: Get a load of this guy
    December 16, 2023, 09:16:03 AM
  • Inject OH 4: Sure
    December 09, 2023, 07:22:27 PM
  • HailToTheKing: cum shot . and cum shot. an d cum shot
    December 01, 2023, 01:54:12 PM
  • Finniespin: ffuck
    November 24, 2023, 03:43:47 PM
  • Inject OH 4: ee
    October 12, 2023, 01:13:56 AM
  • Sly: dd
    August 09, 2023, 03:13:48 AM
  • Inject OH 4: cc
    August 03, 2023, 09:51:36 PM
  • Finniespin: bb
    August 02, 2023, 06:12:50 PM
  • Inject OH 4: aa
    July 04, 2023, 10:29:35 PM
  • Shikaru: Been a very long time
    February 20, 2023, 05:42:04 PM
  • Shikaru: Oh wow my account is STILL active :)
    February 20, 2023, 05:42:00 PM
  • Napoleon BonaPARTY: yooo we got a lovense sponsor???
    December 16, 2022, 03:45:43 PM
  • Napoleon BonaPARTY: holy shit
    December 16, 2022, 03:45:24 PM
  • Napoleon BonaPARTY: oh wow its still here
    December 16, 2022, 03:45:21 PM
  • HailToTheKing: legends never die
    October 16, 2022, 01:28:09 PM
  • Live Bait: Oh wow. Still remember my old password.
    September 23, 2022, 08:37:38 AM
  • Mr_Rainbow: Still alive. Hope all is well with everyone this Christmas
    December 09, 2021, 03:31:04 AM
  • SlyWilliam: Much love, from our MC server to our ZPS server <3
    December 07, 2021, 10:23:37 PM
  • SlyWilliam: For the record, I don't REALLY remember all of you, but goddamn do I MISS all of you <3
    December 07, 2021, 10:23:10 PM
  • Pyro: parrot
    September 07, 2021, 05:23:18 AM
  • Inject OH 4: Do you mean for people that haven't come on in a long time and have to reagree?
    August 02, 2021, 11:39:42 PM

SimplePortal 2.3.5 © 2008-2012, SimplePortal