Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "wx/wx.h"
- #include <wx/timer.h>
- #include "ex7.h"
- #include <cstdio>
- #include <cstdlib>
- #include <time.h>
- using namespace std;
- #define UP 1
- #define UPRIGHT 2
- #define RIGHT 3
- #define DOWNRIGHT 4
- #define DOWN 5
- #define DOWNLEFT 6
- #define LEFT 7
- #define UPLEFT 8
- IMPLEMENT_APP(myApp)
- // ========================================================================================================
- // wxApp
- // ========================================================================================================
- bool myApp::OnInit()
- {
- srand(time(NULL));// add for got real randomal
- MyFrame *frame = new MyFrame();
- frame->Show(true);
- wxImage image;
- wxImage::AddHandler(new wxPNGHandler);
- bool isloadOK = image.LoadFile("shape01.png", wxBITMAP_TYPE_PNG);
- if (isloadOK)
- {
- DragShape *newShape = new DragShape(wxBitmap(image));
- newShape->SetPosition(wxPoint(50, 50));
- frame->GetCanvas()->SetShape(newShape);
- }
- int numOfEnemy = 3;
- while (numOfEnemy > 0)
- {
- bool isloadOK = image.LoadFile("shape01.png", wxBITMAP_TYPE_PNG);
- if (isloadOK)
- {
- DragShape *newShape = new DragShape(wxBitmap(image));
- newShape->SetPosition(wxPoint((rand() % 470) + 1, (rand() % 360) + 1));
- frame->GetCanvas()->SetShapesQueue(newShape);
- }
- numOfEnemy--;
- }
- frame->Show(true);
- SetTopWindow(frame);
- return true;
- }
- // ========================================================================================================
- // MyFrame
- // ========================================================================================================
- MyFrame::MyFrame()
- : wxFrame((wxFrame *)NULL, wxID_ANY, "EX 7",
- wxPoint(20, 20), wxSize(470, 360))
- {
- wxSize sz = GetClientSize();
- m_canvas = new MyCanvas(this, wxID_ANY, wxPoint(0, 0), wxSize(sz.GetX(), sz.GetY()));
- }
- // ========================================================================================================
- // MyCanvas
- // ========================================================================================================
- wxBEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
- EVT_PAINT(MyCanvas::OnPaint)
- EVT_TIMER(wxID_FILE9, MyCanvas::OnTimer)
- wxEND_EVENT_TABLE()
- // ----------------------------------------------------------------------------
- MyCanvas::MyCanvas(wxWindow *parent, wxWindowID id,
- const wxPoint &pos, const wxSize &size)
- : wxScrolledWindow(parent, id, pos, size, wxSUNKEN_BORDER), m_timer(this, wxID_FILE9)
- {
- m_timer.Start(50);
- Bind(wxEVT_CHAR_HOOK, &MyCanvas::OnChar, this);
- SetBackgroundColour(*wxWHITE);
- SetCursor(wxCursor(wxCURSOR_ARROW));
- }
- // ----------------------------------------------------------------------------
- void MyCanvas::OnTimer(wxTimerEvent &event)
- {
- wxSize sz = this->GetClientSize();
- DragShape *temp;
- DragShape *temp2;
- bool flag = false;
- for (int i = 0; i < shapesQueue.size(); i++)
- {
- temp = shapesQueue.front();
- shapesQueue.pop();
- if (shapesQueue.empty())
- {
- wxMessageBox(wxT("You WIN (:"));
- exit(1);
- }
- if (temp->GetRect().Intersects(_shape->GetRect()))
- {
- wxMessageBox(wxT("you LOSE :("));
- exit(1);
- }
- for (int j = 0; j < shapesQueue.size(); j++)
- {
- temp2 = shapesQueue.front();
- shapesQueue.pop();
- if (temp->GetRect().Intersects(temp2->GetRect()))
- {
- DragShape *result = new PairShape(temp, temp2);
- shapesQueue.push(result);
- flag = true;
- }
- else
- {
- shapesQueue.push(temp2);
- }
- }
- if (flag == false)
- {
- shapesQueue.push(temp);
- }
- else
- {
- flag = false;
- }
- }
- for (int i = 0; i < shapesQueue.size(); i++)
- {
- temp = shapesQueue.front();
- shapesQueue.pop();
- temp->updateDirection();
- temp->hitWall(sz);
- temp->updateSteps();
- shapesQueue.push(temp);
- }
- Refresh(true);
- Update();
- }
- // ----------------------------------------------------------------------------
- void MyCanvas::OnChar(wxKeyEvent &event)
- {
- int x = 0, y = 0;
- if (event.GetUnicodeKey() != WXK_NONE)
- return;
- // It's a special key, deal with all the known ones:
- switch (event.GetKeyCode())
- {
- case WXK_NUMPAD_LEFT:
- case WXK_LEFT:
- x--;
- break;
- case WXK_NUMPAD_RIGHT:
- case WXK_RIGHT:
- x++;
- break;
- case WXK_NUMPAD_UP:
- case WXK_UP:
- y--;
- break;
- case WXK_NUMPAD_DOWN:
- case WXK_DOWN:
- y++;
- break;
- }
- _shape->SetPosition(wxPoint(_shape->GetPosition().x + x, _shape->GetPosition().y + y));
- Refresh(true);
- Update();
- }
- // ----------------------------------------------------------------------------
- void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
- {
- wxPaintDC dc(this);
- PrepareDC(dc);
- DrawShapes(dc);
- }
- // ----------------------------------------------------------------------------
- void MyCanvas::DrawShapes(wxDC &dc)
- {
- _shape->Draw(dc);
- DragShape *temp;
- for (int i = 0; i < shapesQueue.size(); i++)
- {
- temp = shapesQueue.front();
- temp->Draw(dc);
- shapesQueue.pop();
- shapesQueue.push(temp);
- }
- }
- // ----------------------------------------------------------------------------
- MyCanvas::~MyCanvas()
- {
- }
- // ========================================================================================================
- // DragShape
- // ========================================================================================================
- DragShape::DragShape(const wxBitmap &bitmap)
- {
- m_bitmap = bitmap;
- m_pos.x = 0;
- m_pos.y = 0;
- this->direction = rand() % 8 + 1;
- this->counterSteps = 0;
- }
- bool DragShape::Draw(wxDC &dc)
- {
- if (m_bitmap.IsOk())
- {
- wxMemoryDC memDC;
- memDC.SelectObject(m_bitmap);
- dc.Blit(m_pos.x, m_pos.y, m_bitmap.GetWidth(), m_bitmap.GetHeight(),
- &memDC, 0, 0, wxCOPY, true);
- return true;
- }
- else
- return false;
- }
- void DragShape::updateDirection()
- {
- if (this->counterSteps == 17)
- {
- this->direction = ((rand() % 8) + 1);
- this->counterSteps = 0;
- }
- }
- void DragShape ::updateSteps()
- {
- switch (this->GetDirection())
- {
- case UP:
- this->SetPosition(wxPoint(this->GetPosition().x, this->GetPosition().y - 1));
- break;
- case UPRIGHT:
- this->SetPosition(wxPoint(this->GetPosition().x + 1, this->GetPosition().y - 1));
- break;
- case RIGHT:
- this->SetPosition(wxPoint(this->GetPosition().x + 1, this->GetPosition().y));
- break;
- case DOWNRIGHT:
- this->SetPosition(wxPoint(this->GetPosition().x + 1, this->GetPosition().y + 1));
- break;
- case DOWN:
- this->SetPosition(wxPoint(this->GetPosition().x, this->GetPosition().y + 1));
- break;
- case DOWNLEFT:
- this->SetPosition(wxPoint(this->GetPosition().x - 1, this->GetPosition().y + 1));
- break;
- case LEFT:
- this->SetPosition(wxPoint(this->GetPosition().x - 1, this->GetPosition().y));
- break;
- case UPLEFT:
- this->SetPosition(wxPoint(this->GetPosition().x - 1, this->GetPosition().y - 1));
- break;
- }
- this->SetCounterSteps(this->GetCounterSteps() + 1);
- }
- void DragShape::hitWall(wxSize sz)
- {
- wxRect r = this->GetRect();
- int posX = r.GetX();
- int posY = r.GetY();
- int height = r.GetHeight();
- int width = r.GetWidth();
- if (posX <= 0)
- {
- if (this->GetDirection() == LEFT)
- {
- this->SetDirection(RIGHT);
- }
- else if (this->GetDirection() == UPLEFT)
- {
- this->SetDirection(RIGHT);
- }
- else if (this->GetDirection() == DOWNLEFT)
- {
- this->SetDirection(DOWNRIGHT);
- }
- }
- else if ((posX + width) >= sz.GetX())
- {
- if (this->GetDirection() == RIGHT)
- {
- this->SetDirection(LEFT);
- }
- else if (this->GetDirection() == UPRIGHT)
- {
- this->SetDirection(UPLEFT);
- }
- else if (this->GetDirection() == DOWNRIGHT)
- {
- this->SetDirection(DOWNLEFT);
- }
- }
- else if ((posY + height) >= sz.GetY())
- {
- if (this->GetDirection() == DOWN)
- {
- this->SetDirection(UP);
- }
- else if (this->GetDirection() == DOWNLEFT)
- {
- this->SetDirection(UPLEFT);
- }
- else if (this->GetDirection() == DOWNRIGHT)
- {
- this->SetDirection(UPRIGHT);
- }
- }
- else if (posY <= 0)
- {
- if (this->GetDirection() == UP)
- {
- this->SetDirection(DOWN);
- }
- else if (this->GetDirection() == UPRIGHT)
- {
- this->SetDirection(DOWNRIGHT);
- }
- else if (this->GetDirection() == UPLEFT)
- {
- this->SetDirection(DOWNLEFT);
- }
- }
- }
- // ========================================================================================================
- // PairShape
- // ========================================================================================================
- PairShape::PairShape(DragShape *enemy1, DragShape *enemy2)
- {
- this->enemyOne = enemy1;
- this->enemyTwo = enemy2;
- this->SetPosition(wxPoint(enemyOne->GetPosition().x / 2 + enemyTwo->GetPosition().x / 2, enemyOne->GetPosition().y / 2 + enemyTwo->GetPosition().y / 2));
- this->counterSteps = 0;
- this->enemyOne->SetCounterSteps(0);
- this->enemyTwo->SetCounterSteps(0);
- this->direction = this->enemyOne->GetDirection();
- this->enemyTwo->SetDirection(this->direction);
- }
- bool PairShape::Draw(wxDC &dc)
- {
- this->enemyOne->Draw(dc);
- this->enemyTwo->Draw(dc);
- }
- void PairShape::updateDirection()
- {
- if (this->counterSteps == 17)
- {
- this->SetDirection(((rand() % 8) + 1));
- this->counterSteps = 0;
- this->enemyOne->SetDirection(this->direction);
- this->enemyTwo->SetDirection(this->direction);
- this->enemyOne->SetCounterSteps(0);
- this->enemyTwo->SetCounterSteps(0);
- }
- }
- void PairShape::updateSteps()
- {
- this->enemyOne->updateSteps();
- this->enemyTwo->updateSteps();
- this->SetCounterSteps(this->GetCounterSteps() + 1);
- switch (this->GetDirection())
- {
- case UP:
- this->SetPosition(wxPoint(this->GetPosition().x, this->GetPosition().y - 1));
- break;
- case UPRIGHT:
- this->SetPosition(wxPoint(this->GetPosition().x + 1, this->GetPosition().y - 1));
- break;
- case RIGHT:
- this->SetPosition(wxPoint(this->GetPosition().x + 1, this->GetPosition().y));
- break;
- case DOWNRIGHT:
- this->SetPosition(wxPoint(this->GetPosition().x + 1, this->GetPosition().y + 1));
- break;
- case DOWN:
- this->SetPosition(wxPoint(this->GetPosition().x, this->GetPosition().y + 1));
- break;
- case DOWNLEFT:
- this->SetPosition(wxPoint(this->GetPosition().x - 1, this->GetPosition().y + 1));
- break;
- case LEFT:
- this->SetPosition(wxPoint(this->GetPosition().x - 1, this->GetPosition().y));
- break;
- case UPLEFT:
- this->SetPosition(wxPoint(this->GetPosition().x - 1, this->GetPosition().y - 1));
- break;
- }
- }
- void PairShape ::hitWall(wxSize sz)
- {
- wxRect r = this->GetRect();
- int posX = r.GetX();
- int posY = r.GetY();
- int height = r.GetHeight();
- int width = r.GetWidth();
- if (posX <= 0)
- {
- if (this->GetDirection() == LEFT)
- {
- this->SetDirection(RIGHT);
- }
- else if (this->GetDirection() == UPLEFT)
- {
- this->SetDirection(RIGHT);
- }
- else if (this->GetDirection() == DOWNLEFT)
- {
- this->SetDirection(DOWNRIGHT);
- }
- }
- else if ((posX + width) >= sz.GetX())
- {
- if (this->GetDirection() == RIGHT)
- {
- this->SetDirection(LEFT);
- }
- else if (this->GetDirection() == UPRIGHT)
- {
- this->SetDirection(UPLEFT);
- }
- else if (this->GetDirection() == DOWNRIGHT)
- {
- this->SetDirection(DOWNLEFT);
- }
- }
- else if ((posY + height) >= sz.GetY())
- {
- if (this->GetDirection() == DOWN)
- {
- this->SetDirection(UP);
- }
- else if (this->GetDirection() == DOWNLEFT)
- {
- this->SetDirection(UPLEFT);
- }
- else if (this->GetDirection() == DOWNRIGHT)
- {
- this->SetDirection(UPRIGHT);
- }
- }
- else if (posY <= 0)
- {
- if (this->GetDirection() == UP)
- {
- this->SetDirection(DOWN);
- }
- else if (this->GetDirection() == UPRIGHT)
- {
- this->SetDirection(DOWNRIGHT);
- }
- else if (this->GetDirection() == UPLEFT)
- {
- this->SetDirection(DOWNLEFT);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement