-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenemy.cpp
More file actions
105 lines (94 loc) · 3.36 KB
/
enemy.cpp
File metadata and controls
105 lines (94 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<SFML/Graphics.hpp>
#include<iostream>
using namespace std;
int main()
{
enum directions {u,ur,r,dr,ul,l,dl,d};
bool shoot=false;
int shootx,shooty;
int enemyxcounter=0;
sf::Vector2i source(0,u);
sf::RenderWindow window(sf::VideoMode(1280,768),"first window",sf::Style::Default);
window.setKeyRepeatEnabled(false);
window.setFramerateLimit(60);
sf::Texture texture;
sf::Texture background;
background.loadFromFile("space.png");
texture.loadFromFile("shipsprite.png");
sf::Sprite sprite;
sf::Sprite backsp;
sf::Sprite enemy;
backsp.setTexture(background);
sprite.setTexture(texture);
enemy.setTexture(texture);
sf::CircleShape circle(5);
circle.setOrigin(5,5);
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
source.y=u;
shootx=0;
shooty=-10;
sprite.move(0.0f,-5.0f);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
source.y=d;
shootx=0;
shooty=10;
sprite.move(0.0f,5.0f);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
source.y=l;
shootx=-10;
shooty=0;
sprite.move(-5.0f,0.0f);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
source.y=r;
shootx=10;
shooty=0;
sprite.move(5.0f,0.0f);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
circle.setPosition(sf::Vector2f(sprite.getPosition().x+32,sprite.getPosition().y+32));
shoot=true;
}
source.x++;
if(source.x*64>=texture.getSize().x)
source.x=0;
enemyxcounter++;
if(enemyxcounter*64>=texture.getSize().x)
enemyxcounter=0;
enemy.move(-2.0f,0.0f);
sprite.setTextureRect(sf::IntRect(source.x*64,source.y*64,64,64));
enemy.setTextureRect(sf::IntRect(enemyxcounter*64,5*64,64,64));
if(enemy.getPosition().x<=0)
enemy.setPosition(window.getSize().x-128,window.getSize().y-704);
window.draw(backsp);
window.draw(sprite);
window.draw(enemy);
if(shoot==true && circle.getPosition().x>0 && circle.getPosition().x<window.getSize().x && circle.getPosition().y>0 && circle.getPosition().y<window.getSize().y)
{
circle.move(shootx,shooty);
window.draw(circle);
}
window.display();
window.clear();
}
return 0;
}