Hero-Turtle

D# Projekt Quellen

Eine primitive Turtle-Grafik in C#



  
1:	  /*
2:	   * Erstellt mit SharpDevelop 2
3:	   * Benutzer: kner
4:	   */
5:	  using System;
6:	  using System.Collections.Generic;
7:	  using System.Drawing;
8:	  using System.Windows.Forms;
9:	  
10:	  namespace turtle
11:	  {
12:	      public partial class MainForm
13:	      {
14:	          public class MovingPoint 
15:	          {
16:	              private int x, y; 
17:	              private static Rectangle pGround = new Rectangle(0,0, 100,100);
18:	              public static System.Drawing.Rectangle PlayGround //static, weil fr alle Turtles gltig
19:	              {
20:	                  set {
21:	                      pGround=value;
22:	                  }
23:	                  get {
24:	                      return pGround;
25:	                  }
26:	              }
27:	                                                      
28:	              public int X { 
29:	                  set {
30:	                      if (value >PlayGround.Left && value < PlayGround.Right) x=value; 
31:	                  }
32:	                  get {
33:	                      return x; 
34:	                  }
35:	              }
36:	              public int Y {
37:	                  set {
38:	                      if (value >=PlayGround.Top && value <= PlayGround.Bottom) y=value; 
39:	                  }
40:	                  get {
41:	                      return y; 
42:	                  }
43:	              }
44:	              public void move(int dx, int dy) {
45:	                  X += dx; 
46:	                  Y += dy; 
47:	              }
48:	          }
49:	          public class MovingControl: MovingPoint 
50:	          {
51:	              Control c; 
52:	              public MovingControl (Control c1, int x, int y) 
53:	              {   X = x;         Y = y; 
54:	                  c = c1;        
55:	                  c.Top = X;    c.Left=Y;                 
56:	              }
57:	              public void move(int dx, int dy)
58:	              {     base.move(dx,dy);
59:	                     c.Top = Y;   c.Left = X; 
60:	              }
61:	          }
62:	          public class MovingPictureBox: MovingControl
63:	          {    PictureBox pBox; 
64:	              Image LeftImage, RightImage,UpImage,DownImage; 
65:	              public MovingPictureBox(PictureBox pb, string path, string img, int x, int y):base(pb,x,y){
66:	                  pBox = pb; 
67:	                  LeftImage = Image.FromFile(path+img+@"Left.gif"); 
68:	                  RightImage = Image.FromFile(path+img+@"Right.gif"); 
69:	                  UpImage = Image.FromFile(path+img+@"Up.gif"); 
70:	                  DownImage = Image.FromFile(path+img+@"Down.gif"); 
71:	              }
72:	              private void setImage(Image img){
73:	                  pBox.Image  = img;
74:	                  pBox.Width  = img.Width; 
75:	                  pBox.Height = img.Height; 
76:	              }
77:	              public void moveLeft(int dx){
78:	                  move(-dx,0); setImage(LeftImage);
79:	              }
80:	              public void moveRight(int dx){
81:	                  move(dx,0); setImage(RightImage);
82:	              }
83:	              public void moveUp(int dy){
84:	                  move(0,-dy); setImage(UpImage);
85:	              }
86:	              public void moveDown(int dy){
87:	                  move(0,dy); setImage(DownImage);
88:	              }
89:	          
90:	          }
91:	          public class Turtle:MovingPictureBox
92:	          {  public Turtle(PictureBox pb,string path, string img, int x, int y)
93:	                                                       :base(pb,path, img, x, y){
94:	              }
95:	          }
96:	          Turtle Hero;     
97:	          static void Main(string [] args)
98:	              {
99:	                  Application.EnableVisualStyles();
100:	                  Application.SetCompatibleTextRenderingDefault(false);
101:	                  Application.Run(new MainForm());
102:	              }
103:	              
104:	      
105:	          public MainForm()        {
106:	                  InitializeComponent();    
107:	                  Turtle.PlayGround =  new Rectangle(10,10, 100,100);
108:	                  // die Bilder sind im Projektverzeichnis, daher der Pfad ..\..\
109:	                  Hero = new Turtle(pictureBox1,@"..\..\",@"turtle", 50, 50);
110:	           }
111:	          
112:	          void TSBLeftClick(object sender, System.EventArgs e)
113:	          {
114:	              Hero.moveLeft(10);
115:	          }
116:	          
117:	          void TsbRightClick(object sender, System.EventArgs e)
118:	          {
119:	              Hero.moveRight(10); 
120:	          }
121:	          
122:	          void TsbDownClick(object sender, System.EventArgs e)
123:	          {
124:	              Hero.moveDown(10); 
125:	          }
126:	          
127:	          void TsbUpClick(object sender, System.EventArgs e)
128:	          {
129:	              Hero.moveUp(10); 
130:	          }
131:	          
132:	          void MainFormPaint(object sender, System.Windows.Forms.PaintEventArgs e)
133:	          {
134:	              e.Graphics.DrawRectangle(new Pen(Color.Blue),Turtle.PlayGround);
135:	          }
136:	      }
137:	  }



This page was automatically generated by SharpDevelop.




Kontrollfragen:

Aufgabenstellungen: