PDA

View Full Version : My first game test


kcd-clan
02-15-2008, 11:44 AM
Finaly I have somthing.
Not much.
Char bounds and animation ... jsut really basic stuff.
Mario base stuff since it takes to much time to make my own spirits and stuff.
http://files.filefront.com/Mario+learning+Debugrar/;9636212;/fileinfo.html
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectDraw;
using Microsoft.DirectX.AudioVideoPlayback;

namespace game
{
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
///
public Device dddevice = null;
public Surface psurface; //Primary Surface ( user visible)
public Surface ssurface; // Secondary surface ( Back buffer)
public Clipper clip = null;
public bool enabled = true;
int tWidth = Screen.PrimaryScreen.Bounds.Width;
int tHeight = Screen.PrimaryScreen.Bounds.Height;
public int i = 0;
public int tick1 =1;
public int clock = 0;
int ground = 784;
public bool running = false;
// for character
public Surface charsurface; // Image surface
Rectangle[] frames = new Rectangle[17];
public int frm4act = 0;
public int xchr, ychr = 600;
private ColorKey ck;
public int chardir = 1;
//for background
public Rectangle backrect;
public Surface backsurface = null;
public int xaxis;
// for sound
Audio titlesound;
public string walkdir = "None";
private Timer ms;
private Timer seconds;
private Timer jump;
private IContainer components;
public Form1()
: base()
{
InitializeComponent();
this.Show();
this.Load += new System.EventHandler(this.GFocus);
dddevice = new Device();
dddevice.SetCooperativeLevel(this, CooperativeLevelFlags.FullscreenExclusive);
createframes();
Background();
titlesound = new Audio("smwwd1.wma", true);
Init();
Gameloop();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.ms = new System.Windows.Forms.Timer(this.components);
this.seconds = new System.Windows.Forms.Timer(this.components);
this.jump = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// ms
//
this.ms.Enabled = true;
this.ms.Interval = 10;
this.ms.Tick += new System.EventHandler(this.timer1_Tick);
//
// seconds
//
this.seconds.Enabled = true;
this.seconds.Interval = 1000;
this.seconds.Tick += new System.EventHandler(this.seconds_Tick);
//
// jump
//
this.jump.Interval = 50;
this.jump.Tick += new System.EventHandler(this.jump_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.movechr);
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.f_keyup);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.f_keydown);
this.ResumeLayout(false);
}
#endregion
public void GFocus(object sender, System.EventArgs e)
{
ending();
Form1 newform = new Form1();
}
public void Init()
{
psurface = null;
ssurface = null;
SurfaceDescription sdesc = new SurfaceDescription();
Clipper clip = new Clipper(dddevice);
clip.Window = this;
sdesc.SurfaceCaps.PrimarySurface = true;
sdesc.SurfaceCaps.Flip = true;
sdesc.SurfaceCaps.Complex = true;
sdesc.BackBufferCount = 1;
psurface = new Surface(sdesc, dddevice);
psurface.Clipper = clip;
sdesc.Clear();
sdesc.SurfaceCaps.BackBuffer = true;
ssurface = psurface.GetAttachedSurface(sdesc.SurfaceCaps);
sdesc.Clear();
sdesc.SurfaceCaps.OffScreenPlain = true;
ck.ColorSpaceHighValue = Color.Lime.ToArgb();//RGB(0, 255, 0);
ck.ColorSpaceLowValue = Color.Lime.ToArgb();
charsurface = new Surface("character.bmp", sdesc, dddevice);
charsurface.SetColorKey(ColorKeyFlags.SourceDraw, ck); //set the color key for the surface
}
private void Gameloop()
{
do
{
if (!this.Focused)
Application.DoEvents();
else
ShowSurface();
Application.DoEvents();
} while (this.Created);
ending();
}
public void ShowSurface()
{
try
{
ssurface.ColorFill(Color.Blue);
backrect.X = xaxis;
backrect.Y = 0;
ssurface.DrawFast(0, 0, backsurface, backrect, DrawFastFlags.DoNotWait);
ssurface.DrawText(10, 10, walkdir, false);
ssurface.DrawText(tWidth-30, 10, clock.ToString(), false);
ssurface.DrawText(250, 10, "X-" + xchr + " - " + "Y-" + ychr, false);

ssurface.DrawFast(xchr, ychr, charsurface, frames[frm4act], DrawFastFlags.SourceColorKey);
psurface.Flip(ssurface, FlipFlags.Wait);
}
catch
{
psurface.Dispose();
Init();
}
}
public void ending()
{
dddevice.Dispose();
psurface.Dispose();
ssurface.Dispose();
//clip.Dispose();
}

public void createframes()
{
int x = 0;
for (x = 0; x < 17; x++)
{

frames[x] = new Rectangle(16 * x,0, 16, 28);
}



}
private void Background()
{
SurfaceDescription sdesc = new SurfaceDescription();
sdesc.SurfaceCaps.OffScreenPlain = true;
backsurface = new Surface("bg.bmp", sdesc, dddevice);
backrect.Size = new Size(tWidth,tHeight);
}
private void movechr(int dir)
{
switch (dir)
{
case 1:
running = true;
walkdir = "Right" + frm4act;
chardir = 1;
frm4act = frm4act + 1;
xchr += 2;//moves x pos +
if (frm4act == 3)
frm4act = 0;
break;
case 2:
running = true;
walkdir = "Left" + frm4act;
chardir = 2;
frm4act = frm4act + 1;
xchr -= 2;//moves x pos -
if (frm4act == 10)
frm4act = 8;
break;
case 3:
walkdir = "Jump" + frm4act;

if (chardir == 1)
{
frm4act = 5;
}
else
{
frm4act = 13;
}
//moves x pos -
while (i < 5)
{
i++;
if (running == true)
{
if (chardir == 1)
{
xchr += 4;
}
else
{
xchr -= 4;
}
}
ychr -= 4;
}
i = 0;
break;
case 4:
walkdir = "Duck" + frm4act;
if (chardir == 1)
{
frm4act = 6;
}
else
{
frm4act = 14;
}
tick1 = 0;
break;
}

}
private void f_keydown(Object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
ending();
Application.Exit();
}
if (enabled == true)
{

if (e.KeyCode == Keys.Right)
{
movechr(1);
}
if (e.KeyCode == Keys.Left)
{
movechr(2);
}
if (e.KeyCode == Keys.Space)
{
movechr(3);
}
if (e.KeyCode == Keys.Down)
{
movechr(4);
}
}
}
private void f_keyup(Object sender, System.Windows.Forms.KeyEventArgs e)
{

walkdir = "---- " + frm4act;
running = false;
if (chardir == 1)
{
frm4act = 0;
}
else
{
frm4act = 8;
}

}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
tick1++;
if (ychr != ground)
ychr = ychr + 1;
if (xchr < 5)
xchr += 10;
if (xchr > tWidth-20)
xchr -= 10;
if (ychr < 5)
ychr += 10;
if (ychr > tHeight - 30)
ychr = ground;
if (frm4act > 16)
frm4act = 0;

if (ychr != ground)
{
enabled = false;
}
else
{
enabled = true;
}
}
private void seconds_Tick(object sender, EventArgs e)
{
clock++;
if (clock == 999)
clock = 0;
}
private void movechr(object sender, MouseEventArgs e)

{
xchr = MousePosition.X;
ychr = MousePosition.Y;
}
private void jump_Tick(object sender, EventArgs e)
{

}

}
}

If you have any sugestions on source let me know.
I barly understand what i have down.

CGTalk Moderation
02-15-2008, 11:44 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.