-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.h
148 lines (110 loc) · 2.35 KB
/
player.h
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>
#include<string.h>
#include<cmath>
#include <iostream>
using namespace sf;
using namespace std;
class Bullet{
public:
Texture tex2;
Sprite sprite1;
float speed2;
int x1,y1;
bool isactive=true;
int windowHeight = 780;
int windowWidth = 780;
Bullet(string png_path)
{
tex2.loadFromFile(png_path);
sprite1.setTexture(tex2);
x1=365;y1=680;
sprite1.setPosition(sf::Vector2f(x1,y1));
sprite1.setScale(0.5,0.5);
}
FloatRect getGlobalBounds() const {
return sprite1.getGlobalBounds();
}
void movebullet(std::string s1){
float delta_x1=0,delta_y1=0;
if(s1=="s")
{delta_y1 = -1;
speed2 = 1;
}
else if(s1=="l")
{delta_x1 = -1;
speed2 = 0.5;
}
else if(s1=="r"){
delta_x1 = 1;
speed2 = 0.5;}
else if(s1=="u"){
delta_y1=-1;
speed2 = 0.5;}
else if(s1=="d"){
delta_y1=1;
speed2 = 0.5;}
delta_y1*=speed2;
delta_x1*=speed2;
sprite1.move(sf::Vector2f(delta_x1, delta_y1));
}
};
class Player{
public:
Bullet * b;
Texture tex;
Sprite sprite;
bool iscollided = false;
float speed=0.5;
int x,y;
Player(std::string png_path)
{
b=new Bullet("img/laserRed08.png");
tex.loadFromFile(png_path);
sprite.setTexture(tex);
x=340;y=700;
sprite.setPosition(sf::Vector2f(x,y));
sprite.setScale(0.75,0.75);
}
FloatRect getGlobalBounds() const {
return sprite.getGlobalBounds();
}
void move(std::string s){
float delta_x=0,delta_y=0;
Vector2f newPos = sprite.getPosition() + Vector2f(delta_x, delta_y);
// Wrap around the window
if (newPos.x < 0)
newPos.x = 780;
else if (newPos.x > 780)
newPos.x = 0;
if (newPos.y < 0)
newPos.y = 780;
else if (newPos.y > 780)
newPos.y = 0;
sprite.setPosition(newPos);
if(s=="l")
{ delta_x = -1;
b->movebullet("l");
}
else if(s=="r"){
delta_x = 1;
b->movebullet("r");}
if(s=="u"){
delta_y=-1;
b->movebullet("u");}
else if(s=="d"){
delta_y=1;
b->movebullet("d");}
delta_x*=speed;
delta_y*=speed;
sprite.move(sf::Vector2f(delta_x, delta_y));
}
void shootbullet(){ // New function to handle bullet shooting
b->movebullet("s");
if (b->sprite1.getPosition().y < 0 ||b->sprite1.getPosition().x < 0) {
b->sprite1.setPosition(this->sprite.getPosition().x+40, this->sprite.getPosition().y-1);
}
}
};
#endif