Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <math.h>
- #include <vector>
- using namespace std;
- HDC dc;
- PAINTSTRUCT ps;
- RECT r;
- class Obiekt {
- protected:
- int Kat, PozX, PozY;
- float SpeedX, SpeedY;
- public:
- Obiekt(int PX, int PY, int SpX, int SpY, int K);
- POINT Pos();
- virtual void Rysuj(HDC DC) = 0;
- void Przesun();
- };
- Obiekt::Obiekt(int PX, int PY, int SpX, int SpY, int K) :
- PozX(PX),
- PozY(PY),
- SpeedX(SpX),
- SpeedY(SpY),
- Kat(K)
- { }
- POINT Obiekt::Pos() {
- POINT Pozycja;
- Pozycja.x = PozX;
- Pozycja.y = PozY;
- return Pozycja;
- }
- void Obiekt::Przesun() {
- if (SpeedX > 0) PozX += floor(SpeedX);
- else PozX += ceil(SpeedX);
- if (SpeedY > 0) PozY += floor(SpeedY);
- else PozY += ceil(SpeedY);
- PozX = (PozX+500)%500;
- PozY = (PozY+500)%500;
- }
- class Lazor : public Obiekt {
- int TTL;
- public:
- Lazor(int PX, int PY, int SpX, int SpY, int K);
- void Rysuj(HDC DC);
- void Odejmij(int Ile) { TTL -= Ile; };
- int Time() { return TTL; }
- };
- Lazor::Lazor(int PX, int PY, int SpX, int SpY, int K) :
- Obiekt(PX, PY, SpX, SpY, K),
- TTL(1000)
- { }
- void Lazor::Rysuj(HDC DC) {
- MoveToEx(DC, PozX-5*sin(Kat*3.14/180), PozY+5*cos(Kat*3.14/180), NULL);
- LineTo(DC, PozX+5*sin(Kat*3.14/180), PozY-5*cos(Kat*3.14/180));
- }
- vector<Lazor> Lazors;
- class Asteroid : public Obiekt {
- int Size;
- public:
- Asteroid(int PX, int PY, int SpX, int SpY, int K, int S);
- void Rysuj(HDC DC);
- int Angle() { return Kat; }
- bool Collision();
- int Rozm() { return Size; }
- };
- Asteroid::Asteroid(int PX, int PY, int SpX, int SpY, int K, int S) :
- Obiekt(PX, PY, SpX, SpY, K),
- Size(S)
- { }
- void Asteroid::Rysuj(HDC DC) {
- Ellipse(DC, PozX-Size*8, PozY-Size*8, PozX+Size*8, PozY+Size*8);
- }
- bool Asteroid::Collision() {
- for (int i = Lazors.size()-1; i >= 0 ; --i) {
- if (pow(PozX-Lazors[i].Pos().x, 2.0) + pow(PozY-Lazors[i].Pos().y, 2.0) <= pow(Size*8, 2.0)) {
- Lazors.erase(Lazors.begin() + i);
- return true;
- }
- }
- return false;
- }
- vector<Asteroid> Asteroids;
- class Statek : public Obiekt{
- int lastShot;
- public:
- Statek();
- void Obroc(int Ile);
- void Rysuj(HDC DC);
- void Przyspiesz();
- void Strzal();
- };
- Statek::Statek() :
- Obiekt(250, 250, 0, 0, 0),
- lastShot(0)
- { }
- void Statek::Obroc(int Ile) {
- Kat = (Kat + Ile) % 360;
- }
- void Statek::Rysuj(HDC DC) {
- POINT P[3];
- 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));
- Polygon(DC, P, 3);
- }
- void Statek::Przyspiesz() {
- float newSX = SpeedX + 0.3*sin(Kat*3.14/180);
- float newSY = SpeedY - 0.3*cos(Kat*3.14/180);
- if (pow(newSX, 2) + pow(newSY, 2) < 100) {
- SpeedX = newSX;
- SpeedY = newSY;
- }
- }
- void Statek::Strzal() {
- SYSTEMTIME czas;
- GetSystemTime(&czas);
- if (abs(czas.wMilliseconds-lastShot) > 200) {
- lastShot = czas.wMilliseconds;
- Lazors.push_back(Lazor(PozX, PozY, 10*sin(Kat*3.14/180), -10*cos(Kat*3.14/180), Kat));
- }
- }
- Statek Player;
- float sx, sy;
- int x, y, k, score = 0;
- int rnd = 10, invincible = 0;
- wchar_t wynik[30];
- LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
- switch(msg) {
- case WM_CREATE:
- SetTimer(hWnd, 1, 10, NULL);
- break;
- case WM_TIMER:
- Player.Przesun();
- if (invincible > 0) invincible -= 10;
- for (int i = Lazors.size()-1; i >= 0 ; --i) {
- Lazors[i].Przesun();
- Lazors[i].Odejmij(10);
- if (Lazors[i].Time() <= 0) Lazors.erase(Lazors.begin()+i);
- }
- if (Asteroids.size() < 5) {
- rnd *= 2;
- srand(rnd);
- x = rand()%500;
- rnd *= 2;
- srand(rnd);
- y = rand()%500;
- rnd *= 2;
- srand(rnd);
- if (x <= y) x = 500 * rand()%2;
- else y = 500 * rand()%2;
- rnd *= 2;
- srand(rnd);
- k = rand()%360;
- sx = 3*sin(k*3.14/180);
- sy = -3*cos(k*3.14/180);
- Asteroids.push_back(Asteroid(x, y, sx, sy, k, 3));
- }
- for (int i = Asteroids.size()-1; i >= 0 ; --i) {
- Asteroids[i].Przesun();
- 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)) {
- invincible = 500;
- score -= 1000;
- }
- if (Asteroids[i].Collision()) {
- if (Asteroids[i].Rozm() > 1) {
- 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));
- 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));
- }
- Asteroids.erase(Asteroids.begin() + i);
- score += 50;
- }
- }
- GetClientRect(hWnd, &r);
- InvalidateRect(hWnd, &r, true);
- break;
- case WM_PAINT:
- dc = BeginPaint(hWnd, &ps);
- GetClientRect(hWnd, &r);
- for (int i = 0; i < Lazors.size(); ++i) Lazors[i].Rysuj(dc);
- for (int i = 0; i < Asteroids.size(); ++i) Asteroids[i].Rysuj(dc);
- Player.Rysuj(dc);
- swprintf(wynik, L"%d", score);
- SetBkMode(dc, TRANSPARENT);
- TextOut(dc, 10, 10, wynik, 15);
- EndPaint(hWnd, &ps);
- break;
- case WM_KEYDOWN:
- if (GetKeyState(VK_UP) & 0xF0) Player.Przyspiesz();
- if (GetKeyState(VK_LEFT) & 0xF0) Player.Obroc(-5);
- if (GetKeyState(VK_RIGHT) & 0xF0) Player.Obroc(5);
- if (GetKeyState(VK_SPACE) & 0xF0) Player.Strzal();
- break;
- case WM_DESTROY:
- KillTimer(hWnd, 1);
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, msg, wParam, lParam);
- break;
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
- HWND hWnd;
- MSG msg;
- WNDCLASSEX wc;
- wc.cbClsExtra = 0;
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.cbWndExtra = 0;
- wc.hbrBackground = CreateSolidBrush(RGB(255,255,255));
- wc.hCursor = 0;
- wc.hIcon = 0;
- wc.hIconSm = 0;
- wc.hInstance = hInstance;
- wc.lpfnWndProc = WndProc;
- wc.lpszClassName = L"KLASA";
- wc.lpszMenuName = 0;
- wc.style = 0;
- if(!RegisterClassEx(&wc)) return 0;
- hWnd = CreateWindowEx(0, L"KLASA", L"Asteroids", WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME, 100, 100, 500, 500, 0, 0, hInstance, 0);
- ShowWindow(hWnd, SW_SHOW);
- UpdateWindow(hWnd);
- while (GetMessage(&msg, NULL, 0, 0) != 0) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- UnregisterClass(L"KLASA", hInstance);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement