Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java - gioco snake: il serpente non muore!!
Forum - Java - gioco snake: il serpente non muore!!

Pagine: [ 1 2 ] Precedente | Prossimo
Avatar
beppe_ita (Normal User)
Newbie


Messaggi: 6
Iscritto: 22/10/2012

Segnala al moderatore
Postato alle 23:14
Lunedì, 22/10/2012
Salve a tutti sono nuovo!! siccome necessito del gioco Snake per un mio progetto ho cercato in internet del codice già scritto e alla fine ne ho trovato uno che più o meno capisco completamente!! l'unico problema è che non si riesce a far terminare la partita perchè il serpente quando sbatte contro la propria coda, invece di morire, la attraversa!! quindi in pratica il gioco è infinito! ho provato a sistemarlo da solo ma davvero non capisco cosa ci sia di sbagliato e per questo spero che qualcuno di voi mi possa aiutare! questo è il codice:
Codice sorgente - presumibilmente Java

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5.  
  6.  class Snake extends JFrame implements KeyListener, Runnable {
  7.  
  8.     /**
  9.          *
  10.          */
  11.         private static final long serialVersionUID = 1L;
  12.         JPanel p1, p2;
  13.     JButton[] lb = new JButton[200];
  14.     JButton bonusfood;
  15.     JTextArea t;
  16.     int x = 500, y = 250, gu = 2, directionx = 1, directiony = 0, speed = 50, difference = 0, oldx, oldy, score = 0;
  17.     int[] lbx = new int[300];
  18.     int[] lby = new int[300];
  19.     Point[] lbp = new Point[300];
  20.     Point bfp = new Point();
  21.     Thread myt;
  22.     boolean food = false, runl = false, runr = true, runu = true, rund = true, bonusflag = true;
  23.     Random r = new Random();
  24.     JMenuBar mymbar;
  25.     JMenu game, help, level;
  26.  
  27.     public void initializeValues() {
  28.         gu = 3;
  29.         lbx[0] = 100;
  30.         lby[0] = 150;
  31.         directionx = 10;
  32.         directiony = 0;
  33.         difference = 0;
  34.         score = 0;
  35.         food = false;
  36.         runl = false;
  37.         runr = true;
  38.         runu = true;
  39.         rund = true;
  40.         bonusflag = true;
  41.     }
  42.  
  43.     Snake() {
  44.         super("Snake");
  45.         setSize(500, 330);
  46.         //Create Menue bar with functions
  47.         creatbar();
  48.         //initialize all variables
  49.         initializeValues();
  50.         // Start of UI design
  51.         p1 = new JPanel();
  52.         p2 = new JPanel();
  53.         // t will view the score
  54.         t = new JTextArea("Score ==>" + score);
  55.         t.setEnabled(false);
  56.         t.setBackground(Color.BLACK);
  57.         // snake have to eat bonousfood to growup
  58.         bonusfood = new JButton();
  59.         bonusfood.setEnabled(false);
  60.         // will make first snake
  61.         createFirstSnake();
  62.  
  63.         p1.setLayout(null);
  64.         p2.setLayout(new GridLayout(0, 1));
  65.         p1.setBounds(0, 0, x, y);
  66.         p1.setBackground(Color.blue);
  67.         p2.setBounds(0, y, x, 30);
  68.         p2.setBackground(Color.RED);
  69.  
  70.         p2.add(t); // will contain score board
  71.         // end of UI design
  72.         getContentPane().setLayout(null);
  73.         getContentPane().add(p1);
  74.         getContentPane().add(p2);
  75.  
  76.         setVisible(true);
  77.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  78.  
  79.         addKeyListener(this);
  80.         // start thread
  81.         myt = new Thread(this);
  82.         myt.start(); // go to run() method
  83.     }
  84.  
  85.     public void createFirstSnake() {
  86.         // Initially the snake has small length 3
  87.         for (int i = 0; i < 3; i++) {
  88.             lb[i] = new JButton("lb" + i);
  89.             lb[i].setEnabled(false);
  90.             p1.add(lb[i]);
  91.             lb[i].setBounds(lbx[i], lby[i], 10, 10);
  92.             lbx[i + 1] = lbx[i] - 10;
  93.             lby[i + 1] = lby[i];
  94.         }
  95.     }
  96.  
  97.     public void creatbar() {
  98.         mymbar = new JMenuBar();
  99.  
  100.         game = new JMenu("Game");
  101.  
  102.         JMenuItem newgame = new JMenuItem("New Game");
  103.         JMenuItem exit = new JMenuItem("Exit");
  104.  
  105.         newgame.addActionListener(
  106.                 new ActionListener() {
  107.  
  108.                     public void actionPerformed(ActionEvent e) {
  109.                         reset();
  110.                     }
  111.                 });
  112.  
  113.         exit.addActionListener(new ActionListener() {
  114.  
  115.             public void actionPerformed(ActionEvent e) {
  116.                 System.exit(0);
  117.             }
  118.         });
  119.  
  120.         game.add(newgame);
  121.         game.addSeparator();
  122.         game.add(exit);
  123.  
  124.         mymbar.add(game);
  125.  
  126.         level = new JMenu("Level");
  127.  
  128.         mymbar.add(level);
  129.  
  130.         help = new JMenu("Help");
  131.  
  132.         JMenuItem creator = new JMenuItem("Creator");
  133.         JMenuItem instruction = new JMenuItem("Instraction");
  134.  
  135.         creator.addActionListener(new ActionListener() {
  136.  
  137.             public void actionPerformed(ActionEvent e) {
  138.                 JOptionPane.showMessageDialog(p2, "Chiara Moccia è il mio amore");
  139.             }
  140.         });
  141.  
  142.         help.add(creator);
  143.         help.add(instruction);
  144.         mymbar.add(help);
  145.  
  146.         setJMenuBar(mymbar);
  147.     }
  148.  
  149.     @SuppressWarnings("deprecation")
  150.         void reset() {
  151.         initializeValues();
  152.         p1.removeAll();
  153.  
  154.         myt.stop();
  155.  
  156.         createFirstSnake();
  157.         t.setText("Score==>" + score);
  158.  
  159.         myt = new Thread(this);
  160.         myt.start();
  161.     }
  162.  
  163.     void growup() {
  164.         lb[gu] = new JButton();
  165.         lb[gu].setEnabled(false);
  166.         p1.add(lb[gu]);
  167.  
  168.         int a = 10 + (10 * r.nextInt(48));
  169.         int b = 10 + (10 * r.nextInt(23));
  170.  
  171.         lbx[gu] = a;
  172.         lby[gu] = b;
  173.         lb[gu].setBounds(a, b, 10, 10);
  174.  
  175.         gu++;
  176.     }
  177.     // this method contains the logic to move the snake. player will define the derection
  178.     // this method just forward the snake to the right derection which deriction is pressed
  179.     // by the player.
  180.     void moveForward() {
  181.         for (int i = 0; i < gu; i++) {
  182.             lbp[i] = lb[i].getLocation();
  183.         }
  184.  
  185.         lbx[0] += directionx;
  186.         lby[0] += directiony;
  187.         lb[0].setBounds(lbx[0], lby[0], 10, 10);
  188.  
  189.         for (int i = 1; i < gu; i++) {
  190.             lb[i].setLocation(lbp[i - 1]);
  191.         }
  192.  
  193.         if (lbx[0] == x) {
  194.             lbx[0] = 10;
  195.         } else if (lbx[0] == 0) {
  196.             lbx[0] = x - 10;
  197.         } else if (lby[0] == y) {
  198.             lby[0] = 10;
  199.         } else if (lby[0] == 0) {
  200.             lby[0] = y - 10;
  201.         }
  202.  
  203.         if (lbx[0] == lbx[gu - 1] && lby[0] == lby[gu - 1]) {
  204.             food = false;
  205.             score += 5;
  206.             t.setText("Score==>" + score);
  207.             if (score % 50 == 0 && bonusflag == true) {
  208.                 p1.add(bonusfood);
  209.                 bonusfood.setBounds((10 * r.nextInt(50)), (10 * r.nextInt(25)), 15, 15);
  210.                 bfp = bonusfood.getLocation();
  211.                 bonusflag = false;
  212.             }
  213.         }
  214.  
  215.         if (bonusflag == false) {
  216.             if (bfp.x <= lbx[0] && bfp.y <= lby[0] && bfp.x + 10 >= lbx[0] && bfp.y + 10 >= lby[0]) {
  217.                 p1.remove(bonusfood);
  218.                 score += 100;
  219.                 t.setText("Score ==>" + score);
  220.                 bonusflag = true;
  221.             }
  222.         }
  223.  
  224.         if (food == false) {
  225.             growup();
  226.             food = true;
  227.         } else {
  228.             lb[gu - 1].setBounds(lbx[gu - 1], lby[gu - 1], 10, 10);
  229.         }
  230.  
  231.         for (int i = 1; i < gu; i++) {
  232.             if (lbp[0] == lbp[i]) {
  233.                 t.setText("GAME OVER    " + score);
  234.                 try {
  235.                     myt.join();
  236.                 } catch (InterruptedException ie) {
  237.                 }
  238.                 break;
  239.             }
  240.         }
  241.  
  242.         p1.repaint();
  243.         setVisible(true);
  244.     }
  245.  
  246.     public void keyPressed(KeyEvent e) {
  247.         // snake move to left when player pressed left arrow
  248.         if (runl == true && e.getKeyCode() == 37) {
  249.             directionx = -10; // means snake move right to left by 10pixels
  250.             directiony = 0;
  251.             runr = false;     // run right(runr) means snake cant move from left to right
  252.             runu = true;      // run up   (runu) means snake can move from down to up
  253.             rund = true;      // run down (run down) means snake can move from up to down
  254.         }
  255.         // snake move to up when player pressed up arrow
  256.         if (runu == true && e.getKeyCode() == 38) {
  257.             directionx = 0;
  258.             directiony = -10; // means snake move from down to up by 10 pixel
  259.             rund = false;     // run down (run down) means snake can move from up to down
  260.             runr = true;      // run right(runr) means snake can move from left to right
  261.             runl = true;      // run left (runl) means snake can move from right to left
  262.         }
  263.         // snake move to right when player pressed right arrow
  264.         if (runr == true && e.getKeyCode() == 39) {
  265.             directionx = +10; // means snake move from left to right by 10 pixel
  266.             directiony = 0;
  267.             runl = false;
  268.             runu = true;
  269.             rund = true;
  270.         }
  271.         // snake move to down when player pressed down arrow
  272.         if (rund == true && e.getKeyCode() == 40) {
  273.             directionx = 0;
  274.             directiony = +10; // means snake move from left to right by 10 pixel
  275.             runu = false;
  276.             runr = true;
  277.             runl = true;
  278.         }
  279.     }
  280.  
  281.     public void keyReleased(KeyEvent e) {
  282.     }
  283.  
  284.     public void keyTyped(KeyEvent e) {
  285.     }
  286.  
  287.     public void run() {
  288.         for (;;) {
  289.             // Move the snake move forword. In the start of the game snake move left to right,
  290.             // if player press up, down, right or left arrow snake change its direction according to
  291.             // pressed arrow
  292.             moveForward();
  293.             try {
  294.                 Thread.sleep(speed);
  295.             } catch (InterruptedException ie) {
  296.             }
  297.         }
  298.     }
  299. }



invece questo è il main:
Codice sorgente - presumibilmente Java

  1. public class Main {
  2. public static void main(String[] args){
  3.   new Snake();
  4. }}


PM Quote
Avatar
LittleHacker (Member)
Guru


Messaggi: 1033
Iscritto: 28/04/2009

Segnala al moderatore
Postato alle 23:59
Lunedì, 22/10/2012
L'errore mi sembra che stia qui:

Codice sorgente - presumibilmente Java

  1. for (int i = 1; i < gu; i++) {
  2.             if (lbp[0] == lbp[i]) {
  3.                 t.setText("GAME OVER    " + score);
  4.                 try {
  5.                     myt.join();
  6.                 } catch (InterruptedException ie) {
  7.                 }
  8.                 break;
  9.             }
  10.         }



Ma quando la attraversa ti dice game over oppure nulla, continua?
sembra tanto che gu, è la variabile che tiene conto del numero di code(se non sbaglio)!
Faccio delle prove e ti faccio sapere! :k:

PM Quote
Avatar
beppe_ita (Normal User)
Newbie


Messaggi: 6
Iscritto: 22/10/2012

Segnala al moderatore
Postato alle 0:09
Martedì, 23/10/2012
si l'errore è in quel pezzo di codice in particolare sembra che
Codice sorgente - presumibilmente Java

  1. if (lbp[0] == lbp[i])


non si verifichi mai!! lbp dovrebbe essere un Pointer in cui sono salvate le posizioni di ogni componente del serpente! ovvero lbp[0] è la testa e verifica se concide con un qualsiasi pezzo del corpo! tuttavia quando si scontra non segnala Game Over e il serpente si attraversa!!

PM Quote
Avatar
LittleHacker (Member)
Guru


Messaggi: 1033
Iscritto: 28/04/2009

Segnala al moderatore
Postato alle 0:21
Martedì, 23/10/2012
Testo quotato

Postato originariamente da beppe_ita:

si l'errore è in quel pezzo di codice in particolare sembra che
Codice sorgente - presumibilmente Java

  1. if (lbp[0] == lbp[i])


non si verifichi mai!! lbp dovrebbe essere un Pointer in cui sono salvate le posizioni di ogni componente del serpente! ovvero lbp[0] è la testa e verifica se concide con un qualsiasi pezzo del corpo! tuttavia quando si scontra non segnala Game Over e il serpente si attraversa!!



Adesso sto scaricando Eclipse(perchè non l'ho scaricato prima? Boh :asd: ) Appena ho qualche risultato ti faccio sapere!

PM Quote
Avatar
LittleHacker (Member)
Guru


Messaggi: 1033
Iscritto: 28/04/2009

Segnala al moderatore
Postato alle 1:35
Martedì, 23/10/2012
Ma il sito da dove lo hai scaricato, ti ha assicurato che funzionasse perchè qui sembra che ci sia un problema più grande del semplice if, sembra che non riesca a prendere i point giusti!

PM Quote
Avatar
beppe_ita (Normal User)
Newbie


Messaggi: 6
Iscritto: 22/10/2012

Segnala al moderatore
Postato alle 13:20
Martedì, 23/10/2012
Il programmatore assicurava che funziona ma nei commenti ammette che c'è questo bug!
Se provi a ad eseguirlo vedi che funziona tutto, tranne questa cosa quindi magari non è un errore globale! Comunque grazie per la tua disponibilità :)

PM Quote
Avatar
LittleHacker (Member)
Guru


Messaggi: 1033
Iscritto: 28/04/2009

Segnala al moderatore
Postato alle 14:11
Martedì, 23/10/2012
Trovato un'altro funzionante, un po' diverso:

Snake.java:
Codice sorgente - presumibilmente Java

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5. import javax.swing.JOptionPane;
  6. import javax.swing.UIManager;
  7.  
  8. public class Snake extends Core implements KeyListener {
  9.         // main method
  10.         public static void main(String[] args) {
  11.                 try {
  12.                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  13.                 } catch (Exception e){}
  14.                
  15.                 Object[] opts = { "Easy", "Normal", "Hard", "Insane", "Exit" };
  16.                 int dif = JOptionPane.showOptionDialog(null, "Select a difficulty",
  17.                                 "Snake", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
  18.                                 null, opts, opts);
  19.  
  20.                 if (dif == 0) {
  21.                         sleep = 110;
  22.                         grid = true;
  23.                         new Snake().run();
  24.                 } else if (dif == 1) {
  25.                         sleep = 99;
  26.                         grid = true;
  27.                         new Snake().run();
  28.                 } else if (dif == 2) {
  29.                         sleep = 69;
  30.                         grid = false;
  31.                         new Snake().run();
  32.                 } else if (dif == 3) {
  33.                         sleep = 29;
  34.                         grid = false;
  35.                         new Snake().run();
  36.                 } else {
  37.                         System.exit(0);
  38.                 }
  39.                
  40.                 if(replay){
  41.                         main(null);
  42.                 }
  43.         }
  44.  
  45.         ArrayList<Rectangle> snake;
  46.         Rectangle food;
  47.         boolean up, down, left, right, eaten, dirChanged;
  48.         static boolean grid;
  49.         int score;
  50.  
  51.         // init method
  52.         public void init() {
  53.                 super.init();
  54.                 w.addKeyListener(this);
  55.                 removeCursor();
  56.  
  57.                 snake = new ArrayList<Rectangle>();
  58.                 snake.add(new Rectangle(280, 240, 20, 20));
  59.                 snake.add(new Rectangle(281, 240, 20, 20));
  60.                 snake.add(new Rectangle(-20, -20, 20, 20));
  61.  
  62.                 eaten = true;
  63.                 score = 0;
  64.         }
  65.  
  66.         // updates the game
  67.         public void update() {
  68.                 // food
  69.                 food();
  70.                
  71.                 dirChanged = false;
  72.  
  73.                 // Collision between snake and itself
  74.                 for (int i = 0; i < snake.size(); i++) {
  75.                         for (int j = i + 1; j < snake.size(); j++) {
  76.                                 if (snake.get(i).getLocation().x == snake.get(j).getLocation().x
  77.                                                 && snake.get(i).getLocation().y == snake.get(j)
  78.                                                                 .getLocation().y) {
  79.                                         stop();
  80.                                         JOptionPane.showMessageDialog(null, "Game Over" + "\nYou scored " + score,
  81.                                                         "Snake", JOptionPane.PLAIN_MESSAGE);
  82.                                         w.setVisible(false);
  83.                                         replay = true;
  84.                                 }
  85.                         }
  86.                 }
  87.  
  88.                 // movement for snake
  89.                 for (int i = snake.size() - 1; i >= 0; i--) {
  90.                         if (up) {
  91.                                 try {
  92.                                         snake.get(i).setLocation(snake.get(i - 1).x,
  93.                                                         snake.get(i - 1).y);
  94.                                 } catch (Exception e) {
  95.                                         if (snake.get(i).getLocation().y > 0) {
  96.                                                 snake.get(i).setLocation(snake.get(i).x,
  97.                                                                 snake.get(i).y - 20);
  98.                                         } else {
  99.                                                 snake.get(i).setLocation(snake.get(i).x,
  100.                                                                 w.getHeight() - 20);
  101.                                         }
  102.                                 }
  103.                         } else if (down) {
  104.                                 try {
  105.                                         snake.get(i).setLocation(snake.get(i - 1).x,
  106.                                                         snake.get(i - 1).y);
  107.                                 } catch (Exception e) {
  108.                                         if (snake.get(i).getLocation().y < w.getHeight() - 20) {
  109.                                                 snake.get(i).setLocation(snake.get(i).x,
  110.                                                                 snake.get(i).y + 20);
  111.                                         } else {
  112.                                                 snake.get(i).setLocation(snake.get(i).x, 0);
  113.                                         }
  114.                                 }
  115.                         } else if (left) {
  116.                                 try {
  117.                                         snake.get(i).setLocation(snake.get(i - 1).x,
  118.                                                         snake.get(i - 1).y);
  119.                                 } catch (Exception e) {
  120.                                         if (snake.get(i).getLocation().x > 0) {
  121.                                                 snake.get(i).setLocation(snake.get(i).x - 20,
  122.                                                                 snake.get(i).y);
  123.                                         } else {
  124.                                                 snake.get(i).setLocation(w.getWidth() - 20,
  125.                                                                 snake.get(i).y);
  126.                                         }
  127.                                 }
  128.                         } else if (right) {
  129.                                 try {
  130.                                         snake.get(i).setLocation(snake.get(i - 1).x,
  131.                                                         snake.get(i - 1).y);
  132.                                 } catch (Exception e) {
  133.                                         if (snake.get(i).getLocation().x < w.getWidth() - 20) {
  134.                                                 snake.get(i).setLocation(snake.get(i).x + 20,
  135.                                                                 snake.get(i).y);
  136.                                         } else {
  137.                                                 snake.get(i).setLocation(0, snake.get(i).y);
  138.                                         }
  139.                                 }
  140.                         }
  141.                 }
  142.         }
  143.  
  144.         // food method
  145.         public void food() {
  146.                 if (eaten && up || eaten && down || eaten && left || eaten && right) {
  147.                         int x = new Random().nextInt(30) * 20;
  148.                         int y = new Random().nextInt(25) * 20;
  149.                         boolean spawn = true;
  150.  
  151.                         for (int i = 0; i < snake.size(); i++) {
  152.                                 if (snake.get(i).x == x && snake.get(i).y == y) {
  153.                                         spawn = false;
  154.                                 }
  155.                         }
  156.  
  157.                         if (spawn) {
  158.                                 food = new Rectangle(x, y, 20, 20);
  159.                                 eaten = false;
  160.                         }
  161.                 } else if (food != null && !eaten) {
  162.                         if (snake.get(0).x == food.x && snake.get(0).y == food.y) {
  163.                                 snake.add(new Rectangle(-20, -20, 20, 20));
  164.                                 score++;
  165.                                 eaten = true;
  166.                         }
  167.                 }
  168.         }
  169.  
  170.         // draw method
  171.         public void draw(Graphics2D g) {
  172.                 g.setColor(Color.BLACK);
  173.                 g.fillRect(0, 0, w.getWidth(), w.getHeight());
  174.  
  175.                 g.setColor(Color.GRAY);
  176.                 if(grid){
  177.                         for (int x = 0; x < w.getWidth() / 20; x++) {
  178.                                 for (int y = 0; y < w.getHeight() / 20; y++) {
  179.                                         g.drawRect(x * 20, y * 20, 20, 20);
  180.                                 }
  181.                         }
  182.                 }
  183.                
  184.                 if (!eaten) {
  185.                         g.fill(food);
  186.                 }
  187.                
  188.                 g.setColor(Color.WHITE);
  189.                 for (int i = snake.size() - 1; i >= 0; i--) {
  190.                         g.fill(snake.get(i));
  191.                 }
  192.  
  193.                 g.setFont(new Font("Dialog", Font.PLAIN, 16));
  194.                 g.drawString("Score: " + score, 2, 17);
  195.                 g.drawString("Escape to quit", 482, 17);
  196.                 if(!up && !down && !left && !right){
  197.                         g.drawString("Up, down, left or right to start game", 162, 17);
  198.                 }
  199.         }
  200.  
  201.         // Key listeners
  202.         public void keyPressed(KeyEvent e) {
  203.                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
  204.                         stop();
  205.                 } else if (e.getKeyCode() == KeyEvent.VK_UP && !down && !dirChanged) {
  206.                         dirChanged = true;
  207.                         up = true;
  208.                         down = false;
  209.                         left = false;
  210.                         right = false;
  211.                 } else if (e.getKeyCode() == KeyEvent.VK_DOWN && !up && !dirChanged) {
  212.                         dirChanged = true;
  213.                         up = false;
  214.                         down = true;
  215.                         left = false;
  216.                         right = false;
  217.                 } else if (e.getKeyCode() == KeyEvent.VK_LEFT && !right && !dirChanged) {
  218.                         dirChanged = true;
  219.                         up = false;
  220.                         down = false;
  221.                         left = true;
  222.                         right = false;
  223.                 } else if (e.getKeyCode() == KeyEvent.VK_RIGHT && !left && !dirChanged) {
  224.                         dirChanged = true;
  225.                         up = false;
  226.                         down = false;
  227.                         left = false;
  228.                         right = true;
  229.                 }
  230.         }
  231.  
  232.         public void keyReleased(KeyEvent e) {
  233.         }
  234.  
  235.         public void keyTyped(KeyEvent e) {
  236.         }
  237. }



Core.java:
Codice sorgente - presumibilmente Java

  1. import java.awt.*;
  2. import java.awt.image.BufferStrategy;
  3. import java.awt.image.BufferedImage;
  4. import javax.swing.*;
  5.  
  6. public abstract class Core {
  7.         boolean running;
  8.         static boolean replay;
  9.         JFrame w;
  10.        
  11.         //stop method
  12.         public void stop(){
  13.                 running = false;
  14.         }
  15.        
  16.         //call init and gameloop
  17.         public void run(){
  18.                 try{
  19.                         init();
  20.                         gameLoop();
  21.                 }finally{
  22.                         if(!replay){
  23.                                 System.exit(0);
  24.                         }
  25.                 }
  26.         }
  27.        
  28.         //set to full screen
  29.         public void init(){
  30.                 w = new JFrame();
  31.                
  32.                 w.setUndecorated(true);
  33.                 w.setIgnoreRepaint(true);
  34.                 w.setResizable(false);
  35.                
  36.                 w.setSize(600, 500);
  37.                 w.setLocationRelativeTo(null);
  38.                
  39.                 w.setFont(new Font("Arial", Font.PLAIN, 24));
  40.                 w.setBackground(Color.BLACK);
  41.                 w.setForeground(Color.WHITE);
  42.                 w.setVisible(true);
  43.                
  44.                 w.createBufferStrategy(2);
  45.                 replay = false;
  46.                 running = true;
  47.         }
  48.        
  49.         static long sleep;
  50.        
  51.         //main game loop
  52.         public void gameLoop(){
  53.                 while(running){
  54.                         Graphics2D g = (Graphics2D) w.getBufferStrategy().getDrawGraphics();
  55.                         draw(g);
  56.                         g.dispose();
  57.                         this.update();
  58.                        
  59.                         if(w != null){
  60.                                 BufferStrategy s = w.getBufferStrategy();
  61.                                 if(!s.contentsLost()){
  62.                                         s.show();
  63.                                 }
  64.                         }
  65.                        
  66.                         try{
  67.                                 Thread.sleep(sleep);
  68.                         }catch(Exception e){}
  69.                 }
  70.         }
  71.        
  72.         //update game
  73.         public abstract void update();
  74.        
  75.         //draws to the screen
  76.         public abstract void draw(Graphics2D g);
  77.        
  78.         //sets blank cursor
  79.         public void removeCursor(){
  80.                 BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
  81.                 Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
  82.                 w.setCursor(blankCursor);
  83.         }
  84. }


L'altro non so proprio come sistemarlo :d :k:

Edit:
Ops 8-| perdonami, ero di fretta e non ho controllato il codice postato, però ero sicuro di aver copiato il codice core! Scusa :k:

Ultima modifica effettuata da LittleHacker il 23/10/2012 alle 23:25
PM Quote
Avatar
beppe_ita (Normal User)
Newbie


Messaggi: 6
Iscritto: 22/10/2012

Segnala al moderatore
Postato alle 21:23
Martedì, 23/10/2012
Grazie milleeeee!!! c'è sono un piccolo problemino col tuo codice :)
e cioè che hai postato 2 volte il codice del Main e manca il Core :D

PM Quote
Avatar
gigisoft (Member)
Guru


Messaggi: 696
Iscritto: 11/10/2008

Segnala al moderatore
Postato alle 12:17
Mercoledì, 24/10/2012
Testo quotato

gioco snake: il serpente non muore!!



:ot: vedi che forse si chiama Dankan Mc Claude :rotfl: :ot:

PM Quote
Pagine: [ 1 2 ] Precedente | Prossimo