using System.Drawing;
namespace BattleShip
{
class BShip:ImgRendingObject
{
public BShip(Image img, Point loc, Size size, int v):base(img,loc,size,v)
{
}
public Rocket Fire()
{
Image img = Image.FromFile("./images/rocket.png");
Image boom = Image.FromFile("./images/boom.png");
return new Rocket(boom,img, new Point(this.loc.X,this.loc.Y-this.size.Height), this.size,10);
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace BattleShip
{
public partial class Form1 : Form
{
bool isGameOver;
BShip bship;
List<Stone> stones;
int MAXSTONESLICE = 10;
int stoneSlice;//frames for creating a new stone
Image stoneImage;
Random rd;
List<Rocket> rockets;
int score;
public Form1()
{
InitializeComponent();
this.Width = 400;
this.Height = 600;
this.BackColor = Color.Black;
this.DoubleBuffered = true;
this.StartPosition = FormStartPosition.CenterScreen;
this.ClientSize = new Size(400, 600);
//objects
Image img = Image.FromFile("./images/battleship.png");
bship = new BShip(img,
new Point(this.ClientSize.Width / 2 - 30, this.ClientSize.Height - 60),
new Size(60,60 ),10);
stones = new List<Stone>();
stoneSlice = MAXSTONESLICE;
stoneImage = Image.FromFile("./images/stone.png");
rd = new Random((int)DateTime.Now.Ticks);
rockets = new List<Rocket>();
score = 0;
isGameOver = false;
//
Timer updater = new Timer();
updater.Interval = 100;
updater.Tick += Updater_Tick;
updater.Start();
}
private void Updater_Tick(object sender, EventArgs e)
{
if (!isGameOver)
{
for (int i = stones.Count - 1; i >= 0; i--)
{
if (bship.isCollision(stones[i]))
{
isGameOver = true;
}
if (stones[i].isOutOfFrame(this.ClientSize))
{
stones.RemoveAt(i);
}
else
{
stones[i].Move(Direction.Down);
}
}
if (stoneSlice == 0)
{
stones.Add(
new Stone(stoneImage, new Point(rd.Next(0, this.ClientSize.Width - 60), 0),
new Size(60, 60), 5));
}
stoneSlice--;
if (stoneSlice < 0)
stoneSlice = MAXSTONESLICE;
for (int i = rockets.Count - 1; i >= 0; i--)
{
if (rockets[i].IsBoom)
{
rockets.RemoveAt(i);
break;
}
bool isCol = false;
foreach (var s in stones)
{
if (rockets[i].isCollision(s))
{
isCol = true;
stones.Remove(s);
score++;
rockets[i].IsBoom = true;
break;
}
}
if (!isCol)
rockets[i].Move(Direction.Up);
}
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.Clear(Color.Black);
bship.Draw(g);
foreach (var s in stones)
{
s.Draw(g);
}
foreach (var r in rockets)
{
r.Draw(g);
}
string strScore = "Scores: " + score.ToString();
Font scoreFont = new Font(FontFamily.GenericSansSerif, 20);
Size size = TextRenderer.MeasureText(strScore, scoreFont);
g.DrawString(strScore, scoreFont, new SolidBrush(Color.White), new Point(0,this.ClientSize.Height- size.Height));
}
private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
bship.Move(Direction.Left);
break;
case Keys.Right:
bship.Move(Direction.Right);
break;
case Keys.Up:
bship.Move(Direction.Up);
break;
case Keys.Down:
bship.Move(Direction.Down);
break;
case Keys.Space:
rockets.Add(bship.Fire());
break;
}
}
}
}
using System.Drawing;
namespace BattleShip
{
enum Direction { Up, Down, Left, Right };
abstract class ImgRendingObject
{
protected Image image;
protected Point loc;
protected Size size;
protected int v;
public ImgRendingObject(Image img, Point loc, Size size, int v)
{
this.image = img;
this.loc = loc;
this.size = size;
this.v = v;
}
public Image Image { get => image; set => image = value; }
public Point Loc { get => loc; set => loc = value; }
public Size Size { get => size; set => size = value; }
public int V { get => v; set => v = value; }
public Rectangle Rect { get => new Rectangle(loc, size); }
virtual public void Draw(Graphics g)
{
g.DrawImage(this.image,
new Rectangle(this.loc, this.size),
new Rectangle(0, 0, this.image.Width, this.image.Height),
GraphicsUnit.Pixel);
}
virtual public void Move(Direction dir)
{
switch (dir)
{
case Direction.Down:
loc.Y += v;
break;
case Direction.Up:
loc.Y -= v;
break;
case Direction.Left:
loc.X -= v;
break;
case Direction.Right:
loc.X += v;
break;
}
}
virtual public bool isCollision(ImgRendingObject obj)
{
return this.Rect.IntersectsWith(obj.Rect);
}
virtual public bool isOutOfFrame(Size size)
{
return !(new Rectangle(0, 0, size.Width, size.Height).IntersectsWith(this.Rect));
}
}
}
using System.Drawing;
namespace BattleShip
{
class Rocket : ImgRendingObject
{
Image effectImage;
bool isBoom;
public Rocket(Image effectImage, Image img, Point loc, Size size, int v) : base(img, loc, size, v) {
this.effectImage = effectImage;
this.isBoom = false;
}
public Image EffectImage { get => effectImage; set => effectImage = value; }
public bool IsBoom { get => isBoom; set => isBoom = value; }
override public void Draw(Graphics g)
{
g.DrawImage(this.image,
new Rectangle(this.loc, this.size),
new Rectangle(0, 0, this.image.Width, this.image.Height),
GraphicsUnit.Pixel);
}
public override bool isCollision(ImgRendingObject obj)
{
bool isCol = base.isCollision(obj);
if (isCol)
{
this.isBoom = true;
this.image = this.effectImage;
}
return isCol;
}
}
}
using System.Drawing;
namespace BattleShip
{
class Stone: ImgRendingObject
{
public Stone(Image image, Point loc, Size size, int v):base(image, loc, size, v)
{
}
}
}
More Stories
GDI cơ bản
Cập nhật dữ liệu MS SQL từ C#
Xóa dữ liệu MS SQL server từ C#