java - How do I add a score counter to this snake game? -


i want able display score players, , have score go 10 every time apple. though, need add space on window allow room score counter, can't find in code.

import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.util.arraylist; import java.util.random; import javax.swing.jpanel;  public class screen extends jpanel implements runnable {      private static final long serialversionuid = 1l;      public static final int width = 800, height = 900;     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 = 20, ycoor = 20;     private int size = 10;      private boolean right = true, left = false, = false, down = false;      private int ticks = 0;      private key key;      public screen() {         setfocusable(true);         key = new key();         addkeylistener(key);         setpreferredsize(new dimension(width, height));          r = new random();          snake = new arraylist<bodypart>();         apples = new arraylist<apple>();          start();     }      public void reset() {         snake.clear();         apples.clear();         xcoor = 20;         ycoor = 20;         size = 10;         running = true;     }      public void tick() {         if(snake.size() == 0) {             b = new bodypart(xcoor, ycoor, 20);             snake.add(b);         }          if(apples.size() == 0) {             int xcoor = r.nextint(40);             int ycoor = r.nextint(40);              apple = new apple(xcoor, ycoor, 20);             apples.add(apple);         }          for(int = 0; < apples.size(); i++) {             if(xcoor == apples.get(i).getxcoor() && ycoor ==         apples.get(i).getycoor()) {                 size++;                 apples.remove(i);                 i--;             }         }          for(int = 0; < snake.size(); i++) {             if(xcoor == snake.get(i).getxcoor() && ycoor ==  snake.get(i).getycoor()) {                 if(i != snake.size() - 1) {                     reset();                 }             }         }          if(xcoor < 0) xcoor = 40;         if(xcoor > 40) xcoor = 0;         if(ycoor < 0) ycoor = 40;         if(ycoor > 40) ycoor = 0;          ticks++;          if(ticks > 250000) {             if(right) xcoor++;             if(left) xcoor--;             if(up) ycoor--;             if(down) ycoor++;              ticks = 185000;              b = new bodypart(xcoor, ycoor, 20);             snake.add(b);              if(snake.size() > size) {                 snake.remove(0);             }         }     }      public void paint(graphics g) {         g.clearrect(0, 0, width, height);          g.setcolor(new color(20, 50, 0));         g.fillrect(0, 0, width, height);          g.setcolor(color.black);         for(int = 0; < width / 20; i++) {             g.drawline(i * 20, 0, * 20, height);         }          for(int = 0; < height / 20; i++) {             g.drawline(0, * 20, width, * 20);         }          for(int = 0; < snake.size(); i++) {             snake.get(i).draw(g);         }         for(int = 0; < apples.size(); i++) {             apples.get(i).draw(g);         }      }      public void start() {         running = true;         thread = new thread(this, "game loop");         thread.start();     }      public void stop() {         running = false;         try {             thread.join();         } catch (interruptedexception e) {             e.printstacktrace();         }     }      public void run() {         while(running) {             tick();             repaint();         }     }      private class key implements keylistener {          public void keypressed(keyevent e) {             int key = e.getkeycode();              if(key == keyevent.vk_right && !left) {                  = false;                 down = false;                 right = true;             }              if(key == keyevent.vk_left && !right) {                  = false;                 down = false;                 left = true;             }              if(key == keyevent.vk_up && !down) {                 left = false;                 right = false;                 = true;             }              if(key == keyevent.vk_down && !up) {                 left = false;                 right = false;                 down = true;             }          }          @override         public void keyreleased(keyevent arg0) {             // todo auto-generated method stub          }          @override         public void keytyped(keyevent arg0) {             // todo auto-generated method stub          }          }          public void keyreleased(keyevent arg0) {             // todo auto-generated method stub          }          public void keytyped(keyevent arg0) {             // todo auto-generated method stub          }      } 

add label on jpanel or add string inside paint() method

g.drawstring("score: " + intscore, 10, 10); 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -