aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/Applications/Pong.cs
blob: 235a049c74e5adf5f79a0a9efc14cf5195e64404 (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ShiftOS.Engine;

namespace ShiftOS.WinForms.Applications
{
    [Launcher("Pong", true, "al_pong", "Games")]
    [WinOpen("pong")]
    [DefaultTitle("Pong")]
    [DefaultIcon("iconPong")]
    public partial class Pong : UserControl, IShiftOSWindow
    {
        public Pong()
        {
            InitializeComponent();
            paddleWidth = pnlcanvas.Width / 30;
            drawTimer = new Timer();
            drawTimer.Interval = 16;
            drawTimer.Tick += (o, a) =>
            {
                UpdateBall();
                pnlcanvas.Refresh();
            };
        }

        private double ballX = 0.0f;
        private double ballY = 0.0f;

        private double aiBallX = 0.0f;
        private double aiBallY = 0.0f;


        private double speedFactor = 0.025;

        private double xVel = 1;
        private double yVel = 1;

        private int paddleWidth;

        private long codepointsToEarn = 0;
        private int level = 1;


        private double playerY = 0.0;
        private double opponentY = 0.0;

        bool doAi = true;

        public void UpdateBall()
        {
            double ballXLocal = linear(ballX, -1.0, 1.0, 0, pnlcanvas.Width);
            double ballYLocal = linear(ballY, -1.0, 1.0, 0, pnlcanvas.Height);

            ballXLocal -= ((double)paddleWidth / 2);
            ballYLocal -= ((double)paddleWidth / 2);

            double aiBallXLocal = linear(aiBallX, -1.0, 1.0, 0, pnlcanvas.Width);
            double aiBallYLocal = linear(aiBallY, -1.0, 1.0, 0, pnlcanvas.Height);

            aiBallXLocal -= ((double)paddleWidth / 2);
            aiBallYLocal -= ((double)paddleWidth / 2);


            double playerYLocal = linear(playerY, -1.0, 1.0, 0, pnlcanvas.Height);
            double opponentYLocal = linear(opponentY, -1.0, 1.0, 0, pnlcanvas.Height);

            int paddleHeight = pnlcanvas.Height / 5;


            Rectangle ballRect = new Rectangle((int)ballXLocal, (int)ballYLocal, paddleWidth, paddleWidth);

            Rectangle aiBallRect = new Rectangle((int)aiBallXLocal, (int)aiBallYLocal, paddleWidth, paddleWidth);


            Rectangle playerRect = new Rectangle((int)paddleWidth, (int)(playerYLocal - (int)(paddleHeight / 2)), (int)paddleWidth, (int)paddleHeight);
            Rectangle opponentRect = new Rectangle((int)(pnlcanvas.Width - (paddleWidth * 2)), (int)(opponentYLocal - (int)(paddleHeight / 2)), (int)paddleWidth, (int)paddleHeight);

            //Top and bottom walls:
            if (ballRect.Top <= 0 || ballRect.Bottom >= pnlcanvas.Height)
                yVel = -yVel; //reverse the Y velocity


            //Left wall
            if(ballRect.Left <= 0)
            {
                //You lose.
                codepointsToEarn = 0;
                Lose();
            }

            //Right wall
            if(ballRect.Right >= pnlcanvas.Width)
            {
                //You win.
                codepointsToEarn += CalculateAIBeatCP();
                Win();
            }

            //Enemy paddle:
            if (ballRect.IntersectsWith(opponentRect))
            {
                //check if the ball x is greater than the player paddle's middle coordinate
                if (ballRect.Right <= opponentRect.Right - (opponentRect.Width / 2))
                {
                    //reverse x velocity to send the ball the other way
                    xVel = -xVel;

                    //set y velocity based on where the ball hit the paddle
                    yVel = linear((ballRect.Top + (ballRect.Height / 2)), opponentRect.Top, opponentRect.Bottom, -1, 1);

                }

            }


            //Enemy paddle - AI:
            if (aiBallRect.IntersectsWith(opponentRect))
            {
                //check if the ball x is greater than the player paddle's middle coordinate
                if (aiBallRect.Right <= opponentRect.Right - (opponentRect.Width / 2))
                {
                    doAi = false;
                }

            }


            //Player paddle:
            if (ballRect.IntersectsWith(playerRect))
            {
                //check if the ball x is greater than the player paddle's middle coordinate
                if(ballRect.Left >= playerRect.Left + (playerRect.Width / 2))
                {
                    //reverse x velocity to send the ball the other way
                    xVel = -xVel;

                    //set y velocity based on where the ball hit the paddle
                    yVel = linear((ballRect.Top + (ballRect.Height / 2)), playerRect.Top, playerRect.Bottom, -1, 1);

                    //reset the ai location
                    aiBallX = ballX;
                    aiBallY = ballY;
                    doAi = true;
                }

            }





            ballX += xVel * speedFactor;
            ballY += yVel * speedFactor;

            aiBallX += xVel * (speedFactor * 2);
            aiBallY += yVel * (speedFactor * 2);

            if (doAi == true)
            {
                if (opponentY != aiBallY)
                {
                    if (opponentY < aiBallY)
                    {
                        if (opponentY < 0.9)
                            opponentY += speedFactor;
                    }
                    else
                    {
                        if (opponentY > -0.9)
                            opponentY -= speedFactor;
                    }
                }
            }
        }

        public void Lose()
        {
            InitializeCoordinates();
        }

        public long CalculateAIBeatCP()
        {
            return 2 * (10 * level);
        }

        public void Win()
        {
            InitializeCoordinates();
        }

        public void InitializeCoordinates()
        {
            ballX = 0;
            ballY = 0;
            xVel = 1;
            yVel = 1;
            opponentY = 0;
            aiBallX = 0;
            aiBallY = 0;
            doAi = true;
        }

        private void pnlcanvas_Paint(object sender, PaintEventArgs e)
        {

            paddleWidth = pnlcanvas.Width / 30;
            double ballXLocal = linear(ballX, -1.0, 1.0, 0, pnlcanvas.Width);
            double ballYLocal = linear(ballY, -1.0, 1.0, 0, pnlcanvas.Height);

            ballXLocal -= ((double)paddleWidth / 2);
            ballYLocal -= ((double)paddleWidth / 2);


            e.Graphics.Clear(pnlcanvas.BackColor);

            //draw the ball
            e.Graphics.FillEllipse(new SolidBrush(pnlcanvas.ForeColor), new RectangleF((float)ballXLocal, (float)ballYLocal, (float)paddleWidth, (float)paddleWidth));

            double playerYLocal = linear(playerY, -1.0, 1.0, 0, pnlcanvas.Height);
            double opponentYLocal = linear(opponentY, -1.0, 1.0, 0, pnlcanvas.Height);

            int paddleHeight = pnlcanvas.Height / 5;

            int paddleStart = paddleWidth;

            //draw player paddle
            e.Graphics.FillRectangle(new SolidBrush(pnlcanvas.ForeColor), new RectangleF((float)paddleWidth, (float)(playerYLocal - (float)(paddleHeight / 2)), (float)paddleWidth, (float)paddleHeight));

            //draw opponent
            e.Graphics.FillRectangle(new SolidBrush(pnlcanvas.ForeColor), new RectangleF((float)(pnlcanvas.Width - (paddleWidth*2)), (float)(opponentYLocal - (float)(paddleHeight / 2)), (float)paddleWidth, (float)paddleHeight));

            string cp_text = Localization.Parse("{CODEPOINTS}: " + codepointsToEarn);

            var tSize = e.Graphics.MeasureString(cp_text, SkinEngine.LoadedSkin.Header3Font);

            var tLoc = new PointF((pnlcanvas.Width - (int)tSize.Width) / 2,
                (pnlcanvas.Height - (int)tSize.Height)
                
                );
            e.Graphics.DrawString(cp_text, SkinEngine.LoadedSkin.Header3Font, new SolidBrush(pnlcanvas.ForeColor), tLoc);
        }

        static public double linear(double x, double x0, double x1, double y0, double y1)
        {
            if ((x1 - x0) == 0)
            {
                return (y0 + y1) / 2;
            }
            return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
        }

        Timer drawTimer = null;

        public void OnLoad()
        {
            drawTimer.Start();
        }

        public void OnSkinLoad()
        {
        }

        public bool OnUnload()
        {
            drawTimer.Stop();
            return true;
        }

        public void OnUpgrade()
        {
        }

        private void pnlcanvas_MouseMove(object sender, MouseEventArgs e)
        {
            playerY = linear(e.Y, 0, pnlcanvas.Height, -1, 1);
        }
    }
}