Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------------------------------
- post 1
- -----------------------------------------------------------------
- package newpackage.graphics;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import newpackage.entities.Apple;
- import newpackage.entities.BodyPart;
- import newpackage.entities.Direction;
- import newpackage.entities.Snake;
- public class Screen extends JPanel implements Runnable {
- public static final int WIDTH=800,HEIGHT=600;
- private Thread thread;
- private boolean running=false;
- private BodyPart b;
- private ArrayList<BodyPart>snake;
- private Apple apple;
- private ArrayList<Apple> apples;
- private Random r;
- private int xCoor=10;
- private int yCoor=10;
- private int size=5;
- private boolean right =true,left=false,up=false,down=false;
- private int ticks=0;
- private Key key;
- //private Direction key1;
- public Screen() {
- setFocusable(true);
- key = new Key();
- //key1 = new Direction();
- addKeyListener(key);
- //addKeyListener(key1.getKey());
- setPreferredSize(new Dimension(WIDTH,HEIGHT));
- r=new Random();
- //Snake s=new Snake();
- //s.
- start();
- thread = new Thread(this, "game loop");
- thread.start();
- }
- public void tick() {
- if(snake.size()==0) {
- b= new BodyPart(xCoor, yCoor, 10);
- snake.add(b);
- }
- if(apples.size()==0) {
- //79x79
- int xCoor= r.nextInt(60);
- int yCoor= r.nextInt(60);
- apple=new Apple(xCoor, yCoor, 10);
- apples.add(apple);
- }
- for(int i=0;i<apples.size();i++) {
- //kada x i y koord zmije dotaknu poziciju jabuke, velicina se povecava a jabuka brise -don't read this
- if(xCoor == apples.get(i).getxCoor() && yCoor == apples.get(i).getyCoor()) {
- size++;
- apples.remove(i);
- i--;
- }
- }
- //max size=15-don't read this
- for(int i=0;i<snake.size();i++) {
- if(xCoor==snake.get(i).getxCoor() && yCoor==snake.get(i).getyCoor()) {
- if(i != snake.size()-1) {
- stop();
- }
- }
- }
- if(xCoor < 0 || xCoor > 79 || yCoor < 0 || yCoor > 60) {
- stop();
- }
- ticks++;
- //brzina kretanja zmije-don't read this
- //prvobitno je bila postavljena na 250000-don't read this
- if(ticks>690000) {
- //pravci u kojima se krece zmija,x-osa (+ i -) i y-psa (+ i -)-don't read this
- if(right) xCoor++;
- if(left) xCoor--;
- if(up) yCoor--;
- if(down) yCoor++;
- ticks=0;
- b=new BodyPart(xCoor, yCoor, 10);
- snake.add(b);
- if(snake.size()>size) {
- snake.remove(0);
- }
- }
- }
- public void paint(Graphics g) {
- g.clearRect(0, 0, WIDTH, HEIGHT);
- g.setColor(Color.lightGray);
- //DrawLine(int x1, int y1, int x2, int y2) -don't read this
- //vraca DrawLine za liniju koja pocinje u (x1, y1) i zavrsava se u (x2, y2).-don't read this
- for(int i=0;i< WIDTH /10;i++) {
- g.drawLine(i*10, 0, i*10, HEIGHT);
- }
- for(int i=0;i< HEIGHT /10;i++) {
- g.drawLine(0,i*10,WIDTH, i*10);
- }
- for(int i=0;i< snake.size();i++) {
- snake.get(i).draw(g);
- }
- for(int i=0;i<apples.size();i++) {
- apples.get(i).draw(g);
- }
- }
- public void start() {
- xCoor=10;
- yCoor=10;
- size=5;
- right =true;
- left=false;
- up=false;
- down=false;
- ticks=0;
- snake=new ArrayList<BodyPart>();
- apples=new ArrayList<Apple>();
- running = true;
- }
- public void stop() {
- int i = JOptionPane.showOptionDialog(this, "Do you want to play again?", "GAME OVER", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null);
- if (i == JOptionPane.YES_OPTION) {
- //Snake s=new Snake();
- //s.
- start();
- } else {
- System.exit(0);
- }
- }
- public void run() {
- //dok je pokrenut program, koristi tick metodu i repaint-don't read this
- while (running) {
- //Snake s=new Snake();
- //s.
- tick();
- repaint();
- }
- }
- private class Key implements KeyListener {
- //proverava koje je dugme pritisnuto na tastaturi i to dugme koristi za pravac
- public void keyPressed(KeyEvent e) {
- int key=e.getKeyCode();
- if(key==KeyEvent.VK_RIGHT && !left) {
- up=false;
- down=false;
- right=true;
- }
- if(key==KeyEvent.VK_LEFT && !right) {
- up=false;
- down=false;
- left=true;
- }
- if(key==KeyEvent.VK_UP && !down) {
- left=false;
- right=false;
- up=true;
- }
- if(key==KeyEvent.VK_DOWN && !up) {
- left=false;
- right=false;
- down=true;
- }
- }
- public void keyReleased(KeyEvent e) {
- //To change body of generated methods, choose Tools | Templates.
- }
- public void keyTyped(KeyEvent e) {
- //To change body of generated methods, choose Tools | Templates.
- }
- }
- }
- ------------------------------------------------------
- post 2
- ------------------------------------------------------
- package newpackage.entities;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- public class Direction implements KeyListener{
- private boolean right =true,left=false,up=false,down=false;
- public Direction key;
- public Direction getKey() {
- return key;
- }
- public void setKey(Direction key) {
- this.key = key;
- }
- public void keyPressed(KeyEvent e) {
- int key=e.getKeyCode();
- if(key==KeyEvent.VK_RIGHT && !left) {
- up=false;
- down=false;
- right=true;
- }
- if(key==KeyEvent.VK_LEFT && !right) {
- up=false;
- down=false;
- left=true;
- }
- if(key==KeyEvent.VK_UP && !down) {
- left=false;
- right=false;
- up=true;
- }
- if(key==KeyEvent.VK_DOWN && !up) {
- left=false;
- right=false;
- down=true;
- }
- }
- public void keyReleased(KeyEvent e) {
- //To change body of generated methods, choose Tools | Templates.
- }
- public void keyTyped(KeyEvent e) {
- //To change body of generated methods, choose Tools | Templates.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment