File: pong.cpp

package info (click to toggle)
vibes 0.3.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,684 kB
  • sloc: cpp: 6,120; python: 412; makefile: 214; sh: 13
file content (176 lines) | stat: -rw-r--r-- 5,518 bytes parent folder | download
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
// This file is part of VIBes' C++ API examples
//
// Copyright (c) 2013-2014 Jeremy Nicola, Vincent Drevelle
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "pong.h"
#include <iostream>

using namespace std;

box leftPlayer, rightPlayer, ball, background;
box questionMark[QUESTIONMARK_BOXES_NB];

box leftBorder(interval(-SCENE_WIDTH,-SCENE_WIDTH/2.0f),interval(-SCENE_HEIGHT,SCENE_HEIGHT));
box rightBorder(interval(SCENE_WIDTH/2.0f,SCENE_WIDTH),interval(-SCENE_HEIGHT,SCENE_HEIGHT));
box topBorder(interval(-SCENE_WIDTH,SCENE_WIDTH),interval(SCENE_HEIGHT/2.0f,SCENE_HEIGHT));
box bottomBorder(interval(-SCENE_WIDTH,SCENE_WIDTH),interval(-SCENE_HEIGHT/2.0f,-SCENE_HEIGHT));

std::vector<box> leftScore;
std::vector<box> rightScore;

int iterations;

int timer;

float angleInit;
float angle;

int main()
{
  // Not needed anymore: vibes::beginDrawing();
  initScene();
  while(true)
    updateScene();
  return 0;
}

void initScene()
{
  vibes::newFigure("Interval Pong");
  angleInit=angle=9.0f;
  timer=0;
  iterations=0;
  leftPlayer=box(interval(-SCENE_WIDTH/2.0f,-SCENE_WIDTH/2.0f+PLATFORM_WIDTH),interval(-PLATFORM_HEIGHT/2.0f,PLATFORM_HEIGHT/2.0f));
  rightPlayer=box(interval(SCENE_WIDTH/2.0f-PLATFORM_WIDTH,SCENE_WIDTH/2.0f),interval(-PLATFORM_HEIGHT/2.0f,PLATFORM_HEIGHT/2.0f));
  background=box(interval(-SCENE_WIDTH/2.0f,SCENE_WIDTH/2.0f),interval(-SCENE_HEIGHT/2.0f,SCENE_HEIGHT/2.0f));
  ball=box(interval(-10,10),interval(-10,10));
  vibes::drawBox(background[1].inf,background[1].sup,background[2].inf,background[2].sup,"b");
}

void drawScene()
{
  iterations++;
  if(iterations%25==0)
  {
    vibes::clearFigure("Interval Pong");
    vibes::drawBox(background[1].inf,background[1].sup,background[2].inf,background[2].sup,"b");
  }
  vibes::drawBox(leftPlayer[1].inf,leftPlayer[1].sup,leftPlayer[2].inf,leftPlayer[2].sup,"y");
  vibes::drawBox(rightPlayer[1].inf,rightPlayer[1].sup,rightPlayer[2].inf,rightPlayer[2].sup,"r");
  vibes::drawBox(ball[1].inf,ball[1].sup,ball[2].inf,ball[2].sup,"k");
  crossSleepMs(SLEEP_MS);
}

void updateScene()
{
  ball=ball+BALL_SPEED*box(interval(cos(angle*3.14/180)),interval(sin(angle*3.14/180)));
  leftPlayer[2].inf=0.5*(SCENE_HEIGHT-PLATFORM_HEIGHT)*sin(2*3.14*LEFT_PLAYER_FREQ*SLEEP_MS*iterations/1000.0f)-PLATFORM_HEIGHT/2.0f;
  leftPlayer[2].sup=0.5*(SCENE_HEIGHT-PLATFORM_HEIGHT)*sin(2*3.14*LEFT_PLAYER_FREQ*SLEEP_MS*iterations/1000.0f)+PLATFORM_HEIGHT/2.0f;
  rightPlayer[2].inf=0.5*(SCENE_HEIGHT-PLATFORM_HEIGHT)*sin(2*3.14*RIGHT_PLAYER_FREQ*SLEEP_MS*iterations/1000.0f)-PLATFORM_HEIGHT/2.0f;
  rightPlayer[2].sup=0.5*(SCENE_HEIGHT-PLATFORM_HEIGHT)*sin(2*3.14*RIGHT_PLAYER_FREQ*SLEEP_MS*iterations/1000.0f)+PLATFORM_HEIGHT/2.0f;
  checkState();
  drawScene();
}

void checkState()
{
  if(timer>TIMER_TRESHOLD)
  {
  angle+=360;
  angle=((int)angle%360);
  if(!Subset(ball,background))
  {
    timer=0;
    if(!Inter(ball,topBorder).IsEmpty())
    {
      std::cout << "Collision top" << std::endl;
      if(angle<90&&angle>=0)
        angle=-angle;
      else if(angle>=90&&angle<180)
        angle=360-angle;
    }
    else if(!Inter(ball,bottomBorder).IsEmpty())
    {
      cout << "Collision bottom" << endl;
      if(angle>=180&&angle<270){
        angle=360-angle;
        cout << "cas 1 " << endl;
        }
      else if(angle>=270&&angle<360)
      {
       cout << "cas 2"<<endl;
       cout << "angle: " << angle << endl;
        angle=(360-angle);
        cout << "angle apres: " << angle << endl;
        }
        else{
        cout << "pb, angle: " << angle << endl;
        }
    }
    else if(!Inter(ball,leftBorder).IsEmpty())
    {
      cout << "Collision left" << endl;
      loseAnimation();
    }
    else if(!Inter(ball,rightBorder).IsEmpty())
    {
      cout << "Collision right" << endl;
      loseAnimation();
    }
  }
  else if(!Inter(ball,leftPlayer).IsEmpty())
  {
    cout << "Collision leftPlayer" << endl;
    timer=0;
    if(angle<180)
      angle=(180-angle);
    else if(angle>=180)
      angle=(180-angle);
  }
  else if(!Inter(ball,rightPlayer).IsEmpty())
  {
    cout << "Collision rightPlayer" << endl;
    if(angle<90)
      angle=180-angle;
    else if(angle<360)
      angle=180+angle;
    timer=0;
  }
  }
}

void loseAnimation()
{
  ball=box(interval(-10,10),interval(-10,10));
  angleInit+=30;
  angle=angleInit;
}

void crossSleepMs(int sleepMs)
{
  #ifdef UNIX
    usleep(sleepMs*1000);
  #endif
  #ifdef WIN32
    Sleep(sleepMs);
  #endif
  timer+=sleepMs;
}