Feedback

C# - GameEngine2D(Gravitation/Content laden/Collision)PreAlpha

Veröffentlicht von am 7/19/2013
(1 Bewertungen)
Hi,
die GameEngine2DPreAlpha ist ein, von mir angefangenes Projekt, der das Verwalten und Erstellen von 2D-Forms-Games(Sollte nicht gemacht werden, dient aber nur zum Spaß)
Ich werde diese Seite updaten, um Euch mit den neuen Versionen vertraut zu machen...

Tutorial
Zunächst einmal wird das Verweisen und Importieren der DLL benötigt.
Imports_2D_Engine_PreAlpha__Test
(;)
Nun das instaziieren,
Dim GameEngine As New GameEngine2DPreAlpha


Das Laden des Contents im Load-Ereignis der Form,

GameEngine.AddContent("TEXTUR2D")
GameEngine.LoadContentTextur2D([FORM.HANDLE], [LOOPS_INT])


Im Paint-Ereignis,
GameEngine.NewVector(GDI+-GAPHICS(e.graphics), "TEXTUR2D", X, Y, WIDTH, HEIGHT)


Die Klasse bietet ebendfalls
.Gravitation (Objekte können dem Stack zugefügt werden (Version 0.3, zu gravierende Objekte können dem Stack beschickt werden))

.Collision(Zu kollidierende Objekte können dem Stack beschickt werden)

Version 0.2, Coded by 3r0rXx

NEUES UPDATE (Version 0.3.1)!!!

Nun wurde NewVector2 mit multi-Tastenabfragen beschickt!
Das heißt,
W und D oder W und A oder S und D oder S und A , und Space und D oder Space und A können gleichzeitig ausgewertet werden!

Ein Beispielprojekt:
http://chatblubber.do.ohost.de/BeispielProjekt.rar

//Coded by 3r0rXx
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;


public class GameEngine2DPreAlpha
{
	//ToDo
	//Gravitation -Done 
	//Charackter_Drawing -(Version 1 -Done)
	//Collision -(Version 1 -Done)
	//TileMapping
	//Pathfinding(Will implemented soon)

	//---Gravitation---
	public int PositionXOfObj;
	public int PositionYOfObj;
	public Stack<Rectangle> StackOfGravitationObj = new Stack<Rectangle>();
	bool Gravitate_bool = true;

	bool Pressed = true;
	//---Collision---

	public Stack<Rectangle> StackOfCollisionObj = new Stack<Rectangle>();
	//---Drawing---

	Bitmap ObjTextur2D;
	//---Local_declarations_gravitation---
	int ReachedFullSizeBuffer;
	bool Gravitation_IsTrue;

	double BufferSpeed = 2;
	//---Local_PosBitmap---
	Bitmap render_btm;
	Rectangle ToLoadOn = new Rectangle(0, 0, 0, 0);
	int localBufferSize;
	Graphics GraphicsLoadContent;

	bool Loaded;
	//---Content---

	Stack<string> AddContentStack = new Stack<string>();
	//---BoolsControl---
	bool w;
	bool s;
	bool d;
	bool a;

	bool Space;

	private void Gravitation_bool_()
	{

		if (Pressed) {
		} else {
			Pressed = true;

			Gravitate_bool = false;
		}
	}


	public void AddContent(string Textur2D)
	{
		AddContentStack.Push(Textur2D);
	}
	public void Gravitation(Rectangle RecToGravitate, double gravitation_speed, double JumpSpeed, Point MaxFallingY, int MaximumJump)
	{
		foreach (Rectangle recs in StackOfGravitationObj) {
			if (RecToGravitate.IntersectsWith(recs)) {
				Gravitation_IsTrue = true;

				break; // TODO: might not be correct. Was : Exit For
			} else {
				Gravitation_IsTrue = false;
			}
		}

		if (Gravitate_bool) {
			if (Gravitation_IsTrue || PositionYOfObj >= MaxFallingY.Y) {
				BufferSpeed = 1;
				Pressed = false;
			} else {
				BufferSpeed += gravitation_speed;
				PositionYOfObj = Convert.ToInt32(PositionYOfObj + BufferSpeed);
			}
		} else if (ReachedFullSizeBuffer > MaximumJump) {
			Gravitate_bool = true;
			ReachedFullSizeBuffer = 0;
		} else {
			ReachedFullSizeBuffer += 1;
			PositionYOfObj = Convert.ToInt32(PositionYOfObj - JumpSpeed);
		}

	}

	public void LoadContentsTextur2D(Form Form, int LoopSize)
	{
		//ToDo:
		//Loading tiles
		try {
			while (true) {
				Application.DoEvents();
				foreach (string path in AddContentStack) {
					try {
						render_btm = new Bitmap(path);

					} catch {
						AddContentStack.Clear();
						Loaded = false;
						Form.Invalidate();
						MessageBox.Show("GDI+ Error: Content(" + path + ")wurde entladen,(Beschreibung : " + Err().GetException().ToString());

					}

				}

				if (!Loaded) {
					if (localBufferSize >= LoopSize) {
						Cursor.Hide();
						Loaded = true;

					} else {
						GraphicsLoadContent = Graphics.FromHwnd(Form.Handle);
						GraphicsLoadContent.DrawImage(render_btm, ToLoadOn);
						localBufferSize += 1;
					}

				} else {
					AddContentStack.Clear();
					Form.Invalidate();
					break; // TODO: might not be correct. Was : Exit Do
				}
			}
		} catch {
			Loaded = false;
		}


	}
	public void Unload(Form Form)
	{
		if (Loaded) {
			Loaded = false;
			Form.Invalidate();
			Environment.Exit(0);
		} else {
			MessageBox.Show("Unloading error: No content was loaded.");
		}

	}
	public void NewVector(Graphics Graphics, string TexturPath2D, int X, int Y, int Width, ref int Height)
	{
		if (Loaded) {
			ObjTextur2D = new Bitmap(TexturPath2D);
			Graphics.DrawImage(ObjTextur2D, new Rectangle(X, Y, Width, Height));
		} else {
		}
	}
	public void Vector(Graphics Graphics, string TexturPath2D, Rectangle Rectangle)
	{
		if (Loaded) {
			ObjTextur2D = new Bitmap(TexturPath2D);
			Graphics.DrawImage(ObjTextur2D, Rectangle);
		} else {
		}
	}
	public void Collision(int X, int Y, int Width, ref int Height, double recspeed)
	{
		foreach (Rectangle collisiobobjrecs in StackOfCollisionObj) {

			if (collisiobobjrecs.IntersectsWith(new Rectangle(X, Y, Width, Height))) {
				break; // TODO: might not be correct. Was : Exit For

			} else {
				PositionXOfObj = Convert.ToInt32(PositionXOfObj + recspeed);

			}
		}
	}
	public void NewVector2(char Keys, double Speed)
	{
		//To activate Bools
		//W
		//S
		//D
		//A

		//To able Bools
		//w
		//s
		//d
		//a

		//Active
		//W activating
		if (Keys == 'W') {
			s = false;
			w = true;
		}
		if (Keys == 'S') {
			w = false;
			s = true;
		}
		if (Keys == "D") {
			a = false;
			d = true;
		}
		if (Keys == "A") {
			d = false;
			a = true;
		}
		if (Keys == ';') {
			w = false;
			s = false;
			Space = true;
		}

		//Inactive

		//W activating
		if (Keys == 'w') {
			w = false;
		}
		if (Keys == 's') {
			s = false;
		}
		if (Keys == "d") {
			d = false;
		}
		if (Keys == "a") {
			a = false;
		}
		if (Keys == ',') {
			Space = false;
		}

		//---Result---

		if (w & d | d & w) {
			PositionXOfObj += Convert.ToInt32(Speed);
			PositionYOfObj -= Convert.ToInt32(Speed);
		} else if (w) {
			PositionYOfObj -= Convert.ToInt32(Speed);
		} else if (d) {
			PositionXOfObj += Convert.ToInt32(Speed);
		}
		if (w & a | a & w) {
			PositionYOfObj -= Convert.ToInt32(Speed);
			PositionXOfObj -= Convert.ToInt32(Speed);
		} else if (w) {
			PositionYOfObj -= Convert.ToInt32(Speed);
		} else if (a) {
			PositionXOfObj -= Convert.ToInt32(Speed);
		}

		if (s & d | d & s) {
			PositionXOfObj += Convert.ToInt32(Speed);
			PositionYOfObj += Convert.ToInt32(Speed);
		} else if (s) {
			PositionYOfObj += Convert.ToInt32(Speed);
		} else if (d) {
			PositionXOfObj += Convert.ToInt32(Speed);
		}
		if (s & a | a & s) {
			PositionXOfObj -= Convert.ToInt32(Speed);
			PositionYOfObj += Convert.ToInt32(Speed);
		} else if (s) {
			PositionYOfObj += Convert.ToInt32(Speed);
		} else if (a) {
			PositionXOfObj -= Convert.ToInt32(Speed);
		}

		if (Space & d | d & Space) {
			PositionXOfObj += Convert.ToInt32(Speed);
			Gravitation_bool_();
		} else if (Space) {
			Gravitation_bool_();
		} else if (d) {
			PositionXOfObj += Convert.ToInt32(Speed);
		}
		if (Space & a | a & Space) {
			PositionXOfObj -= Convert.ToInt32(Speed);
			Gravitation_bool_();
		} else if (Space) {
			Gravitation_bool_();
		} else if (a) {
			PositionXOfObj -= Convert.ToInt32(Speed);
		}
	}
}
//PreAlpha Version 0.2
//Coded by 3r0rXx
//----------------------------------------
//          Verwendung(VB.NET)             
//----------------------------------------
'Coded by 3r0rXx
Imports _2D_Engine_PreAlpha__Test
Public Class GameEngine2D_Using
    Dim GameEngine As New GameEngine2DPreAlpha
    'Position of Charackter
    Dim Player As Rectangle
    Dim PosX As Integer
    Dim PosY As Integer
    Dim graviate As New Rectangle(500, 530, 120, 30)
    Dim Char_ As Char = "_"c
    Dim FPS As Integer
    Private Sub Unload(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        GameEngine.Unload(Me)
    End Sub

    Private Sub GameEngine2D_Using_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.D
                Char_ = "D"c
                GameEngine.NewVector2("D"c, 3.2)
            Case Keys.A
                Char_ = "_"c
                GameEngine.NewVector2("A"c, 3.2)
            Case Keys.Space
                GameEngine.NewVector2(";"c, 3.2)
        End Select
        PosX = GameEngine.PositionXOfObj
        PosY = GameEngine.PositionYOfObj
    End Sub

    Private Sub GameEngine2D_Using_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Select Case e.KeyCode

            Case Keys.D
                GameEngine.NewVector2("d"c, 3.2)
            Case Keys.A
                GameEngine.NewVector2("a"c, 3.2)
            Case Keys.Space
                GameEngine.NewVector2(","c, 3.2)
        End Select
    End Sub
    Private Sub LoadTextures_Load() Handles MyBase.Load

        'IMPORTANT: YOU HAVE TO REFER TO 2D-Engine(PreAlpha)-Test.dll
        'Write here the contents to load
        'It must be an 2 Texture (GameEngine.AddContent("TEXTUR2D"))

        'WRITE HERE ALL CONTENTS(Optional,recommended)

        'Loading of all contents in stack

        GameEngine.LoadContentsTextur2D(Me, 0)
        'YOU CAN WRITE CODE HERE
        GameEngine.StackOfGravitationObj.Push(graviate)
    End Sub
    Private Sub GameLoop()
        Do While True
            Application.DoEvents()
            'Write here code to loop
            GameEngine.Gravitation(Player, 0.01, 1, New Point(Player.Location.X, 550), 130)
            PosY = GameEngine.PositionYOfObj
            Me.Invalidate()
        Loop
    End Sub
    Public Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

        'Loading of the GameLoop
        Me.Show()
        Me.Focus()
        Me.Icon = My.Resources.Game
        Me.Text = Nothing
        Me.Size = New Size(1200, 700)
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
        Me.Location = New Point(0, 0)
        GameLoop()
    End Sub

    Dim Sekunde As Integer = TimeOfDay.Second
    Dim Ticks As Integer = 0
    Dim Resultat As Integer = 0
    Private Sub TickCounter()
        If Sekunde = TimeOfDay.Second = True Then
            Ticks += 1
        Else
            Resultat = Ticks
            Ticks = 0
            Sekunde = TimeOfDay.Second
        End If
        FPS = Resultat
    End Sub
    Private Sub DrawContent(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        'IMPORTANT
        DoubleBuffered = True
        'OPTIONAL
        e.Graphics.Clear(Color.CornflowerBlue)
        'DRAWING OF LOADED CONTENT
        'Call the method (NewVector) to draw a new 2D-Point
        'Set e.graphics as renderer
        GameEngine.Vector(e.Graphics, Application.StartupPath & "\Textures\Top.png", graviate)
        Player = New Rectangle(PosX, PosY, 45, 120)
        Select Case Char_
            Case "D"c
                GameEngine.Vector(e.Graphics, Application.StartupPath & "\Textures\Chr2.png", Player)
            Case "_"c
                GameEngine.Vector(e.Graphics, Application.StartupPath & "\Textures\Chr1.png", Player)

        End Select
        e.Graphics.DrawString(FPS & " Fps", New Font("Arial", 12), Brushes.Red, New PointF(0, 0))
        TickCounter()
    End Sub
End Class
'Coded by 3r0rXx
Abgelegt unter C#, Engine2d, 3r0rXx, 2DEngineVB, 2DEngineC#.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!