This repository has been archived by the owner on Jan 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoorbeeld.txt
94 lines (75 loc) · 2.7 KB
/
voorbeeld.txt
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
http://itools.subhashbose.com/grapher/index.php
A field initializer cannot reference the non-static field, method, or property?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsGame
{
public partial class Form1 : Form
{
Bitmap Backbuffer;
const int BallAxisSpeed = 2;
Point BallPos = new Point(30, 30);
Point BallSpeed = new Point(BallAxisSpeed, BallAxisSpeed);
const int BallSize = 50;
public Form1()
{
InitializeComponent();
this.SetStyle(
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer, true);
Timer GameTimer = new Timer();
GameTimer.Interval = 10;
GameTimer.Tick += new EventHandler(GameTimer_Tick);
GameTimer.Start();
this.ResizeEnd += new EventHandler(Form1_CreateBackBuffer);
this.Load += new EventHandler(Form1_CreateBackBuffer);
this.Paint += new PaintEventHandler(Form1_Paint);
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
BallSpeed.X = -BallAxisSpeed;
else if (e.KeyCode == Keys.Right)
BallSpeed.X = BallAxisSpeed;
else if (e.KeyCode == Keys.Up)
BallSpeed.Y = -BallAxisSpeed; // Y axis is downwards so -ve is up.
else if (e.KeyCode == Keys.Down)
BallSpeed.Y = BallAxisSpeed;
}
void Form1_Paint(object sender, PaintEventArgs e)
{
if (Backbuffer != null)
{
e.Graphics.DrawImageUnscaled(Backbuffer, Point.Empty);
}
}
void Form1_CreateBackBuffer(object sender, EventArgs e)
{
if (Backbuffer != null)
Backbuffer.Dispose();
Backbuffer = new Bitmap(ClientSize.Width, ClientSize.Height);
}
void Draw()
{
if (Backbuffer != null)
{
using (var g = Graphics.FromImage(Backbuffer))
{
g.Clear(Color.White);
g.FillEllipse(Brushes.Black, BallPos.X - BallSize / 2, BallPos.Y - BallSize / 2, BallSize, BallSize);
}
Invalidate();
}
}
void GameTimer_Tick(object sender, EventArgs e)
{
BallPos.X += BallSpeed.X;
BallPos.Y += BallSpeed.Y;
Draw();
// TODO: Add the notion of dying (disable the timer and show a message box or something)
}
}
}