-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pong.cpp
69 lines (56 loc) · 1.53 KB
/
Pong.cpp
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
//
// Created by david on 5/31/15.
//
#include "Pong.h"
void Pong::Init(wxSize boardSize)
{
m_boardSize = boardSize;
m_middle.x = boardSize.x / 2;
m_middle.y = boardSize.y / 2;
wxPoint leftPaddle, rightPaddle;
leftPaddle.x = 0;
leftPaddle.y = m_middle.y;
rightPaddle.x = boardSize.x - Paddle::width;
rightPaddle.y = m_middle.y;
int radius = 15;
m_paddlePlayer.Init(leftPaddle);
m_paddleAI.Init(rightPaddle);
m_ball.Init(m_middle, radius, boardSize, m_paddlePlayer, m_paddleAI);
m_artifitialInteligence.Init(m_ball,m_paddleAI, m_middle);
}
void Pong::OnHumanPlayerMove(int newYPosition)
{
m_paddlePlayer.m_position.y = newYPosition;
}
void Pong::ComputeNextStep()
{
m_artifitialInteligence.ComputePaddlePos();
// Score changed
if(m_ball.GetScore(m_humanScore, m_AIScore))
{
m_ball.SetPosition(m_middle);
}
else
{
m_ball.SetPosition(m_ball.GetNextPosition());
}
}
void Pong::Paint(wxPaintDC& dc)
{
dc.Clear();
m_paddlePlayer.Paint(dc);
m_paddleAI.Paint(dc);
m_ball.Paint(dc);
BoardPaint(dc);
}
void Pong::BoardPaint(wxPaintDC &dc)
{
dc.SetTextForeground(*wxWHITE);
dc.SetFont(wxFont(30, wxFONTFAMILY_SWISS, wxNORMAL, wxBOLD));
wxString humainScoreStr = wxString::Format(wxT("%d"), m_humanScore);
dc.DrawText(humainScoreStr, m_middle.x / 2, m_middle.y /6);
wxString AIScoreStr = wxString::Format(wxT("%d"), m_AIScore);
dc.DrawText(AIScoreStr, m_middle.x + m_middle.x / 2, m_middle.y /6);
dc.SetPen(wxPen( wxColor(255,255,255), 1 , wxLONG_DASH));
dc.DrawLine(wxPoint(m_middle.x,0), wxPoint(m_middle.x, m_boardSize.y));
}