0% encontró este documento útil (0 votos)
30 vistas8 páginas

Completo Minitenis

Este documento presenta el código para un juego de Snake escrito en Java. Define las variables y métodos necesarios para controlar el movimiento de la serpiente y la manzana, detectar colisiones, reiniciar el juego y pausar/reanudar. El código controla el movimiento y dibujo de los objetos, y la lógica del juego para actualizar la posición de la serpiente y manejear los eventos del usuario.

Cargado por

22325050510729
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
30 vistas8 páginas

Completo Minitenis

Este documento presenta el código para un juego de Snake escrito en Java. Define las variables y métodos necesarios para controlar el movimiento de la serpiente y la manzana, detectar colisiones, reiniciar el juego y pausar/reanudar. El código controla el movimiento y dibujo de los objetos, y la lógica del juego para actualizar la posición de la serpiente y manejear los eventos del usuario.

Cargado por

22325050510729
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd

Identifique los códigos nuevos y estudie para qué se usan

import GameLib.Game;

import java.awt.Graphics;

import java.awt.Color;

import java.awt.Rectangle;

import java.awt.Event;

import java.awt.Image;

import java.applet.AudioClip;

public class snake extends Game

// Aquí se declaran nuestras variables

boolean PAUSE = true;

int maxlong = 500;

Rectangle serpiente[] = new Rectangle[maxlong];

Rectangle manzana = new Rectangle (100, 100, 10, 10);

int direccion = 1;

int longitud = 1;

boolean gameover;

boolean leible;

Image iSerpiente, iManzana, iFondo;

AudioClip aComer, aMorir;

public void init()

// Comienza descarga asíncrona de recursos pesados


this.setSize(350, 200);

this.setBackground(Color.black);

iSerpiente = this.getImage (this.getCodeBase(), "media/body.png");

iManzana = this.getImage (this.getCodeBase(), "media/apple.png");

iFondo = this.getImage(this.getCodeBase(), "media/grass.png");

aComer = this.getAudioClip (this.getCodeBase(), "media/chomp.au");

aMorir = this.getAudioClip (this.getCodeBase(), "media/dies.au");

for (int i = 0; i < maxlong; i++)

serpiente[i] = new Rectangle(0, 0, 10, 10);

reset();

public void reset()

serpiente[0].x=50;

serpiente[0].y=50;

direccion = 1;

longitud = 3;

manzana.x = (int)(Math.random() * (this.getWidth()/10 - 1)) * 10;

manzana.y = (int)(Math.random() * (this.getHeight()/10 - 2)) * 10 + 10;

gameover = false;

}
public void game()

// Aquí va el código de nuestro juego

if (!PAUSE)

// Se mueve el serpiente

for (int i = longitud; i > 0; i--)

serpiente[i].x = serpiente[i-1].x;

serpiente[i].y = serpiente[i-1].y;

// Se mueve el serpiente[0]

switch (direccion)

case 0:

serpiente[0].y -= 10;

break;

case 1:

serpiente[0].x += 10;

break;

case 2:

serpiente[0].y += 10;

break;

case 3:
serpiente[0].x -= 10;

break;

leible = true;

// Salida del escenario

if (serpiente[0].x < 0)

serpiente[0].x = this.getWidth()-10;

if (serpiente[0].x > this.getWidth()-10)

serpiente[0].x = 0;

if (serpiente[0].y < 10)

serpiente[0].y = this.getHeight()-10;

if (serpiente[0].y > this.getHeight()-10)

serpiente[0].y = 10;

// Colisión serpiente[0]-Manzana

if (serpiente[0].intersects(manzana))

{
if (longitud < maxlong)

longitud += 1;

aComer.play();

manzana.x = (int)(Math.random() * (this.getWidth()/10 - 1)) * 10;

manzana.y = (int)(Math.random() * (this.getHeight()/10 - 2)) * 10 +


10;

// Colisión con el serpiente

for (int i = 2; i < longitud; i++)

if (serpiente[0].intersects(serpiente[i]))

PAUSE = true;

gameover = true;

aMorir.play();

public void paint(Graphics g)

// Aquí se dibujan todos los objetos

//g.clearRect(0, 0, this.getWidth(), this.getHeight());

g.drawImage(iFondo, 0, 0, this);
//g.setColor(Color.green);

for (int i = 0; i < longitud; i++)

//g.drawOval(serpiente[i].x, serpiente[i].y, serpiente[i].width,


serpiente[i].height);

g.drawImage(iSerpiente, serpiente[i].x, serpiente[i].y, this);

//g.setColor(Color.red);

//g.drawOval(manzana.x, manzana.y, manzana.width, manzana.height);

g.drawImage(iManzana, manzana.x, manzana.y, this);

g.setColor(Color.white);

g.drawString("Longitud: " + longitud, 10, 10);

if (gameover)

g.drawString("GAME OVER", this.getWidth()/2-30, this.getHeight()/2);

else if (PAUSE)

g.drawString("PAUSA", this.getWidth()/2-20, this.getHeight()/2);

public boolean keyDown(Event e, int key)

switch(key)

{
// Cambia el rumbo de la serpiente

case Event.UP:

if (direccion != 2 && leible)

direccion = 0;

leible = false;

break;

case Event.RIGHT:

if (direccion != 3 && leible)

direccion = 1;

leible = false;

break;

case Event.DOWN:

if (direccion != 0 && leible)

direccion = 2;

leible = false;

break;

case Event.LEFT:

if (direccion != 1 && leible)

direccion = 3;
leible = false;

break;

case Event.ENTER:

PAUSE = !PAUSE;

if (gameover)

reset();

break;

return true;

También podría gustarte