Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "wx/wx.h"
- #include "wx/dcbuffer.h"
- #include <string>
- using namespace std;
- class MyFrame : public wxFrame{
- public:
- wxRect player;
- wxRect leftEye, rightEye;
- wxRect shot;
- wxRect enemy;
- bool shotMoving;
- wxPoint shotSpeed;
- wxPoint playerSpeedUpLeft, playerSpeedDownRight;
- wxTimer timer;
- wxColor black;
- wxBrush blackBrush;
- wxColor red;
- wxBrush redBrush;
- wxColor blue;
- wxBrush blueBrush;
- const static int UP{0}, DOWN{1}, LEFT{2}, RIGHT{3};
- int facing;
- const static int playerSpeed{7}, shotVelocity{7}, eyeOffset{5},
- playerMajorDimension{60},playerMinorDimension{40};
- MyFrame(const wxString& title) : wxFrame(nullptr, wxID_ANY, title, wxPoint(50,50), wxSize(700,550)){
- SetBackgroundStyle(wxBG_STYLE_PAINT);
- facing = RIGHT;
- player = wxRect((GetSize().GetWidth() - playerMinorDimension)/2, (GetSize().GetHeight() - playerMajorDimension)/2,
- playerMinorDimension, playerMajorDimension);
- leftEye.SetSize(wxSize(10,10));
- rightEye.SetSize(wxSize(10,10));
- leftEye.SetX(player.GetX() + player.GetWidth() - eyeOffset - leftEye.GetWidth());
- rightEye.SetX(player.GetX() + player.GetWidth() - eyeOffset - rightEye.GetWidth());
- int yCenter = player.GetY() + player.GetHeight()/2;
- leftEye.SetY(yCenter - eyeOffset - leftEye.GetHeight());
- rightEye.SetY(yCenter + eyeOffset);
- shot = wxRect(wxPoint(-100,-100), wxSize(5,5));
- shotMoving = false;
- shotSpeed = wxPoint(0,0);
- playerSpeedUpLeft = wxPoint(0,0); playerSpeedDownRight = wxPoint(0,0);
- timer.SetOwner(this);
- black = wxColor(0,0,0);
- blackBrush = wxBrush(black);
- red = wxColor(255,0,0);
- redBrush= wxBrush(red);
- blue = wxColor(0,0,255);
- blueBrush = wxBrush(blue);
- Bind(wxEVT_CLOSE_WINDOW, &MyFrame::OnClose, this);
- Bind(wxEVT_KEY_DOWN, &MyFrame::OnKeyDown, this);
- Bind(wxEVT_KEY_UP, &MyFrame::OnKeyUp, this);
- Bind(wxEVT_PAINT, &MyFrame::OnPaint, this);
- Bind(wxEVT_TIMER, &MyFrame::OnTimer, this);
- timer.Start(30);
- }
- void OnClose(wxCloseEvent &event){
- Destroy();
- }
- void updateplayerAndEyes(){
- if (facing == LEFT || facing == RIGHT){
- player.SetWidth(playerMinorDimension);
- player.SetHeight(playerMajorDimension);
- int yCenter = player.GetY() + player.GetHeight()/2;
- leftEye.SetY(yCenter - eyeOffset - leftEye.GetHeight());
- rightEye.SetY(yCenter + eyeOffset);
- if (facing == LEFT){
- leftEye.SetX(player.GetX() + eyeOffset);
- rightEye.SetX(player.GetX() + eyeOffset);
- }
- else{
- leftEye.SetX(player.GetX() + player.GetWidth() - eyeOffset - leftEye.GetWidth());
- rightEye.SetX(player.GetX() + player.GetWidth() - eyeOffset - rightEye.GetWidth());
- }
- }
- else{
- player.SetWidth(playerMajorDimension);
- player.SetHeight(playerMinorDimension);
- int xCenter = player.GetX() + player.GetWidth()/2;
- leftEye.SetX(xCenter - eyeOffset - leftEye.GetWidth());
- rightEye.SetX(xCenter + eyeOffset);
- if (facing == UP){
- leftEye.SetY(player.GetY() + eyeOffset);
- rightEye.SetY(player.GetY() + eyeOffset);
- }
- else{
- leftEye.SetY(player.GetY() + player.GetHeight() - eyeOffset - leftEye.GetHeight());
- rightEye.SetY(player.GetY() + player.GetHeight() - eyeOffset - rightEye.GetHeight());
- }
- }
- }
- void OnKeyDown(wxKeyEvent &event){
- char key = static_cast<char>(event.GetUnicodeKey());
- if (key == 'W'){
- playerSpeedUpLeft.y = playerSpeed;
- facing = UP;
- }
- else if (key == 'S'){
- playerSpeedDownRight.y = playerSpeed;
- facing = DOWN;
- }
- else if (key == 'A'){
- playerSpeedUpLeft.x = playerSpeed;
- facing = LEFT;
- }
- else if (key == 'D'){
- playerSpeedDownRight.x = playerSpeed;
- facing = RIGHT;
- }
- else if (event.GetKeyCode() == WXK_SPACE && !shotMoving){
- if (facing == UP){
- shot.SetX(player.GetX() + (player.GetWidth() - shot.GetWidth())/2);
- shot.SetY(player.GetY() - shot.GetWidth()/2);
- shotSpeed = wxPoint(0, -shotVelocity);
- }
- if (facing == DOWN){
- shot.SetX(player.GetX() + (player.GetWidth() - shot.GetWidth())/2);
- shot.SetY(player.GetY() + player.GetHeight() - shot.GetWidth()/2);
- shotSpeed = wxPoint(0, shotVelocity);
- }
- if (facing == RIGHT){
- shot.SetX(player.GetX() + player.GetWidth() - shot.GetWidth()/2);
- shot.SetY(player.GetY() + (player.GetHeight()- shot.GetHeight())/2);
- shotSpeed = wxPoint(shotVelocity, 0);
- }
- if (facing == LEFT){
- shot.SetX(player.GetX() - shot.GetWidth()/2);
- shot.SetY(player.GetY() + (player.GetHeight()- shot.GetHeight())/2);
- shotSpeed = wxPoint(-shotVelocity, 0);
- }
- shotMoving = true;
- }
- updateplayerAndEyes();
- }
- void OnKeyUp(wxKeyEvent &event){
- char key = static_cast<char>(event.GetUnicodeKey());
- if (key == 'W'){
- playerSpeedUpLeft.y = 0;
- if (playerSpeedDownRight.y > 0){
- facing = DOWN;
- }
- else if (playerSpeedDownRight.x > 0){
- facing = RIGHT;
- }
- else if (playerSpeedUpLeft.x > 0){
- facing = LEFT;
- }
- }
- if (key == 'S'){
- playerSpeedDownRight.y = 0;
- if (playerSpeedUpLeft.y > 0){
- facing = UP;
- }
- else if (playerSpeedDownRight.x > 0){
- facing = RIGHT;
- }
- else if (playerSpeedUpLeft.x > 0){
- facing = LEFT;
- }
- }
- if (key == 'A'){
- playerSpeedUpLeft.x = 0;
- if (playerSpeedDownRight.x > 0){
- facing = RIGHT;
- }
- else if (playerSpeedDownRight.y > 0){
- facing = DOWN;
- }
- else if (playerSpeedUpLeft.y > 0){
- facing = UP;
- }
- }
- if (key == 'D'){
- playerSpeedDownRight.x = 0;
- if (playerSpeedUpLeft.x > 0){
- facing = LEFT;
- }
- else if (playerSpeedDownRight.y > 0){
- facing = DOWN;
- }
- else if (playerSpeedUpLeft.y > 0){
- facing = UP;
- }
- }
- updateplayerAndEyes();
- }
- void OnPaint(wxPaintEvent &event){
- wxAutoBufferedPaintDC dc(this);
- dc.SetBackground(*wxWHITE_BRUSH);
- dc.Clear();
- dc.SetPen(*wxBLACK_PEN);
- dc.SetBrush(blackBrush);
- dc.DrawEllipse(player);
- dc.SetBrush(redBrush);
- dc.DrawEllipse(leftEye);
- dc.DrawEllipse(rightEye);
- dc.SetBrush(blackBrush);
- dc.DrawEllipse(shot);
- dc.SetBrush(blueBrush);
- dc.DrawEllipse(enemy);
- }
- void OnTimer(wxTimerEvent &event){
- player.SetPosition(player.GetPosition() + playerSpeedDownRight - playerSpeedUpLeft);
- leftEye.SetPosition(leftEye.GetPosition() + playerSpeedDownRight - playerSpeedUpLeft);
- rightEye.SetPosition(rightEye.GetPosition() + playerSpeedDownRight - playerSpeedUpLeft);
- if (shotMoving){
- shot.SetPosition(shot.GetPosition() + shotSpeed);
- if (shot.GetX() + shot.GetWidth() < 0 || shot.GetX() > GetSize().GetWidth() ||
- shot.GetY() + shot.GetHeight() < 0 || shot.GetY() > GetSize().GetHeight()){
- shotSpeed = wxPoint(0,0);
- shotMoving = false;
- }
- }
- Refresh();
- }
- };
- class MyApp : public wxApp{
- public:
- virtual bool OnInit(){
- MyFrame *frame = new MyFrame("Final Game");
- frame->Show(true);
- return true;
- }
- };
- IMPLEMENT_APP(MyApp);
Advertisement
Add Comment
Please, Sign In to add comment