aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/Applications/Pong.cs
blob: a88d69f185f859b9f6cec9e4284ca2f4602c75b1 (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
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 speedFactor = 0.025;

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

        private int paddleWidth;

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

        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 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 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 and right walls (NOTE: TEMPORARY)
            if (ballRect.Left <= 0 || ballRect.Right >= pnlcanvas.Width)
                xVel = -xVel; //reverse the Y velocity



            //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);

                }

            }




            //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);

                }

            }





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

        }

        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));

        }

        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);
        }
    }
}