| | |
direct robot
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 77
Reputation:
Solved Threads: 0
Hi, been working on this project and just confusing the heck out of myself with classes and stuff and was hoping maybe someone could lend a hand on what I might be doing wrong?
The general idea of this is to get the arrow (int 231-234) to point in the direct that the use clicked (n/s/w/e) and then when they press go 1 or go 10 it goes the direction selected and go either 1 or 10. But I have to limit it from going over 100 in any direct and show a message that they can't do that. And on load it set to 0,0 and set to north which I believe I have figured already.
General I'm confused on the class\method stuff, which is below, I think I got it right, but for some reason on my form I can't get it to find the getDirection function just errors up on all that.
I'm not sure if I coded something wrong or what, suggested hints or tricks would be helpful.
Thanks in advance.
Main Form:
robot.cs
The general idea of this is to get the arrow (int 231-234) to point in the direct that the use clicked (n/s/w/e) and then when they press go 1 or go 10 it goes the direction selected and go either 1 or 10. But I have to limit it from going over 100 in any direct and show a message that they can't do that. And on load it set to 0,0 and set to north which I believe I have figured already.
General I'm confused on the class\method stuff, which is below, I think I got it right, but for some reason on my form I can't get it to find the getDirection function just errors up on all that.
I'm not sure if I coded something wrong or what, suggested hints or tricks would be helpful.
Thanks in advance.
Main Form:
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace simple_robot { public partial class Robot : Form { Robot myRobot = new Robot(); public Robot() { InitializeComponent(); pointer.Text = myRobot.getDirection().ToString(); } private void button3_Click(object sender, EventArgs e) { this.Close(); } private void label1_Click(object sender, EventArgs e) { } private void robotbox_SelectedIndexChanged(object sender, EventArgs e) { } private void label1_Click_1(object sender, EventArgs e) { } private void northBtn_Click(object sender, EventArgs e) { myRobot.setDirection(231); pointer.Text = myRobot.getDirection().ToString(); } private void westBtn_Click(object sender, EventArgs e) { myRobot.SetDirection(232); pointer.Text = myRobot.getDirection().ToString(); } private void southBtn_Click(object sender, EventArgs e) { myRobot.SetDirection(233); pointer.Text = myRobot.getDirection().ToString(); } private void eastBtn_Click(object sender, EventArgs e) { myRobot.SetDirection(234); pointer.Text = myRobot.getDirection().ToString(); } private void label1_Click_2(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { this.OnClick = myRobot.getX(myRobot.getX() + 1); } private void button2_Click(object sender, EventArgs e) { this.OnClick = myRobot.getX(myRobot.getX() + 10); } } }
robot.cs
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace simple_robot { class robot { private int direction; private int x; private int y; public void setDirection(int newDirection) { direction = newDirection; } public int getDirection(int direction) { return direction; } public int getX() { return x; } public int getY() { return y; } public void move(int unitToMove) { } public robot() { x = 0; y = 0; direction = 231; } } }
Last edited by EvilLinux; Sep 25th, 2009 at 2:47 pm.
Whew!
Just going to concentrate on your robot class here.
Find out what automatic properties are.
I rewrote it so:
Use this class like this:
Just going to concentrate on your robot class here.
Find out what automatic properties are.
I rewrote it so:
c# Syntax (Toggle Plain Text)
class robot { public robot() // Default constructor { location = new Point(); // Sets location.X=0 and location.Y=0 direction = 231; } public int direction { get; set; } //auto property public Point location { get; set; } //auto property public void move(int unitToMove) { } }
c# Syntax (Toggle Plain Text)
robot Robby = new robot(); Point P = new Point(); P = Robby.location; // Get location of Robby Point P2 = new Point(); P2.X = 23; P2.Y = 100; Robby.location = P2; // Set location of Robby
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Sep 2009
Posts: 77
Reputation:
Solved Threads: 0
•
•
•
•
Whew!
Just going to concentrate on your robot class here.
Find out what automatic properties are.
I rewrote it so:
Use this class like this:c# Syntax (Toggle Plain Text)
class robot { public robot() // Default constructor { location = new Point(); // Sets location.X=0 and location.Y=0 direction = 231; } public int direction { get; set; } //auto property public Point location { get; set; } //auto property public void move(int unitToMove) { } }
c# Syntax (Toggle Plain Text)
robot Robby = new robot(); Point P = new Point(); P = Robby.location; // Get location of Robby Point P2 = new Point(); P2.X = 23; P2.Y = 100; Robby.location = P2; // Set location of Robby
Thanks, although confused on the second part of the code where do you work with that, I'm guessing the form itself?
Last edited by EvilLinux; Sep 25th, 2009 at 4:05 pm.
•
•
Join Date: Sep 2009
Posts: 77
Reputation:
Solved Threads: 0
ok cool, I'm going to keep trying to solve the project with the C# 2.0 way because idk what is required for the assignment, I'm figuring follow the way the book is teaching
. SO going to have to use the old method of coding C# 2.0 crap. But if I give up on that I'll go with this way which seems a lot simpler.
. SO going to have to use the old method of coding C# 2.0 crap. But if I give up on that I'll go with this way which seems a lot simpler. •
•
Join Date: Sep 2009
Posts: 77
Reputation:
Solved Threads: 0
I'm going to feel slightly bad for wasting a bit of your time, I figured a big issue I had, one of my methods was called setDirection if you look in my form I put it as SetDirection and was causing HUGE issues. I think I'm more on track now with my form. I'll post if I need some help again, but I think I'm figure out the C#2.0 way lol.
This is how I think your complete robot class might look like:
c# Syntax (Toggle Plain Text)
class robot { public enum Direction { North, West, South, East } public robot() // Default constructor { location = new Point(); // Sets location.X=0 and location.Y=0 direction = Direction.North; } public Direction direction { get; set; } public Point location { get; set; } public void Move(int unitToMove) { Point P = new Point(); switch (direction) { case Direction.North: P.X = location.X; P.Y = location.Y - unitToMove; break; case Direction.West: P.X = location.X - unitToMove; P.Y = location.Y; break; case Direction.South: P.X = location.X; P.Y = location.Y + unitToMove; break; case Direction.East: P.X = location.X + unitToMove; P.Y = location.Y; break; default: break; } location = P; } public void Draw(Graphics G) { // Our robot will look like a red ball // but you could draw anything here SolidBrush redBrush = new SolidBrush(Color.Red); Size S = new Size(40,40); Rectangle R = new Rectangle(location, S); // Make a red ball size 40,40 and put it at location G.FillEllipse(redBrush, R); } }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Sep 2009
Posts: 77
Reputation:
Solved Threads: 0
•
•
•
•
This is how I think your complete robot class might look like:
c# Syntax (Toggle Plain Text)
class robot { public enum Direction { North, West, South, East } public robot() // Default constructor { location = new Point(); // Sets location.X=0 and location.Y=0 direction = Direction.North; } public Direction direction { get; set; } public Point location { get; set; } public void Move(int unitToMove) { Point P = new Point(); switch (direction) { case Direction.North: P.X = location.X; P.Y = location.Y - unitToMove; break; case Direction.West: P.X = location.X - unitToMove; P.Y = location.Y; break; case Direction.South: P.X = location.X; P.Y = location.Y + unitToMove; break; case Direction.East: P.X = location.X + unitToMove; P.Y = location.Y; break; default: break; } location = P; } public void Draw(Graphics G) { // Our robot will look like a red ball // but you could draw anything here SolidBrush redBrush = new SolidBrush(Color.Red); Size S = new Size(40,40); Rectangle R = new Rectangle(location, S); // Make a red ball size 40,40 and put it at location G.FillEllipse(redBrush, R); } }
I should also put a Panel on your Form, and use the Paint event of that Panel, so you can have a Graphics object to draw the robot in.
Seems to me much better than moving around a Label with a strange Font. Albeit, I must admit it was a nice try
Seems to me much better than moving around a Label with a strange Font. Albeit, I must admit it was a nice try
Last edited by ddanbe; Sep 25th, 2009 at 6:14 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Sep 2009
Posts: 77
Reputation:
Solved Threads: 0
•
•
•
•
I should also put a Panel on your Form, and use the Paint event of that Panel, so you can have a Graphics object to draw the robot in.
Seems to me much better than moving around a Label with a strange Font.
. ![]() |
Similar Threads
- Game: Robots (Class, Constructors, etc.) (C++)
- Python for Laptop Robot Speech recognition and TTS. (Python)
- Siebel Systems Engineer needed (Tech / IT Consultant Job Offers)
- News Story: Linux Powered Robot Dog (Linux Servers and Apache)
- Slow CPU causes damage to robot (C)
- direct X version problems (Windows 95 / 98 / Me)
- Direct CD: PC to Mac (Mac Software)
Other Threads in the C# Forum
- Previous Thread: auto scaling in web application
- Next Thread: Controls added to panel are moving down when repainted
| Thread Tools | Search this Thread |
.net access activedirectory analyst app array asp.net basic broadcast c# camera check checkbox code combobox commerce copy custom database datagrid datetime dba dbconnection developer development dll droid ecommerce element email enum eventhandlers excel file files foreach form forms gdi+ hardware httpwebrequest index javascript linux lisp list listbox marshalbyrefobject math mdd msword mysql news open operator packaging panel path photoshop php post printer programmer programming redirect remote remoting reporting resource richtextbox robot server smoobjects softwaredevelopment sql sql-server statistics stream study table tcpclientchannel textbox totaldays treeview upload uploadatextfile validation validator vb video visual visualbasic visualbasic6 webbrowser webdevelopment window windows winforms wpf write







