Click here to Skip to main content
16,005,222 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to split work into multiple threads. Pin
kripzz2-Jun-07 20:25
kripzz2-Jun-07 20:25 
GeneralRe: How to split work into multiple threads. Pin
Expert Coming2-Jun-07 20:29
Expert Coming2-Jun-07 20:29 
AnswerRe: How to split work into multiple threads. Pin
Christian Graus2-Jun-07 20:32
protectorChristian Graus2-Jun-07 20:32 
AnswerRe: How to split work into multiple threads. Pin
Luc Pattyn3-Jun-07 2:34
sitebuilderLuc Pattyn3-Jun-07 2:34 
AnswerRe: How to split work into multiple threads. Pin
Robert Rohde3-Jun-07 2:57
Robert Rohde3-Jun-07 2:57 
GeneralRe: How to split work into multiple threads. Pin
kripzz11-Jun-07 16:39
kripzz11-Jun-07 16:39 
AnswerRe: How to split work into multiple threads. Pin
Hesham Yassin6-Jun-07 8:32
Hesham Yassin6-Jun-07 8:32 
QuestionHmmm why won't this work? Pin
mfkr2-Jun-07 17:58
mfkr2-Jun-07 17:58 
Alright so I've been working on this zombie simulation for a while and I recently redid it to work better. Problem is when I loop through to draw 1000 zombies on screen it only draws (and moves) a random number from 1 to 4. It is weird...anyways the code is below make someone can tell me why...
<br />
using System; <br />
using System.Drawing; <br />
using System.Drawing.Drawing2D; <br />
using System.Collections; <br />
using System.ComponentModel; <br />
using System.Windows.Forms; <br />
using System.Data; <br />
  <br />
namespace ZombiesSim<br />
{<br />
	/// <summary> <br />
	/// Summary description for Form1. <br />
	/// </summary> <br />
	public class Zombie : System.Windows.Forms.Form <br />
	{<br />
	    private System.ComponentModel.IContainer components;<br />
		<br />
		private Timer timer = new Timer();<br />
		private int freeze;<br />
		private Brush human, panichuman, zombie, wall;<br />
		private int num = 1000;<br />
		private int speed = 1;<br />
		private bool gxstart;<br />
		private Being[] beings;<br />
		private Bitmap world;<br />
		private int width, height;<br />
		private Random rand = new Random();<br />
		private Building[] buildings;<br />
<br />
	  <br />
	    public Zombie() <br />
	    { <br />
			// <br />
			// Required for Windows Form Designer support <br />
			// <br />
			InitializeComponent(); <br />
			<br />
			human = new SolidBrush( Color.Purple );<br />
			panichuman = new SolidBrush( Color.Pink );<br />
			zombie = new SolidBrush( Color.Lime );<br />
			wall = new SolidBrush( Color.DimGray );<br />
			<br />
			width = this.ClientSize.Width;<br />
			height = this.ClientSize.Height;<br />
			<br />
			world = new Bitmap( width, height );<br />
			gxstart = true;<br />
			<br />
			beings = new Being[4000];<br />
			<br />
			for( int i = 0; i < 1000; i++ )<br />
			{<br />
				beings[i] = new Being( width, height, zombie );<br />
			}<br />
			<br />
			buildings = new Building[130];<br />
			<br />
			for( int i = 0; i < 100; i++ )<br />
			{<br />
				buildings[i] = new Building( rand.Next( width ), rand.Next( height ), rand.Next( 30, 90 ), rand.Next( 30, 90 ), wall );<br />
			}<br />
			<br />
			for( int i = 100; i < 130; i++ )<br />
			{<br />
				buildings[i] = new Building( rand.Next( width ), rand.Next( height ), rand.Next( 40, 60 ), rand.Next( 40, 60 ), wall );<br />
			}<br />
			<br />
			timer.Tick += new EventHandler( OnTimer );<br />
			timer.Enabled = true;<br />
			timer.Interval = 30;<br />
			<br />
			freeze = 0;<br />
			<br />
			this.SetStyle( ControlStyles.DoubleBuffer, true );<br />
			this.SetStyle( ControlStyles.AllPaintingInWmPaint, true );<br />
			this.SetStyle( ControlStyles.UserPaint, true );<br />
	    }<br />
	  <br />
	    /// <summary> <br />
	    /// Clean up any resources being used. <br />
	    /// </summary> <br />
	    protected override void Dispose( bool disposing ) <br />
	    { <br />
			if( disposing ) <br />
			{ <br />
				if (components != null) <br />
				{ <br />
					components.Dispose(); <br />
				} <br />
			} <br />
			base.Dispose( disposing ); <br />
	    } <br />
	  <br />
	    #region Windows Form Designer generated code <br />
	    /// <summary> <br />
	    /// Required method for Designer support - do not modify <br />
	    /// the contents of this method with the code editor. <br />
	    /// </summary> <br />
	    private void InitializeComponent() <br />
	    { <br />
			this.components = new System.ComponentModel.Container(); <br />
			this.SuspendLayout(); <br />
			// <br />
			// Zombie<br />
			// <br />
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); <br />
			this.ClientSize = new System.Drawing.Size(666, 666); <br />
			this.Name = "Zombie Simulation"; <br />
			this.Text = "Zombie Simulation"; <br />
			this.ResumeLayout(false);<br />
			this.MaximizeBox = false;<br />
			this.FormBorderStyle = FormBorderStyle.FixedDialog;<br />
			this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);<br />
	    }<br />
	    #endregion<br />
		<br />
	    /// <summary><br />
	    /// The main entry point for the application.<br />
	    /// </summary><br />
	    [STAThread]<br />
	    static void Main()<br />
	    {<br />
			Application.Run(new Zombie());<br />
	    }<br />
		<br />
	    private void OnPaint(object sender, PaintEventArgs e)<br />
	    {<br />
			e.Graphics.FillRectangle( new SolidBrush( Color.Black ), 0, 0, width, height );<br />
			<br />
			foreach( Building building in buildings )<br />
			{<br />
				e.Graphics.FillRectangle( building.Color, building.Draw() );<br />
			}<br />
			<br />
			for( int i = 0; i < 1000; i++ )<br />
			{<br />
				e.Graphics.FillRectangle( beings[i].Color, beings[i].Draw() );<br />
			}<br />
			<br />
	    }<br />
		<br />
		private void OnTimer(object obj, EventArgs ea)<br />
		{<br />
			for( int i = 0; i < 1000; i++ )<br />
			{<br />
				beings[i].move(); <br />
			}<br />
			Invalidate( false );<br />
		}<br />
	}<br />
	<br />
	public class Being : Rect<br />
	{<br />
		private int dir, type, active, maxwidth, maxheight;<br />
		private Random rand;<br />
		<br />
		public Being( int height, int width, Brush theHue )<br />
		{<br />
			rand = new Random();<br />
			type = 1;<br />
			active = 0;<br />
			hue = theHue;<br />
			maxwidth = width;<br />
			maxheight = height;<br />
			dir = rand.Next( 1, 4 );<br />
			position();<br />
			rect = new Rectangle( xpos, ypos, 5, 5 );<br />
		}<br />
		<br />
		public void position()<br />
		{<br />
			//for( int j = 0; j < 100; j++ )<br />
			//{<br />
				xpos = rand.Next( 1, maxwidth - 1);<br />
				ypos = rand.Next( 1, maxheight - 1);<br />
				//Color test = world.GetPixel( xpos, ypos );<br />
				//if( test == Color.FromArgb( 0, 0, 0, 0 ) )<br />
			//}<br />
		}<br />
		<br />
		public void infect( int x, int y )<br />
		{<br />
			if( x == xpos && y == ypos )<br />
				type = 2;<br />
		}<br />
		<br />
		public void infect()<br />
		{<br />
			//hue = new SolidBrush( Color.Lime );<br />
			type = 2;<br />
		}<br />
		<br />
		public void uninfect()<br />
		{<br />
			// hue = new SolidBrush( Color.Purple );<br />
			type = 1;<br />
		}<br />
		<br />
		public void look( int x, int y )<br />
		{<br />
			dir = rand.Next( 1, 4 );<br />
			<br />
			if( dir == 1 )<br />
				y++;<br />
			if( dir == 2 )<br />
				x++;<br />
			if( dir == 3 )<br />
				y--;<br />
			if( dir == 4 )<br />
				x--;<br />
		}<br />
		<br />
		public void move()<br />
		{<br />
			rect.Offset( rand.Next( -4, 4 ), rand.Next( -4, 4 ) );<br />
			if( rect.X > 666 )<br />
				rect.X = 664;<br />
			if( rect.Y > 666 )<br />
				rect.Y = 664;<br />
			if( rect.X < 0 )<br />
				rect.X = 0;<br />
			if( rect.Y < 0 )<br />
				rect.Y = 0;<br />
		}<br />
	}<br />
	<br />
	public class Building : Rect<br />
	{<br />
		<br />
		public Building( int x, int y, int randw, int randh, Brush theHue )<br />
		{<br />
			hue = theHue;<br />
			rect = new Rectangle( x, y, randw, randh );<br />
		}<br />
	}<br />
}

AnswerRe: Hmmm why won't this work? Pin
Christian Graus2-Jun-07 19:07
protectorChristian Graus2-Jun-07 19:07 
GeneralRe: Hmmm why won't this work? Pin
mfkr2-Jun-07 19:17
mfkr2-Jun-07 19:17 
GeneralRe: Hmmm why won't this work? Pin
Christian Graus2-Jun-07 19:22
protectorChristian Graus2-Jun-07 19:22 
AnswerRe: Hmmm why won't this work? Pin
Robert Surtees2-Jun-07 19:24
Robert Surtees2-Jun-07 19:24 
GeneralRe: Hmmm why won't this work? Pin
mfkr2-Jun-07 19:32
mfkr2-Jun-07 19:32 
GeneralRe: Hmmm why won't this work? Pin
Christian Graus2-Jun-07 20:33
protectorChristian Graus2-Jun-07 20:33 
GeneralRe: Hmmm why won't this work? Pin
Dave Kreskowiak3-Jun-07 5:07
mveDave Kreskowiak3-Jun-07 5:07 
QuestionCasting generic parameters to int Pin
Jack Valmadre2-Jun-07 15:19
Jack Valmadre2-Jun-07 15:19 
AnswerRe: Casting generic parameters to int Pin
mikker_1232-Jun-07 16:29
mikker_1232-Jun-07 16:29 
GeneralRe: Casting generic parameters to int Pin
Jack Valmadre2-Jun-07 17:27
Jack Valmadre2-Jun-07 17:27 
GeneralRe: Casting generic parameters to int Pin
Daniel Grunwald3-Jun-07 2:13
Daniel Grunwald3-Jun-07 2:13 
GeneralRe: Casting generic parameters to int Pin
mikker_1233-Jun-07 4:37
mikker_1233-Jun-07 4:37 
GeneralRe: Casting generic parameters to int Pin
Jack Valmadre3-Jun-07 10:34
Jack Valmadre3-Jun-07 10:34 
QuestionGetting to the encapsulated class within a ServiceHost object? Pin
LongRange.Shooter2-Jun-07 11:36
LongRange.Shooter2-Jun-07 11:36 
QuestionHow can you adjust the maximum window size for Word, IE, Notepad, etc.??? Pin
RedPocket2-Jun-07 8:11
RedPocket2-Jun-07 8:11 
QuestionAutomatically adding dll file to publish without reference Pin
jayrooney072-Jun-07 5:56
jayrooney072-Jun-07 5:56 
AnswerRe: Automatically adding dll file to publish without reference Pin
mikker_1232-Jun-07 16:23
mikker_1232-Jun-07 16:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.