Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Snake_no2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Timer timer = new Timer(); Random random1 = new Random(); Random random2 = new Random(); short headX = 170; short headY = 170; byte circleW = 10; byte circleH = 10; int score = 0; int rndFutterX; int rndFutterY; byte richtung = 0; private void Form1_Load(object sender, EventArgs e) { this.Paint += Form1_Paint; //wird benötigt um Kreis bei Programm-Ausführung zu zeichnen timer.Interval = 1000; timer.Tick += new EventHandler(Update); timer.Start(); rndFutterX = 10 * random1.Next(0, 35); rndFutterY = 10 * random2.Next(0, 35); } private void Form1_Paint(object sender, PaintEventArgs e) { SolidBrush playerBrush = new SolidBrush(Color.Red); Graphics grafik = this.CreateGraphics(); Rectangle circle = new Rectangle(headX, headY, circleH, circleW); //grafik.DrawEllipse(Pens.Red, circle); grafik.FillEllipse(playerBrush, circle); SolidBrush foodBrush = new SolidBrush(Color.Blue); Graphics futter = this.CreateGraphics(); Rectangle rekt = new Rectangle(rndFutterX, rndFutterY, 10, 10); futter.FillRectangle(foodBrush, rekt); } private void Update(object sender, EventArgs e) { if (richtung == 1) { headY += 10; this.Refresh(); } if (richtung == 2) { headY -= 10; this.Refresh(); } if (richtung == 3) { headX -= 10; this.Refresh(); } if (richtung == 4) { headX += 10; this.Refresh(); } //Schlange an Rand if (headY == 360 || headY == -10 || headX == 360 || headX == -10) { timer.Stop(); MessageBox.Show("Game Over. Your Score: " + score); } //Schlange auf Futter if (headY == rndFutterY && headX == rndFutterX) { rndFutterX = 10 * random1.Next(0, 35); rndFutterY = 10 * random2.Next(0, 35); score += 1; if (timer.Interval > 250) { timer.Interval = timer.Interval - 250; } if (timer.Interval > 100 && timer.Interval <= 250) { timer.Interval = timer.Interval - 50; } if (timer.Interval > 20 && timer.Interval <= 100) { timer.Interval = timer.Interval - 20; } if (timer.Interval > 2 && timer.Interval <= 20) { timer.Interval = timer.Interval - 2; } } } public void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Down) { richtung = 1; } if (e.KeyCode == Keys.Up) { richtung = 2; } if (e.KeyCode == Keys.Left) { richtung = 3; } if (e.KeyCode == Keys.Right) { richtung = 4; } } } } |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Das ist kein sehr guter Vorschlag. Baue niemals Dinge, von denen Du nicht weißt, ob Du sie jemals brauchst. Baue Dinge dann, wenn Du sie brauchst.Der einzige Verbesserungsvorschlag wäre deshalb nur alle Ereignisse in der Form zu abonnieren. Dann könnte man einfach ein neues Projekt erstellen und euren Code kopieren und es würde laufen.
Was wäre denn eine Idee für ein nächstes Spiel - nichts großartiges, einfach zum üben und um neues kennen zu lernen mit dem Wissen was ich habe?
Werbeanzeige