-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cs
114 lines (97 loc) · 3.69 KB
/
main.cs
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
using System;
using System.Diagnostics;
using SFML.Window;
using SFML.Graphics;
using SFML.Audio;
namespace sfml_cs
{
public class main
{
static void OnClose(object sender, EventArgs e)
{
// Close the window when OnClose event is received
RenderWindow window = (RenderWindow)sender;
window.Close();
}
public static void Main()
{
/*
RenderWindow window = new RenderWindow(new VideoMode(200, 200), "SFML works!");
Console.WriteLine("test");
Console.ReadKey();
*/
// Create the main window
RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML window");
window.Closed += new EventHandler(OnClose);
// Load a sprite to display
Texture texture = new Texture("media/image/mario.png");
Sprite sprite = new Sprite(texture);
// Create a graphical text to display
Font font = new Font("media/font/arial.ttf");
Text text = new Text("Hello SFML", font, 50);
// Load & play music
Music music = new Music("media/sound/mario.ogg");
music.Play();
// starts the Stopwatch
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// last mouse position
Vector2i last_mouse_position = new Vector2i(0, 0);
// Start the game loop
while (window.IsOpen())
{
// Process events
window.DispatchEvents();
float speed = 0.1f * Convert.ToSingle(stopwatch.Elapsed.TotalMilliseconds);
stopwatch.Reset();
stopwatch.Start();
if (Keyboard.IsKeyPressed(Keyboard.Key.Up))
{
sprite.Position = new Vector2f(sprite.Position.X, sprite.Position.Y - speed);
}
if (Keyboard.IsKeyPressed(Keyboard.Key.Down))
{
sprite.Position = new Vector2f(sprite.Position.X, sprite.Position.Y + speed);
}
if (Keyboard.IsKeyPressed(Keyboard.Key.Left))
{
sprite.Position = new Vector2f(sprite.Position.X - speed, sprite.Position.Y);
}
if (Keyboard.IsKeyPressed(Keyboard.Key.Right))
{
sprite.Position = new Vector2f(sprite.Position.X + speed, sprite.Position.Y);
}
// only if its a new mouse position
if (!last_mouse_position.Equals(Mouse.GetPosition()))
{
if (Mouse.IsButtonPressed(Mouse.Button.Left))
{
Console.WriteLine("Klick Maustaste: Links; X: " + Mouse.GetPosition().X
+ "; Y: " + Mouse.GetPosition().X);
last_mouse_position = Mouse.GetPosition();
}
if (Mouse.IsButtonPressed(Mouse.Button.Middle))
{
Console.WriteLine("Klick Maustaste: Mitte; X: " + Mouse.GetPosition().X
+ "; Y: " + Mouse.GetPosition().X);
last_mouse_position = Mouse.GetPosition();
}
if (Mouse.IsButtonPressed(Mouse.Button.Right))
{
Console.WriteLine("Klick Maustaste: Rechts; X: " + Mouse.GetPosition().X
+ "; Y: " + Mouse.GetPosition().X);
last_mouse_position = Mouse.GetPosition();
}
}
// Clear screen
window.Clear();
// Draw the sprite
window.Draw(sprite);
// Draw the string
window.Draw(text);
// Update the window
window.Display();
}
}
}
}