0 Members and 1 Guest are viewing this topic.
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); } } }}
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); }}
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){}}