Documente Academic
Documente Profesional
Documente Cultură
UNIDAD 1
Timer
NÚMERO DE CONTROL
C17211621
DOCENTE:
Dr. Bogart Yair Márquez Lobato
FECHA:
16/Marzo/2020
Introducción
En tópicos avanzados de programación, la meta es lograr conocer las distintas
herramientas con las que contamos a la hora de programar. El Timer es una de esas
herramientas que nos ayudaran cuando necesitemos que una acción o un conjunto de
acciones se realicen cada cierto tiempo. En esta practica se comprendido de mejor
manera el uso del Timer al crear una réplica del clásico juego de “Snake”, en el cual el
Timer fungió como herramienta indispensable para evaluar la situación del juego en
todo momento y que este pueda saber si el jugador se encuentra sobre la comida o
este ha chocado en algún obstáculo.
Código:
Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Snake3
{
public partial class Form1 : Form
{
Graphics juego;//Aqui se crean las variables y los objetos que seran utilizadas
en la ejecucion del juego
Cola cabeza;
Comida comida;
int espacio;
int xdir = 0, ydir = 0;
int puntaje = 0;
Boolean ejex = true, ejey = true;
public Form1()
{
espacio = 10;
InitializeComponent();
juego = canvas.CreateGraphics();
cabeza = new Cola(10, 10);
comida = new Comida();
high.Text = puntos.Text;
}
public void findeJuego()//Aqui se crea lo necesario para indicar que el juego
termino, como un MessageBox que salte para mencionar que el juego temrino
{
xdir = 0;
ydir = 0;
puntaje = 0;
high.Text = puntos.Text;
puntos.Text = "0";
ejex = true;
ejey = true;
cabeza = new Cola(10, 10);
comida = new Comida();
MessageBox.Show("Has perdido\nMala suerte amigo, intentalo de nuevo!!");
facil.Enabled = true;
Dificil.Enabled = true;
Salir.Enabled = true;
}
public void choqueCuerpo()//Este metodo se encarga de evaluar la situacion del
juego para detectar si el jugador choco con su propio cuerpo
{
Cola temp;
try
{
temp = cabeza.verSiguiente().verSiguiente();
}
catch (Exception err)
{
temp = null;
}
while (temp != null)
{
if (cabeza.interseccion(temp))
{
findeJuego();
}
else
{
temp = temp.verSiguiente();
}
}
}
findeJuego();
}
}
}
public void movimiento()//Este metodo se encarga del movimiento
{
cabeza.setxy(cabeza.getX() + xdir, cabeza.getY() + ydir);
}
}
}
}
Objeto:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snake3
{
class objeto
{
protected int x, y, espacio;
public objeto()
{
espacio = 10;
}
public Boolean interseccion(objeto o)
{
int difx = Math.Abs(this.x - o.x);
int dify = Math.Abs(this.y - o.y);
if (difx >= 0 && difx < espacio && dify >= 0 && dify < espacio)
{
return true;
}
else
{
return false;
}
}
}
}
Cola:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace Snake3
{
class Cola : objeto
{
Cola siguiente;
public Cola(int x, int y)
{
this.x = x;
this.y = y;
siguiente = null;
}
public void dibujar(Graphics g)
{
if (siguiente != null)
{
siguiente.dibujar(g);
}
g.FillRectangle(new SolidBrush(Color.ForestGreen), this.x, this.y,
this.espacio, this.espacio);
}
public void setxy(int x, int y)
{
if (siguiente != null)
{
siguiente.setxy(this.x, this.y);
}
this.x = x;
this.y = y;
}
public void meter()
{
if (siguiente == null)
{
siguiente = new Cola(this.x, this.y);
}
else
{
siguiente.meter();
}
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public Cola verSiguiente()
{
return siguiente;
}
}
}
Comida:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace Snake3
{
class Comida : objeto
{
public Comida()
{
this.x = generar(77);
this.y = generar(37);
}
public void dibujar(Graphics g)
{
g.FillRectangle(new SolidBrush(Color.HotPink), this.x, this.y, this.espacio,
this.espacio);
}
public void colocar()
{
this.x = generar(77);
this.y = generar(37);
}
public int generar(int n)
{
Random random = new Random();
int num = random.Next(0, n) * 10;
return num;
}
}
}
Fotos del programa funcionando: