Buscar

Diagrama de Classes em C++

Prévia do material em texto

Diagrama de clases:
Personaje.h:
#pragma once
#include <iostream>
using namespace System::Drawing;
class Personaje {
protected:
	int x, y;
	int dx, dy;
	int ancho, alto;
	int idx, idy;
	bool visible;
public:
	Personaje(int ancho, int alto) {
		this->ancho = ancho;
		this->alto = alto;
		visible = true;
	}
	~Personaje() {}
	void dibujar(Graphics^ g, Bitmap^ bmp) {
		Rectangle seccion = Rectangle(idx * ancho, idy * alto, ancho, alto);
		Rectangle zoom = Rectangle(x, y, ancho * 1.0, alto * 1.0);
		g->DrawImage(bmp, zoom, seccion, GraphicsUnit::Pixel);
	}
	virtual void mover() {}
	Rectangle getRectangle() { return Rectangle(x, y, ancho * 1.0, alto * 1.0); }
	int getX() { return x; }
	int getY() { return y; }
	void setX(int v) { x = v; }
	void setY(int v) { y = v; }
	bool getVisible() { return visible; }
	void setVisible(bool v) { visible = v; }
};
Prota1.h:
#pragma once
#include "Personaje.h"
class Prota1 : public Personaje {
private:
	int vidas;
	char direccion;
	int inicioX, inicioY;
	int puntos;
public:
	Prota1(int ancho, int alto) : Personaje(ancho, alto) {
		x = 325;
		y = 15;
		dx = dy = 15;
		idx = idy = 0;
		direccion = 'D';
		vidas = 5;
		inicioX = x;
		inicioY = y;
	}
	~Prota1() {}
	void mover(Graphics^ g, char tecla) {
		switch (tecla)
		{
		case 'A':
			if (x > 0) {
				x -= dx;
				idy = 1;
				direccion = 'A';
			}
			break;
		case 'D':
			if (x + ancho * 1.0 < g->VisibleClipBounds.Width) {
				x += dx;
				idy = 0;
				direccion = 'D';
			}
			break;
		}
		idx++;
		if (idx > 2) { idx = 0; }
	}
	void disminuir_Vidas() { vidas--; }
	int getX() { return x; }
	int getY() { return y; }
	void setX(int v) { x = v; }
	void setY(int v) { y = v; }
	int getVidas() { return vidas; }
	void setVidas(int v) { vidas = v; }
	char getDireccion() { return direccion; }
	void setDireccion(char v) { direccion = v; }
	int getInicioX() { return inicioX; }
	int getInicioY() { return inicioY; }
};
Prota2.h:
#pragma once
#include "Personaje.h"
class Prota2 : public Personaje {
private:
	int vidas;
	char direccion;
	int inicioX, inicioY;
public:
	Prota2(int ancho, int alto) : Personaje(ancho, alto) {
		x = 325;
		y = 15;
		dx = dy = 15;
		idx = idy = 0;
		direccion = 'D';
		vidas = 5;
		inicioX = x;
		inicioY = y;
	}
	~Prota2() {}
	void mover(Graphics^ g, char tecla) {
		switch (tecla)
		{
		case 'A':
			if (x > 0) {
				x -= dx;
				idy = 0;
				direccion = 'A';
			}
			break;
		case 'D':
			if (x + ancho * 1.0 < g->VisibleClipBounds.Width) {
				x += dx;
				idy = 1;
				direccion = 'D';
			}
			break;
		}
		idx++;
		if (idx > 2) { idx = 0; }
	}
	void disminuir_Vidas() { vidas--; }
	int getX() { return x; }
	int getY() { return y; }
	void setX(int v) { x = v; }
	void setY(int v) { y = v; }
	int getVidas() { return vidas; }
	void setVidas(int v) { vidas = v; }
	char getDireccion() { return direccion; }
	void setDireccion(char v) { direccion = v; }
	int getInicioX() { return inicioX; }
	int getInicioY() { return inicioY; }
};
Enemigo.h:
#pragma once
#include <iostream>
using namespace System::Drawing;
class Enemigo : public Personaje {
private:
	int idX, idY;
	int direccion;
	bool visibility;
public:
	Enemigo(int w, int h, int d) :Personaje(ancho, alto) {
		direccion = d;
		dx = dy = 5;
		x = 590;
		y = 15;
		idX = idY = 0;
		visibility = true;
	}
	~Enemigo() {}
	void move(Graphics^ g) {
		if (x < 0 || x + ancho * 1.0 > g->VisibleClipBounds.Width - 100) { dx *= -1; }
		x += dx;
	}
	void setVisibility(bool v) { visibility = v; }
	bool getVisibility() { return visibility; }
};
Disparo.h:
#pragma once
#include <iostream>
using namespace System::Drawing;
class Disparo {
private:
	int x, y;
	char direccion;
	int dx, dy;
	bool visible;
	int ancho, alto;
public:
	Disparo(int xjugador, int yjugador, char direccion,int w, int h) {
		ancho = w;
		alto = h;
		this->x = xjugador - 5;
		this->y = yjugador;
		dx = dy = 15;
		this->direccion = direccion;
		visible = true;
	}
	~Disparo() {}
	void dibujar(Graphics^ g, Bitmap^ bmp) {
		g->DrawImage(bmp, x, y, ancho * 0.03, alto * 0.03);
	}
	void move(Graphics^ g) {
		if (direccion == 'A' && x >= 0) {
			x -= dx;
		}
		if (direccion == 'D' && x + ancho <= g->VisibleClipBounds.Width) {
			x += dx;
		}
	}
	Rectangle getRectangle() { return Rectangle(x, y, ancho, alto); }
	bool getVisible() { return visible; }
	void setVisible(bool v) { visible = v; }
	int getXAncho() { return x + ancho; }
	int getYAlto() { return y + ancho; }
	int getX() { return x; }
	int getY() { return y; }
};
Dinamita.h:
#pragma once
#include <iostream>
using namespace System::Drawing;
class Dinamita {
private:
	int x, y;
	char direccion;
	int dx, dy;
	bool visible;
	int ancho, alto;
public:
	Dinamita(int xjugador, int yjugador, char direccion, int w, int h) {
		ancho = w;
		alto = h;
		this->x = xjugador - 5;
		this->y = yjugador;
		dx = dy = 15;
		this->direccion = direccion;
		visible = true;
	}
	~Dinamita() {}
	void dibujar(Graphics^ g, Bitmap^ bmp) {
		g->DrawImage(bmp, x, y, ancho * 0.03, alto * 0.03);
	}
	Rectangle getRectangle() { return Rectangle(x, y, ancho, alto); }
	bool getVisble() { return visible; }
	void setVisible(bool v) { visible = v; }
	int getX() { return x; }
	int getY() { return y; }
};
Controller.h:
#pragma once
#include "Prota1.h"
#include "Prota2.h"
#include "Disparo.h"
#include "Enemigo.h"
#include "Dinamita.h"
#include <vector>
using namespace std;
class Controlador {
private:
	Prota1* prota1;
	Prota2* prota2;
	vector<Enemigo*>enemigo;
	vector<Disparo*>disparos;
	vector<Dinamita*>dinamita;
	int numberOfEnemies;
	int puntaje;
	int n_enemigos_matados;
public:
	Controlador() {
		numberOfEnemies = 5;
		puntaje = 0;
		n_enemigos_matados=0
	}
	~Controlador() {}
	void createEnemies(int width, int height) {
		for (int i = 0; i < numberOfEnemies; i++)
		{
			Enemigo* e = new Enemigo(width, height, rand() % 2);
			enemigo.push_back(e);
		}
	}
	void addInyeccion(Disparo* b) {
		disparos.push_back(b);
	}
	void drawEveryThing(Graphics^ g, Bitmap^ bmpEnemigo, Bitmap^ bmpDisparo, Bitmap^ bmpDinamita) {
		for (int i = 0; i < enemigo.size(); i++)
		{
			enemigo[i]->dibujar(g, bmpEnemigo);
		}
		for (int i = 0; i < disparos.size(); i++)
		{
			disparos[i]->dibujar(g, bmpDisparo);
		}
		for (int i = 0; i < dinamita.size(); i++)
		{
			dinamita[i]->dibujar(g, bmpDinamita);
		}
	}
	void moveEveryThing(Graphics^ g) {
		for (int i = 0; i < enemigo.size(); i++)
		{
			enemigo[i]->move(g);
		}
		for (int i = 0; i < disparos.size(); i++)
		{
			disparos[i]->move(g);
		}
	}
	void collision(Graphics^ g) {
		//verificar
		for (int i = 0; i < disparos.size(); i++)
		{
			if (disparos[i]->getX() <= 0 || disparos[i]->getXAncho() >= g->VisibleClipBounds.Width ||
				disparos[i]->getY() <= 0 || disparos[i]->getYAlto() >= g->VisibleClipBounds.Height) {
				disparos[i]->setVisible(false);
			}
		}
		// colision disparo vs enemigos
		for (int i = 0; i < enemigo.size(); i++)
		{
			for (int j = 0; j < disparos.size(); j++)
			{
				if (enemigo[i]->getRectangle().IntersectsWith(disparos[j]->getRectangle())) {
					enemigo[i]->setVisibility(false);
					disparos[j]->setVisible(false);
				}
			}
			puntaje += 8;
			n_enemigos_matados += 1;
		}
		// colision dinamita vs enemigos
		for (int i = 0; i < enemigo.size(); i++)
		{
			for (int j = 0; j < dinamita.size(); j++)
			{
				if (enemigo[i]->getRectangle().IntersectsWith(dinamita[j]->getRectangle())) {
					enemigo[i]->setVisibility(false);
					dinamita[j]->setVisible(false);
				}
			}
			puntaje += 8;
			n_enemigos_matados += 1;
		}
		// colision protagonistas vs enemigos
		for (int i = 0; i < enemigo.size(); i++)
		{
			if (prota1->getRectangle().IntersectsWith(enemigo.at(i)->getRectangle())) {
				prota1->setVisible(false);
				prota1->setX(prota1->getInicioX());
				prota1->setY(prota1->getInicioY());
				prota1->disminuir_Vidas();
			}
		}
		for (int i = 0; i < enemigo.size(); i++)
		{
			if (prota2->getRectangle().IntersectsWith(enemigo.at(i)->getRectangle())) {
				prota2->setVisible(false);
				prota2->setX(prota2->getInicioX());prota2->setY(prota2->getInicioY());
				prota2->disminuir_Vidas();
			}
		}
		//borrar
		for (int i = 0; i < disparos.size(); i++)
		{
			if (!disparos[i]->getVisible()) {
				disparos.erase(disparos.begin() + i);
			}
		}
		for (int i = 0; i < enemigo.size(); i++)
		{
			if (!enemigo[i]->getVisibility()) {
				enemigo.erase(enemigo.begin() + i);
			}
		}
	}
	Prota1* getProta1() { return prota1; }
	Prota2* getProta2() { return prota2; }
	int getPuntos() { return puntaje; }
};
Myform.h:
#pragma once
#include<ctime>
#include"Controller.h"
namespace EF {
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:
		MyForm(void)
		{
			InitializeComponent();
			srand(time(NULL));
			g = this->CreateGraphics();
			space = BufferedGraphicsManager::Current;
			buffer = space->Allocate(g, this->ClientRectangle);
			bmpProta1 = gcnew Bitmap("imagenes/Prota1.png");
			bmpProta2 = gcnew Bitmap("imagenes/Prota2.png");
			bmpEnemigo = gcnew Bitmap("imagenes/Enemigo.jpg");
			bmpMap = gcnew Bitmap("imagenes/Fondo.png");
			bmpDisparo = gcnew Bitmap("imagenes/Rayo.png");
			bmpDinamita = gcnew Bitmap("imagenes/Dinamita.png");
			prota1 = new Prota1(bmpProta1->Width / 5, bmpProta1->Height / 2);
			prota2 = new Prota2(bmpProta2->Width / 5, bmpProta2->Height / 2);
			controlador = new Controlador();
		}
	protected:
		~MyForm()
		{
			if (components)
			{
				delete components;
			}
		}
	private:
		System::ComponentModel::Container^ components;
	protected:
	private:
		Graphics^ g;
		BufferedGraphicsContext^ space;
		BufferedGraphics^ buffer;
		Bitmap^ bmpProta1;
		Bitmap^ bmpProta2;
		Bitmap^ bmpEnemigo;
		Bitmap^ bmpMap;
		Bitmap^ bmpDisparo;
		Bitmap^ bmpDinamita;
		Prota1* prota1;
		Prota2* prota2;
		Controlador* controlador;
	private: System::Windows::Forms::Timer^ timer1;
	private: System::Windows::Forms::Button^ btn_Final;
	private: System::Windows::Forms::Label^ lblVidas;
	private: System::Windows::Forms::Label^ lblPuntos;
	private: System::Windows::Forms::Timer^ tmrMain;
#pragma region Windows Form Designer generated code
		 void InitializeComponent(void)
		 {
			 this->components = (gcnew System::ComponentModel::Container());
			 this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
			 this->lblVidas = (gcnew System::Windows::Forms::Label());
			 this->tmrMain = (gcnew System::Windows::Forms::Timer(this->components));
			 this->SuspendLayout();
			 // 
			 // timer1
			 // 
			 this->timer1->Enabled = true;
			 this->timer1->Tick += gcnew System::EventHandler(this, &MyForm::timer1_Tick);
			 // 
			 // btn_Final
			 //
			 this->btn_Final->AutoEllipsis = true;
			 this->btn_Final->BackColor = System::Drawing::SystemColors::ActiveCaption;
			 this->btn_Final->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
				 static_cast<System::Byte>(0)));
			 this->btn_Final->Location = System::Drawing::Point(340, 375);
			 this->btn_Final->Name = L"btn_Final";
			 this->btn_Final->Size = System::Drawing::Size(116, 45);
			 this->btn_Final->TabIndex = 1;
			 this->btn_Final->Text = L"FINAL";
			 this->btn_Final->UseVisualStyleBackColor = false;
			 this->btn_Final->Click += gcnew System::EventHandler(this, &EF::btn_Final_Click);
			 // 
			 // lblVidas
			 // 
			 this->lblVidas->AutoSize = true;
			 this->lblVidas->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
				 static_cast<System::Byte>(0)));
			 this->lblVidas->Location = System::Drawing::Point(12, 9);
			 this->lblVidas->Name = L"lblVidas";
			 this->lblVidas->Size = System::Drawing::Size(60, 22);
			 this->lblVidas->TabIndex = 0;
			 this->lblVidas->Text = L"Vidas";
			 this->lblVidas->Click += gcnew System::EventHandler(this, &MyForm::lblVidas_Click);
			 // 
			 // lblPuntos
			 //
			 this->lblPuntos->AutoSize = true;
			 this->lblPuntos->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
				 static_cast<System::Byte>(0)));
			 this->lblPuntos->Location = System::Drawing::Point(800, 9);
			 this->lblPuntos->Name = L"lblPuntos";
			 this->lblPuntos->Size = System::Drawing::Size(60, 22);
			 this->lblPuntos->TabIndex = 0;
			 this->lblPuntos->Text = L"Puntos";
			 this->lblPuntos->Click += gcnew System::EventHandler(this, &MyForm::lblVidas_Click);
			 // 
			 // MyForm
			 // 
			 this->AutoScaleDimensions = System::Drawing::SizeF(9, 20);
			 this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			 this->ClientSize = System::Drawing::Size(843, 400);
			 this->Controls->Add(this->lblVidas);
			 this->Name = L"MyForm";
			 this->Text = L"MyForm";
			 this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
			 this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &MyForm::MyForm_KeyDown);
			 this->ResumeLayout(false);
			 this->PerformLayout();
		 }
#pragma endregion
	private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
	}
	private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
		//clear
		buffer->Graphics->Clear(Color::WhiteSmoke);
		//collision
		controlador->collision(buffer->Graphics);
		//move para el personaje se hara con teclas
		controlador->moveEveryThing(buffer->Graphics);
		//draw
		buffer->Graphics->DrawImage(bmpMap, 0, 0, bmpMap->Width * 2.2, bmpMap->Height * 1.6);
		controlador->drawEveryThing(buffer->Graphics, bmpEnemigo, bmpDisparo, bmpDinamita);
		prota1->dibujar(buffer->Graphics, bmpProta1);
		//lblVidas->Text = "Vidas: " + controller->getDoctor()->getVidas();
		if (controlador->getProta1()->getVidas() == 0 || controlador->getProta2()->getVidas() == 0) {
			this->tmrMain->Enabled = false;
			MessageBox::Show("Termino el juego");
		}
		else;
		if (controlador->getPuntos()) {
			this->tmrMain->Enabled = false;
			MessageBox::Show("Enemigos eliminados: " << puntaje);
		}
		//Render
		buffer->Render(g);
	}
	private: System::Void lblVidas_Click(System::Object^ sender, System::EventArgs^ e) {
	}
	private: System::Void MyForm_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) {
		switch (e->KeyCode)
		{
		case Keys::Left: controlador->getProta1()->mover(buffer->Graphics, 'A'); break;
		case Keys::Right: controlador->getProta1()->mover(buffer->Graphics, 'D'); break;
		case Keys::Space:
			Disparo* b = new Disparo(prota2->getX(), prota2->getY(), bmpDisparo->Width, bmpDisparo->Height, prota2->getDireccion());
			controlador->addInyeccion(b);
		}
	}
	};
}
Myform.cpp:
#include "MyForm.h"
using namespace EF;
int main() {
	Application::Run(gcnew MyForm());
	return 0;
}
Imágenes:
Enemigo:
Prota1:
Prota2:
Rayo:
Dinamita:

Continue navegando