Neustarten bei Mini Spiel (C#)
Hi!
Ich hab jetzt mal eine kleines C# Spiel programmiert (nur zum üben) und habe folgendes Problem:
Wenn man verloren hat, wird 1. der Timer der ein Objekt bewegt gestoppt und 2. ein Button eingeblendet mit dem man neue starten kann.
Wenn man darauf klick beginnt der Timer wieder und das bewegbare Objekt n kommt wieder auf die Ausgangspostion. Aber ich kann das Objekt nciht mehr mit den Pfeiltasten bewegen, was vorher gegangen ist. Ich glaube ich müsste public void HandleKeyDown noch mal starten, aber mir ist derzeit nicht ganz klar wie diese ganzen (public...)-Dinger genau funktionieren.
Vieleicht kann mir ja wer helfen.
Ich hab jetzt mal eine kleines C# Spiel programmiert (nur zum üben) und habe folgendes Problem:
Wenn man verloren hat, wird 1. der Timer der ein Objekt bewegt gestoppt und 2. ein Button eingeblendet mit dem man neue starten kann.
Wenn man darauf klick beginnt der Timer wieder und das bewegbare Objekt n kommt wieder auf die Ausgangspostion. Aber ich kann das Objekt nciht mehr mit den Pfeiltasten bewegen, was vorher gegangen ist. Ich glaube ich müsste public void HandleKeyDown noch mal starten, aber mir ist derzeit nicht ganz klar wie diese ganzen (public...)-Dinger genau funktionieren.
Vieleicht kann mir ja wer helfen.
|
|
Quellcode |
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Snake
{
public partial class Board : Form
{
int dirX = 0;
int dirY = 0;
int seconds = 0;
Control[] obstacles;
public Board()
{
InitializeComponent();
timer.Start();
obstacles = new Control[4];
obstacles[0] = ob0;
obstacles[1] = ob1;
obstacles[2] = ob2;
obstacles[3] = ob3;
}
private void Board_Load(object sender, EventArgs e) {
this.KeyDown += new KeyEventHandler(HandleKeyDown);
}
public void HandleKeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode.ToString())
{
case "P":
if (timer.Enabled)
timer.Stop();
else
timer.Start();
break;
case "Left":
dirX = -1;
dirY = 0;
//if (!timer.Enabled)
// timer.Start();
break;
case "Right":
dirX = +1;
dirY = 0;
break;
case "Up":
dirX = 0;
dirY = -1;
break;
case "Down":
dirX = 0;
dirY = +1;
break;
case "X":
dirX = +1;
dirY = +1;
break;
case "Y":
dirX = -1;
dirY = -1;
break;
}
}
private bool HasCollision()
{
int x = snake.Location.X;
int y = snake.Location.Y;
// check collision with board
if ((x <= 0 && dirX == -1) ||
(y <= 0 && dirY == -1) ||
(x + snake.Width + 6 >= this.Width && dirX == +1) ||
(y + snake.Height + 26 >= this.Height && dirY == +1)) {
return true;
}
// check collision with obstacles
for (int i = 0; i < obstacles.Length; i++) {
if (HasCollisionWithObstacle(obstacles[i], snake)) {
return true;
}
}
return false;
}
private bool HasCollisionWithObstacle(Control obstacle, Control snake)
{
int x = snake.Location.X;
int y = snake.Location.Y;
// check collision with obstacle
if (dirX == -1 &&
obstacle.Location.X + obstacle.Width >= x &&
obstacle.Location.X < x &&
obstacle.Location.Y < y + snake.Height &&
obstacle.Location.Y + obstacle.Height > y)
{
return true;
}
if (dirX == +1 &&
obstacle.Location.X <= x + snake.Width &&
obstacle.Location.X + obstacle.Width > x &&
obstacle.Location.Y < y + snake.Height &&
obstacle.Location.Y + obstacle.Height > y)
{
return true;
}
if (dirY == -1 &&
obstacle.Location.Y + obstacle.Height >= y &&
obstacle.Location.Y < y &&
obstacle.Location.X < x + snake.Width &&
obstacle.Location.X + obstacle.Width > x)
{
return true;
}
if (dirY == +1 &&
obstacle.Location.Y <= y + snake.Height &&
obstacle.Location.Y + obstacle.Height > y &&
obstacle.Location.X < x + snake.Width &&
obstacle.Location.X + obstacle.Width > x)
{
return true;
}
return false;
}
private void TimerTick(object sender, EventArgs e)
{
int x = snake.Location.X;
int y = snake.Location.Y;
seconds++;
if (HasCollision())
{
labelLost.Visible = true;
button1.Visible = true;
Console.Beep();
timer.Stop();
//labelTime.Text = "00:00";
//return;
}
else
{
labelTime.Text = Convert.ToString(seconds / 60) + ":" + Convert.ToString(seconds % 60);
snake.Location = new Point(x + dirX, y + dirY);
}
}
private void button1_Click(object sender, EventArgs e)
{
snake.Location = new Point(101, 59);
labelLost.Visible = false;
button1.Visible = false;
dirX = 0;
dirY = 0;
seconds = 0;
timer.Start();
}
private void snake_Click(object sender, EventArgs e)
{
}
}
}
|


