check_command in .txt file [SOLVED]

Making my program react on certain commands in the .txt file

Page 1 of 1

0 Replies - 862 Views - Last Post: 25 January 2010 - 07:55 PM Rate Topic: -----

#1 takex   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-June 09

check_command in .txt file [SOLVED]

Posted 25 January 2010 - 07:55 PM

Hello!

To display the story of the storytelling game I am currently programming I am using this code:

	   
	 StreamReader str = new StreamReader("storyboard.txt", System.Text.Encoding.Default);
			string msg;
			while ((msg = str.ReadLine()) != null)
			{
				msglist.Add(msg);
			}



It is working just fine and together with

  private void T_On()
		{
			int FPS = 60;								
			int frame = 0;							   
			int before = Environment.TickCount;	   
			while (!IsDisposed)	 
			{
				int now = Environment.TickCount;		   
				int progress = now - before;			   
				int ideal = (int)(frame * (1000.0f / FPS)); 
  
				if (ideal > progress) Thread.Sleep(ideal - progress);
				Invalidate();				
				frame++;			  
				if (progress > 1000)  
				{
					before = now;		 
					frame = 0;			  
				}
			}
		}



With this I got a story line that prints itself out like someone is currently typing it up while playing (like a normal japanese RPG novelstory game). I have also added the touch that when the mousebutton is pressed, the text speeds up.

However, it does only display the first paragraph (where the player is asked to type in his name and the name is stored in a player class I made) what I want to happen next is a next button so that new text will show up the same slow typing way.

My teacher told me last week that this was done through scripts and then we ran out of time, so I have been trying to figure it out myself discovering that C# workis with XML scripting but somehow I can't manage to google or find tutorials that match what I want. On top of it all, our class today is a selfclass as our teacher appears to be abscent.

Is the script way the way to go to get what I want? I don't know how to use XML but if I am able to find a basic tutorial I believe I'd be able to figure it out.

All I got so far is this

<?xml version="1.0" encoding="utf-8" ?>
<Story>
<Next>
  
</Next>
</Story>



I am guessing (with the zero knowledge I have right now) that I am going to be telling the script to read until a certain stop, await a button click of some sort then read to next "stop". Is this correct?

Would you guys with more knowledge and experience go about the same way to do this? If not maybe I could get some tips on commandos to use instead?

Thank you in advance and very sorry if the post got a little long and messy. I try my best not to talk in circles.


This is my whole coding and when I thought I had the solution it just wont work. The text till wont stop on the "#K" in the .txt.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace _20
{
	public partial class StartGame : Form
	{

		// Game ints
		/*
		int HP = 100;
		int XP = 0;
		*/
		public player pl = null;

		// Text speed
		List<string> msglist = new List<string>();
		List<Rectangle> selBox = new List<Rectangle>();

		bool mouseLeftPress = false;
		bool selectFlag = false;
		bool keyPressFlag = false;

		Font font;

		int ftime = 0;
		int fontSpeed = 10;
		int startPos = 0;			  
		int mtimer = 0;
		int mtimeMax = 10;
		int nowLine = 0;
		int lineStart = 0;
		int lineNum = 0;
		int selJumpNum = 0;

		Bitmap bmp = null;
		Bitmap bgbmp = null;
		Bitmap msgBmp = null;
		Rectangle rect;
		ImageAttributes msgattr = null;


		public StartGame()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
						ControlStyles.AllPaintingInWmPaint, true);
			this.ClientSize = new Size(640, 480);
			this.FormBorderStyle = FormBorderStyle.FixedSingle;
			font = new Font("MS Gothic", 12f);

			pl = new player(this);

			// attributes
			bmp = new Bitmap(640, 480);
			bgbmp = new Bitmap("back2.png");
			msgBmp = new Bitmap(600, 440);
			Brush brush = new SolidBrush(Color.Black);
			Graphics g = Graphics.FromImage(msgBmp);
			g.FillRectangle(brush, 0, 0, 600, 440);
			msgattr = new ImageAttributes();
			rect = new Rectangle(20, 20, 600, 440);

			// attributes stop

			//Get storyboard
			StreamReader str = new StreamReader("storyboard.txt", System.Text.Encoding.Default);
			string msg;
			while ((msg = str.ReadLine()) != null)
			{
				msglist.Add(msg);
			}

			Thread thread = new Thread(new ThreadStart(T_On));
			thread.Start();


		}

		private void Form1_Paint(object sender, PaintEventArgs e)
		{
			e.Graphics.DrawImage(bmp, 0, 0);
			Render(e.Graphics);
		}

		private void T_On()
		{
			int FPS = 60;								
			int frame = 0;							   
			int before = Environment.TickCount;	   
			while (!IsDisposed)	 
			{
				int now = Environment.TickCount;		   
				int progress = now - before;			   
				int ideal = (int)(frame * (1000.0f / FPS)); 
  
				if (ideal > progress) Thread.Sleep(ideal - progress);
				Invalidate();				
				frame++;			  
				if (progress > 1000)  
				{
					before = now;		 
					frame = 0;			  
				}
			}
		}

		private void Render(Graphics g)
		{
			SolidBrush brush = new SolidBrush(Color.Black);
			Graphics g = Graphics.FromImage(bmp);
			Graphics msggr = Graphics.FromImage(msgBmp);
			ClearScreen(g);

			g.DrawImage(bgbmp, 0, 0);
			g.DrawImage(msgBmp, rect, 0, 0, 600, 440, GraphicsUnit.Pixel, msgattr);

			setStr(msggr);

			if (nowLine < msglist.Count)
				drawMessage();
			if (selKey()) return;
			if (selCsr()) return;
			if (command(g)) return;
			ClearScreen(msggr);
			stringStep();
		}

				private bool command(Graphics g)
		{
			if (msglist.Count > lineNum)
			{
				while (msglist[lineNum].IndexOf("#") == 0)
				{
					if (msglist[lineNum].IndexOf("#L") >= 0)
					{
						lineNum++;
						lineStart = lineNum;
						continue;
					}
				   if (msglist[lineNum].IndexOf("#J") >= 0)
				   checkJCommand();
					if (msglist[lineNum].IndexOf("#K") >= 0)
					{
						checkKCommand();
						return true;
					}
				 if (msglist[lineNum].IndexOf("#S") >= 0)
				  {
					   checkSCommand(g);
						return true;
					}
				}
			}

			mouseLeftPress = false;
			return false;
				}


		 private void checkJCommand()
		{
			int startLine = 0;
			int chkNum;
			int jNum;
			string msg = msglist[lineNum].Substring(2, 3);
			chkNum = int.Parse(msg);
			while (true)
			{
				if (msglist[startLine].IndexOf("#J") < 0)
				{
					startLine++;
					continue;
				}
				msg = msglist[startLine].Substring(2, 3);
				jNum = int.Parse(msg);
				if (chkNum == jNum)
				{
					lineNum = startLine + 1;
					break;
				}
			}
			lineStart = lineNum;
		}
		private void checkKCommand()
		{
			keyPressFlag = true;
		}
		private void checkSCommand(Graphics g)
		{
			int selNum;
			string msg = msglist[lineNum].Substring(2, 1);
			selNum = int.Parse(msg);
			lineNum++;
			selBox.Clear();
			int i;
			for (i = lineNum; i < lineNum + selNum; i++)
			{
				drawString(g, msglist[i], 100, (i - lineStart) * this.Font.Height);
				Rectangle rect = new Rectangle(100,(i-lineStart)*this.Font.Height,100,this.Font.Height);
				selBox.Add(rect);
			}
			selJumpNum = 0;
			lineNum += selNum;
			selectFlag = true;
		}

				private bool selCsr()
		{
			if (selectFlag)
			{
				if (!mouseLeftPress)
					return true;
				mouseLeftPress = false;
				selectFlag = false;
				lineNum += selJumpNum;
			}
			return false;
		}
		private bool selKey()
		{
			if (keyPressFlag)
			{
				if (!mouseLeftPress)
					return true;
				keyPressFlag = false;
				mouseLeftPress = false;
				lineNum++;
				lineStart = lineNum;
				fontSpeed = 10;
			}
			return false;
		}


		private void clearBmp(Bitmap bitmap)
		{

			Color col = Color.FromArgb(0, 0, 0, 0);
			int x, y;
			for (y = 0; y < bitmap.Height; y++)
			{
				for (x = 0; x < bitmap.Width; x++)
				{
					bitmap.SetPixel(x, y, col);
				}
			}
		}

		private void setStr(Graphics g)
		{
			if ((msglist.Count > lineNum) && (msglist[lineNum].IndexOf("#K") < 0))
			{
				int i;
				for (i = lineStart; i <= lineNum; i++)
				{
					if (i != lineNum)
						drawString(g, msglist[i], 100, (i - lineStart) * this.Font.Height);
					else
					{
						string mess = msglist[i].Substring(0, startPos);
						drawString(g, mess, 100, (i - lineStart) * this.Font.Height);
					}
				}

				ftime++;
				if (ftime > fontSpeed)
				{
					if (msglist[lineNum].Length > startPos)
					{
						startPos++;
					}
					else
					{
						startPos = 0;
						lineNum++;
					}
					ftime = 0;
				}
			}
			else
			{
				int i;
				for (i = lineStart; i < lineNum; i++)
				{
					if (msglist[i].IndexOf("#K") >= 0)
						break;
					drawString(g, msglist[i], 100, (i - lineStart) * this.Font.Height);
				}
			}
		}

				private void drawString(Graphics g, string msg, int x, int y)
		{
			Brush brush = new SolidBrush(Color.White);
			g.DrawString(msg, this.Font, brush, x, y + 100);


		}

				private void ClearScreen(Graphics g)
				{
					Brush brush = new SolidBrush(Color.Black);
					g.FillRectangle(brush, 0, 0, 640, 480);
				}

		private void clearLine(Bitmap bitmap)
		{
			if (nowLine < msglist.Count)
			{

				Color col = Color.FromArgb(0, 0, 0, 0);
				int x, y;
				for (y = 0; y < font.Height; y++)
				{
					for (x = 0; x < bitmap.Width; x++)
					{
						bitmap.SetPixel(x, y + nowLine * font.Height, col);
					}
				}
			}
		}

		private void drawMessage()
		{
			Graphics gr = Graphics.FromImage(bmp);
			string moji = msglist[nowLine].Substring(0, startPos);
			SolidBrush brush = new SolidBrush(Color.White);
			gr.DrawString(moji, font, brush, 0, nowLine * font.Height);
		}

		private void stringStep()
		{
			if (nowLine < msglist.Count)
			{

				if (startPos < msglist[nowLine].Length)
				{
					mtimer++;
				   
					if (mtimer >= mtimeMax)
					{
						mtimer = 0;
						startPos++;
					}
				}
				else
				{
					startPos = 0;
					nowLine++;
				}
			}
		}

		private void Form1_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Left)
			{
				mouseLeftPress = true;
				mtimeMax = 0;
			}
		}
	

		private void button1_Click(object sender, EventArgs e)
		{
		   pl.pname = textBox1.Text;
		}

		private void Form1_MouseUp(object sender, MouseEventArgs e)
		{

			if (e.Button == MouseButtons.Left)
			{
				mouseLeftPress = false;
				mtimeMax = 10;
			}
		}

	}
}



It is very possible that I've done something I shouldn't and I am sorry that I don't have any comments in it yet, I should hurry up and get that into my system. If anyone has any idea of what I have done wrong, please give me a hint!

I don't have any idea why I got one error about "g" either. I have been looking through the code for the past hour trying to figure out where I haven't declared or might have messed up "g" for graphics = (
Graphics g = Graphics.FromImage(bmp);
in Void Render)

This post has been edited by takex: 25 January 2010 - 10:31 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1