Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.42 KB | None | 0 0
  1. #include <windows.h>
  2. #include <math.h>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. HDC dc;
  8. PAINTSTRUCT ps;
  9. RECT r;
  10.  
  11. class Obiekt {
  12. protected:
  13.     int Kat, PozX, PozY;
  14.     float SpeedX, SpeedY;
  15. public:
  16.     Obiekt(int PX, int PY, int SpX, int SpY, int K);
  17.     POINT Pos();
  18.     virtual void Rysuj(HDC DC) = 0;
  19.     void Przesun();
  20. };
  21.  
  22. Obiekt::Obiekt(int PX, int PY, int SpX, int SpY, int K) :
  23.     PozX(PX),
  24.     PozY(PY),
  25.     SpeedX(SpX),
  26.     SpeedY(SpY),
  27.     Kat(K)
  28. { }
  29.  
  30. POINT Obiekt::Pos() {
  31.     POINT Pozycja;
  32.     Pozycja.x = PozX;
  33.     Pozycja.y = PozY;
  34.     return Pozycja;
  35. }
  36.  
  37. void Obiekt::Przesun() {
  38.     if (SpeedX > 0) PozX += floor(SpeedX);
  39.     else PozX += ceil(SpeedX);
  40.     if (SpeedY > 0) PozY += floor(SpeedY);
  41.     else PozY += ceil(SpeedY);
  42.     PozX = (PozX+500)%500;
  43.     PozY = (PozY+500)%500;
  44. }
  45.  
  46. class Lazor : public Obiekt {
  47.     int TTL;
  48. public:
  49.     Lazor(int PX, int PY, int SpX, int SpY, int K);
  50.     void Rysuj(HDC DC);
  51.     void Odejmij(int Ile) { TTL -= Ile; };
  52.     int Time() { return TTL; }
  53. };
  54.  
  55. Lazor::Lazor(int PX, int PY, int SpX, int SpY, int K) :
  56.     Obiekt(PX, PY, SpX, SpY, K),
  57.     TTL(1000)
  58. { }
  59.  
  60.  
  61. void Lazor::Rysuj(HDC DC) {
  62.     MoveToEx(DC, PozX-5*sin(Kat*3.14/180), PozY+5*cos(Kat*3.14/180), NULL);
  63.     LineTo(DC, PozX+5*sin(Kat*3.14/180), PozY-5*cos(Kat*3.14/180));
  64. }
  65.  
  66. vector<Lazor> Lazors;
  67.  
  68. class Asteroid : public Obiekt {
  69.     int Size;
  70. public:
  71.     Asteroid(int PX, int PY, int SpX, int SpY, int K, int S);
  72.     void Rysuj(HDC DC);
  73.     int Angle() { return Kat; }
  74.     bool Collision();
  75.     int Rozm() { return Size; }
  76. };
  77.  
  78. Asteroid::Asteroid(int PX, int PY, int SpX, int SpY, int K, int S) :
  79.     Obiekt(PX, PY, SpX, SpY, K),
  80.     Size(S)
  81. { }
  82.  
  83. void Asteroid::Rysuj(HDC DC) {
  84.     Ellipse(DC, PozX-Size*8, PozY-Size*8, PozX+Size*8, PozY+Size*8);
  85. }
  86.  
  87. bool Asteroid::Collision() {
  88.     for (int i = Lazors.size()-1; i >= 0 ; --i) {
  89.         if (pow(PozX-Lazors[i].Pos().x, 2.0) + pow(PozY-Lazors[i].Pos().y, 2.0) <= pow(Size*8, 2.0)) {
  90.             Lazors.erase(Lazors.begin() + i);
  91.             return true;
  92.         }
  93.     }
  94.  
  95.     return false;
  96. }
  97.  
  98. vector<Asteroid> Asteroids;
  99.  
  100. class Statek : public Obiekt{  
  101.     int lastShot;
  102. public:
  103.     Statek();
  104.     void Obroc(int Ile);
  105.     void Rysuj(HDC DC);
  106.     void Przyspiesz();
  107.     void Strzal();
  108. };
  109.  
  110. Statek::Statek() :
  111.     Obiekt(250, 250, 0, 0, 0),
  112.     lastShot(0)
  113. { }
  114.  
  115. void Statek::Obroc(int Ile) {
  116.     Kat = (Kat + Ile) % 360;
  117. }
  118.  
  119. void Statek::Rysuj(HDC DC) {
  120.     POINT P[3];
  121.     P[0].x = PozX+(11*sin((240+Kat)*3.14/180)); P[0].y = PozY-(11*cos((240+Kat)*3.14/180)); P[1].x = PozX+(11*sin((120+Kat)*3.14/180)); P[1].y = PozY-(11*cos((120+Kat)*3.14/180)); P[2].x = PozX+(15*sin(Kat*3.14/180)); P[2].y = PozY-(15*cos(Kat*3.14/180));
  122.     Polygon(DC, P, 3);
  123. }
  124.  
  125. void Statek::Przyspiesz() {
  126.     float newSX = SpeedX + 0.3*sin(Kat*3.14/180);
  127.     float newSY = SpeedY - 0.3*cos(Kat*3.14/180);
  128.     if (pow(newSX, 2) + pow(newSY, 2) < 100) {
  129.         SpeedX = newSX;
  130.         SpeedY = newSY;
  131.     }
  132. }
  133.  
  134. void Statek::Strzal() {
  135.     SYSTEMTIME czas;
  136.     GetSystemTime(&czas);
  137.     if (abs(czas.wMilliseconds-lastShot) > 200) {
  138.         lastShot = czas.wMilliseconds;
  139.         Lazors.push_back(Lazor(PozX, PozY, 10*sin(Kat*3.14/180), -10*cos(Kat*3.14/180), Kat));
  140.     }
  141. }
  142.  
  143. Statek Player;
  144. float sx, sy;
  145. int x, y, k, score = 0;
  146. int rnd = 10, invincible = 0;
  147. wchar_t wynik[30];
  148.  
  149. LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  150.     switch(msg) {
  151.     case WM_CREATE:
  152.         SetTimer(hWnd, 1, 10, NULL);
  153.     break;
  154.     case WM_TIMER:
  155.         Player.Przesun();
  156.         if (invincible > 0) invincible -= 10;
  157.         for (int i = Lazors.size()-1; i >= 0 ; --i) {
  158.             Lazors[i].Przesun();
  159.             Lazors[i].Odejmij(10);
  160.             if (Lazors[i].Time() <= 0) Lazors.erase(Lazors.begin()+i);
  161.         }
  162.         if (Asteroids.size() < 5) {
  163.             rnd *= 2;
  164.             srand(rnd);
  165.             x = rand()%500;
  166.             rnd *= 2;
  167.             srand(rnd);
  168.             y = rand()%500;
  169.             rnd *= 2;
  170.             srand(rnd);
  171.             if (x <= y) x = 500 * rand()%2;
  172.             else y = 500 * rand()%2;
  173.             rnd *= 2;
  174.             srand(rnd);
  175.             k = rand()%360;
  176.             sx = 3*sin(k*3.14/180);
  177.             sy = -3*cos(k*3.14/180);
  178.             Asteroids.push_back(Asteroid(x, y, sx, sy, k, 3));
  179.         }
  180.         for (int i = Asteroids.size()-1; i >= 0 ; --i) {
  181.             Asteroids[i].Przesun();
  182.             if (invincible == 0 && pow(Player.Pos().x-Asteroids[i].Pos().x, 2.0) + pow(Player.Pos().y-Asteroids[i].Pos().y, 2.0) <= pow(Asteroids[i].Rozm()*8, 2.0)) {
  183.                 invincible = 500;
  184.                 score -= 1000;
  185.             }
  186.             if (Asteroids[i].Collision()) {
  187.                 if (Asteroids[i].Rozm() > 1) {
  188.                     Asteroids.push_back(Asteroid(Asteroids[i].Pos().x, Asteroids[i].Pos().y, 3*sin((Asteroids[i].Angle()+20)*3.14/180), -3*cos((Asteroids[i].Angle()+20)*3.14/180), Asteroids[i].Angle()+20, Asteroids[i].Rozm()-1));
  189.                     Asteroids.push_back(Asteroid(Asteroids[i].Pos().x, Asteroids[i].Pos().y, 3*sin((Asteroids[i].Angle()-20)*3.14/180), -3*cos((Asteroids[i].Angle()-20)*3.14/180), Asteroids[i].Angle()-20, Asteroids[i].Rozm()-1));
  190.                 }
  191.                 Asteroids.erase(Asteroids.begin() + i);
  192.                 score += 50;
  193.             }
  194.         }
  195.         GetClientRect(hWnd, &r);
  196.         InvalidateRect(hWnd, &r, true);
  197.     break;
  198.     case WM_PAINT:
  199.         dc = BeginPaint(hWnd, &ps);
  200.         GetClientRect(hWnd, &r);
  201.         for (int i = 0; i < Lazors.size(); ++i) Lazors[i].Rysuj(dc);
  202.         for (int i = 0; i < Asteroids.size(); ++i) Asteroids[i].Rysuj(dc);
  203.         Player.Rysuj(dc);
  204.         swprintf(wynik, L"%d", score);
  205.         SetBkMode(dc, TRANSPARENT);
  206.         TextOut(dc, 10, 10, wynik, 15);
  207.         EndPaint(hWnd, &ps);
  208.     break;
  209.     case WM_KEYDOWN:
  210.         if (GetKeyState(VK_UP) & 0xF0) Player.Przyspiesz();
  211.         if (GetKeyState(VK_LEFT) & 0xF0) Player.Obroc(-5);
  212.         if (GetKeyState(VK_RIGHT) & 0xF0) Player.Obroc(5);
  213.         if (GetKeyState(VK_SPACE) & 0xF0) Player.Strzal();
  214.     break;
  215.     case WM_DESTROY:
  216.         KillTimer(hWnd, 1);
  217.         PostQuitMessage(0);
  218.     break;
  219.     default:
  220.         return DefWindowProc(hWnd, msg, wParam, lParam);
  221.     break;
  222.     }
  223.     return 0;
  224. }
  225.  
  226.  
  227. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
  228.     HWND hWnd;
  229.     MSG msg;
  230.     WNDCLASSEX wc;
  231.     wc.cbClsExtra = 0;
  232.     wc.cbSize = sizeof(WNDCLASSEX);
  233.     wc.cbWndExtra = 0;
  234.     wc.hbrBackground = CreateSolidBrush(RGB(255,255,255));
  235.     wc.hCursor = 0;
  236.     wc.hIcon = 0;
  237.     wc.hIconSm = 0;
  238.     wc.hInstance = hInstance;
  239.     wc.lpfnWndProc = WndProc;
  240.     wc.lpszClassName = L"KLASA";
  241.     wc.lpszMenuName = 0;
  242.     wc.style = 0;
  243.    
  244.     if(!RegisterClassEx(&wc)) return 0;
  245.  
  246.     hWnd = CreateWindowEx(0, L"KLASA", L"Asteroids", WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME, 100, 100, 500, 500, 0, 0, hInstance, 0);
  247.     ShowWindow(hWnd, SW_SHOW);
  248.     UpdateWindow(hWnd);
  249.    
  250.     while (GetMessage(&msg, NULL, 0, 0) != 0) {
  251.         TranslateMessage(&msg);
  252.         DispatchMessage(&msg);
  253.     }
  254.    
  255.     UnregisterClass(L"KLASA", hInstance);
  256.  
  257.     return 0;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement