Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <time.h>
- #include <math.h>
- using namespace std;
- using namespace sf;
- sf::RenderWindow window(sf::VideoMode(300, 800), "Space Invaders");
- float pi = 3.14159;
- class spelare
- {
- Vector2f pos, dim;
- RectangleShape skepp, kanon;
- clock_t t0;
- float v, vdir;
- public:
- spelare(float lengd, float bredd, float xpos, float ypos)
- {
- dim.x = lengd; dim.y = bredd;
- pos.x = xpos; pos.y = ypos;
- v = 0; vdir = 0;
- skepp.setSize(dim);
- skepp.setOrigin(dim.x * 2 / 5, dim.y / 2);
- kanon.setOrigin(dim.x * 2 / 5, dim.y / 2);
- kanon.setSize(Vector2f(dim.x / 5, dim.y));
- kanon.setFillColor(Color::Red);
- }
- void uppdatera()
- {
- clock_t dt = clock() - t0;
- Vector2f vrp = pos;
- vrp.x += 0.7*dim.x*cos(vdir*pi / 180);
- vrp.y += 0.7*dim.x*sin(vdir*pi / 180);
- t0 += dt;
- pos.x += v * cos(vdir*pi / 180)*dt;
- pos.y += v * sin(vdir*pi / 180)*dt;
- skepp.setPosition(pos);
- skepp.setRotation(vdir);
- kanon.setPosition(vrp);
- kanon.setRotation(vdir);
- }
- void rita()
- {
- window.draw(skepp);
- window.draw(kanon);
- }
- void acc(float a)
- {
- v += a;
- }
- };
- int main()
- {
- clock_t t0, t1;
- spelare skepp(85, 35, 200, 200);
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- else if (event.type == sf::Event::KeyPressed)
- if (event.key.code == sf::Keyboard::Right)
- {
- skepp.acc(0.001);
- }
- else if (event.key.code == sf::Keyboard::Left)
- {
- skepp.acc(-0.001);
- }
- else
- {
- skepp.acc(0);
- }
- }
- window.clear();
- skepp.uppdatera();
- skepp.rita();
- window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement