SnakeBite Game입니다.
두개의 창을 사용합니다. 시작창과 게임창...
[시작창]
<Grid>
<Image Source="tulip.jpg" Stretch="Fill"></Image>
<TextBlock Margin="20,20">SnakeBite V.1.0 by BeeEye Dmu </TextBlock>
<TextBlock Margin="20,35">Mission: Eat 25 Eggs using Arrow Keys</TextBlock>
<TextBlock Margin="20,50">Press ESC to pause while playing</TextBlock>
<Button Content="Play" Width="100" Height="40" HorizontalAlignment="Left"
Margin="120,200,0,0" Name="button1" FontSize="20" Click="button1_Click"/>
<Button Content="Quit" Width="100" Height="40" HorizontalAlignment="Left"
Margin="300,200,0,0" Name="button2" FontSize="20" Click="button2_Click"/>
</Grid>
private void button1_Click(object sender, RoutedEventArgs e)
{
Window1 w = new Window1();
w.Show();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
[게임창] - Window1
<Window x:Class="snake.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SnakeBite v.1.0 by BeeEye Dmu" Height="438" Width="512" KeyDown="Window_KeyDown">
<Canvas Name="Canvas1" Width="500" Height="400" Background="LightSteelBlue">
<TextBlock Name="Score" Text="Eggs = 0" FontSize="14" Canvas.Left="200" Canvas.Top="20"/>
<TextBlock Name="Time" Text="Time = 00:00:00" FontSize="14" Canvas.Left="270" Canvas.Top="20"/>
</Canvas>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading; // timer
using System.Diagnostics; // StopWatch
namespace snake
{
/// <summary>
/// Window1.xaml에 대한 상호 작용 논리
/// </summary>
public partial class Window1 : Window
{
Random r = new Random();
Ellipse[] Snakes = new Ellipse[30];
Ellipse Egg = new Ellipse();
private int Size = 12; // Egg와 Body의 사이즈
private int visibleCount = 5;
private string move = "";
private int eaten = 0;
DispatcherTimer timer = new DispatcherTimer();
Stopwatch sw = new Stopwatch();
private bool StartFlag = false;
public Window1()
{
InitializeComponent();
InitSnake();
InitEgg();
timer.Interval = new TimeSpan(0, 0, 0, 0, 100); // 0.1초 마다
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
if (move != "")
{
StartFlag = true;
for (int i = visibleCount; i >= 1; i--) // 꼬리 하나를 더 계산해 둔다
{
Point p = (Point)Snakes[i-1].Tag;
Snakes[i].Tag = new Point(p.X, p.Y);
}
Point pnt = (Point)Snakes[0].Tag;
if (move == "Right")
Snakes[0].Tag = new Point(pnt.X + Size, pnt.Y);
else if (move == "Left")
Snakes[0].Tag = new Point(pnt.X - Size, pnt.Y);
else if (move == "Up")
Snakes[0].Tag = new Point(pnt.X, pnt.Y - Size);
else if (move == "Down")
Snakes[0].Tag = new Point(pnt.X, pnt.Y + Size);
eatEgg(); // 알을 먹었는지 체크
}
if (StartFlag == true)
{
TimeSpan ts = sw.Elapsed;
Time.Text = String.Format("Time = {0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
DrawSnakes();
}
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if(move == "") // 맨 처음 키가 눌렸을 때 sw 시작
sw.Start();
if (e.Key == Key.Right)
move = "Right";
else if (e.Key == Key.Left)
move = "Left";
else if (e.Key == Key.Up)
move = "Up";
else if (e.Key == Key.Down)
move = "Down";
else if (e.Key == Key.Escape)
move = "";
}
private void eatEgg()
{
Point pS = (Point)Snakes[0].Tag;
Point pE = (Point)Egg.Tag;
if (pS.X == pE.X && pS.Y == pE.Y)
{
Egg.Visibility = Visibility.Hidden;
visibleCount++;
Snakes[visibleCount - 1].Visibility = Visibility.Visible; // 꼬리를 하나 늘림
Score.Text = "Eggs = " + (++eaten).ToString();
if (visibleCount == 30)
{
timer.Stop();
sw.Stop();
DrawSnakes();
TimeSpan ts = sw.Elapsed;
string TimeElapsed = String.Format("Time = {0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
MessageBox.Show("Success!!! " + TimeElapsed + " sec");
this.Close();
}
Point p = new Point(r.Next(1, 480 / Size) * Size, r.Next(1, 380 / Size) * Size);
Egg.Tag = p;
Egg.Visibility = Visibility.Visible;
Canvas.SetLeft(Egg, p.X);
Canvas.SetTop(Egg, p.Y);
}
}
private void DrawSnakes()
{
for (int i = 0; i < visibleCount; i++)
{
Point p = (Point)Snakes[i].Tag;
Canvas.SetLeft(Snakes[i], p.X);
Canvas.SetTop(Snakes[i], p.Y);
}
}
private void InitEgg()
{
Egg = new Ellipse();
Egg.Tag = new Point(r.Next(1, 480 / Size) * Size, r.Next(1, 380 / Size) * Size);
Egg.Width = Size;
Egg.Height = Size;
Egg.Stroke = Brushes.Black;
Egg.Fill = Brushes.Red;
Point p = (Point)Egg.Tag;
Canvas1.Children.Add(Egg);
Canvas.SetLeft(Egg, p.X);
Canvas.SetTop(Egg, p.Y);
}
private void InitSnake()
{
int x = r.Next(1, 480 / Size) * Size;
int y = r.Next(1, 380 / Size) * Size;
for(int i=0; i<30; i++)
{
Snakes[i] = new Ellipse();
Snakes[i].Width = Size;
Snakes[i].Height = Size;
if(i == 0)
Snakes[i].Fill = Brushes.Chocolate; // 머리와 5번째 마디마다 색깔 변경
else if(i%5 == 0)
Snakes[i].Fill = Brushes.YellowGreen; // 5번째 마디마다 색깔 변경
else
Snakes[i].Fill = Brushes.Gold;
Snakes[i].Stroke=Brushes.Black;
Canvas1.Children.Add(Snakes[i]);
}
for(int i=visibleCount; i<30; i++)
{
Snakes[i].Visibility = Visibility.Hidden;
}
CreateSnake(x, y);
}
private void CreateSnake(int x,int y)
{
for (int i = 0; i < visibleCount; i++)
{
Snakes[i].Tag = new Point(x, y + i * Size);
Canvas.SetLeft(Snakes[i], x);
Canvas.SetTop(Snakes[i], y + i * Size);
}
}
}
}
BeeEye Dmu
'C# WPF' 카테고리의 다른 글
C# WPF delay 주는 방법 (0) | 2013.11.22 |
---|---|
C# WPF 카드게임(Memory Card Game) (0) | 2013.11.22 |
C#에서 실행시간 체크 (0) | 2013.11.21 |
Thread.Sleep() 문제 (0) | 2013.11.14 |
C# 숫자퍼즐(Jeu De Tacquin) in WPF (0) | 2013.10.19 |