-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatgpt.cs
More file actions
123 lines (103 loc) · 3.13 KB
/
Copy pathchatgpt.cs
File metadata and controls
123 lines (103 loc) · 3.13 KB
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
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CarRacing
{
public partial class MainForm : Form
{
// Console window size
const int WIDTH = 400;
const int HEIGHT = 600;
// Car dimensions
const int CAR_WIDTH = 40;
const int CAR_HEIGHT = 80;
// Car position and speed
int carX = WIDTH / 2 - CAR_WIDTH / 2;
int carY = HEIGHT - CAR_HEIGHT - 50;
int carSpeed = 10;
// Obstacle position and speed
int obstacleX = 0;
int obstacleY = 0;
int obstacleSpeed = 10;
// Score
int score = 0;
// Random number generator
Random rand = new Random();
public MainForm()
{
InitializeComponent();
// Set form size
this.ClientSize = new Size(WIDTH, HEIGHT);
// Start the game loop
gameTimer.Start();
}
private void gameTimer_Tick(object sender, EventArgs e)
{
// Move the car
if (Input.Left)
{
carX -= carSpeed;
}
else if (Input.Right)
{
carX += carSpeed;
}
// Move the obstacle
obstacleY += obstacleSpeed;
if (obstacleY >= HEIGHT)
{
obstacleX = rand.Next(0, WIDTH - 100);
obstacleY = 0;
score++;
}
// Check for collision
if (obstacleY + 100 >= carY && obstacleY <= carY + CAR_HEIGHT && obstacleX + 100 >= carX && obstacleX <= carX + CAR_WIDTH)
{
// Collision detected, end the game
gameTimer.Stop();
MessageBox.Show($"Game over! Your score was {score}.", "Car Racing", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
// Redraw the screen
this.Invalidate();
}
private void MainForm_Paint(object sender, PaintEventArgs e)
{
// Draw the background
e.Graphics.FillRectangle(Brushes.White, 0, 0, WIDTH, HEIGHT);
// Draw the car
e.Graphics.FillRectangle(Brushes.Blue, carX, carY, CAR_WIDTH, CAR_HEIGHT);
// Draw the obstacle
e.Graphics.FillRectangle(Brushes.Red, obstacleX, obstacleY, 100, 100);
// Draw the score
e.Graphics.DrawString($"Score: {score}", new Font("Arial", 16), Brushes.Black, 10, 10);
}
}
public static class Input
{
public static bool Left;
public static bool Right;
public static void KeyDown(Keys key)
{
if (key == Keys.Left)
{
Left = true;
}
else if (key == Keys.Right)
{
Right = true;
}
}
public static void KeyUp(Keys key)
{
if (key == Keys.Left)
{
Left = false;
}
else if (key == Keys.Right)
{
Right = false;
}
}
}
}