-
Notifications
You must be signed in to change notification settings - Fork 0
/
frog.cpp
125 lines (106 loc) · 2.49 KB
/
frog.cpp
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
/*
* Frog.hpp
*
* Description: This file contains the declaration of the Frog class, representing the player-controlled frog in the game.
* The class defines the frog's position, movement, hitbox, and other related functionalities.
*
* Author: Brandon Xu
* Date: 4/28/23
*/
#include "frog.hpp"
// Default constructor
Frog::Frog() : x(550) , y(749)
{
is_hit = false;
texture.loadFromFile("images/Frog.png");
sprite.setTexture(texture);
}
// Render frog onto screen
void Frog::draw(sf::RenderWindow& window)
{
sprite.setPosition(x, y);
window.draw(sprite);
}
// Player controlled movement
void Frog::move()
{
const int cell_size = 50;
const int screen_width = 1200;
const int screen_height = 800;
if (!control_keys[1] && sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
if (y + cell_size < screen_height - cell_size)
{
y += cell_size;
}
}
else if (!control_keys[0] && sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
if (y >= 0)
{
y -= cell_size;
}
}
else if (!control_keys[2] && sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
if (x - cell_size >= 0)
{
x -= cell_size;
}
}
else if (!control_keys[3] && sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
if (x + cell_size < screen_width)
{
x += cell_size;
}
}
// Update control key states
control_keys[0] = sf::Keyboard::isKeyPressed(sf::Keyboard::Up);
control_keys[1] = sf::Keyboard::isKeyPressed(sf::Keyboard::Down);
control_keys[2] = sf::Keyboard::isKeyPressed(sf::Keyboard::Left);
control_keys[3] = sf::Keyboard::isKeyPressed(sf::Keyboard::Right);
// NOTE: commented out to allow frog to die when hit instead of resetting
//if (this->get_hit() == true) //collision detected
//{
// reset(550, 749); //resets to starting position
//}
}
void Frog::move(int logSpeed) //move function used within log section
{
x += logSpeed;
}
// Getters
int Frog::get_x()
{
return x;
}
int Frog::get_y()
{
return y;
}
void Frog::set_x(int newX) {
this->x = newX;
}
void Frog::set_y(int newY) {
this->y = newY;
}
sf::IntRect Frog::get_hitbox()
{
return sf::IntRect(x, y, 49, 49);
}
bool Frog::get_hit()
{
return is_hit;
}
// Indicate a collision with frog
void Frog::set_hit()
{
is_hit = true;
}
void Frog::reset(int new_x, int new_y)
{
x = new_x;
y = new_y;
is_hit = false;
}