Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Primary navigation
Search or go to…
Project
C
Cpp_training_material
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Privacy statement
Keyboard shortcuts
?
What's new
6
Snippets
Groups
Projects
Show more breadcrumbs
AlaskaLinuxUser
Cpp_training_material
Commits
aa1969af
Commit
aa1969af
authored
6 years ago
by
alaskalinuxuser
Browse files
Options
Downloads
Patches
Plain Diff
Section 3 completed, added timebar and text.
parent
b29e55e3
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
Timber/Timber.cpp
+184
-105
184 additions, 105 deletions
Timber/Timber.cpp
Timber/Timber.o
+0
-0
0 additions, 0 deletions
Timber/Timber.o
Timber/timber-app
+0
-0
0 additions, 0 deletions
Timber/timber-app
with
184 additions
and
105 deletions
Timber/Timber.cpp
+
184
−
105
View file @
aa1969af
#include
<SFML/Graphics.hpp>
#include
<sstream>
int
main
()
{
sf
::
RenderWindow
window
(
sf
::
VideoMode
(
1280
,
1024
),
"Timber!"
);
int
sWidth
=
1024
;
int
sHeight
=
786
;
sf
::
RenderWindow
window
(
sf
::
VideoMode
(
sWidth
,
sHeight
),
"Timber!"
);
// Create a texture for our background and load it.
sf
::
Texture
textureBackground
;
...
...
@@ -20,7 +23,7 @@ int main()
textureTree
.
loadFromFile
(
"graphics/tree.png"
);
sf
::
Sprite
spriteTree
;
spriteTree
.
setTexture
(
textureTree
);
spriteTree
.
setPosition
(
640
,
0
);
spriteTree
.
setPosition
(
sWidth
/
2.0
f
,
0
);
// Add some clouds
sf
::
Texture
textureCloud
;
...
...
@@ -31,9 +34,9 @@ int main()
spriteCloudOne
.
setTexture
(
textureCloud
);
spriteCloudTwo
.
setTexture
(
textureCloud
);
spriteCloudThree
.
setTexture
(
textureCloud
);
spriteCloudOne
.
setPosition
(
0
,
250
);
spriteCloudTwo
.
setPosition
(
8
0
,
125
);
spriteCloudThree
.
setPosition
(
1
0
,
0
);
spriteCloudOne
.
setPosition
(
sWidth
+
10
0
,
250
);
spriteCloudTwo
.
setPosition
(
sWidth
+
10
0
,
125
);
spriteCloudThree
.
setPosition
(
sWidth
+
10
0
,
0
);
bool
cloudOneActive
=
false
;
bool
cloudTwoActive
=
false
;
bool
cloudThreeActive
=
false
;
...
...
@@ -46,13 +49,52 @@ int main()
textureBee
.
loadFromFile
(
"graphics/insect.png"
);
sf
::
Sprite
spriteBee
;
spriteBee
.
setTexture
(
textureBee
);
spriteBee
.
setPosition
(
5
0
,
600
);
spriteBee
.
setPosition
(
sWidth
-
10
0
,
600
);
bool
beeActive
=
false
;
float
beeSpeed
=
0.0
f
;
// Add a font.
sf
::
Font
font
;
font
.
loadFromFile
(
"fonts/KOMIKAP_.ttf"
);
// Add startText
sf
::
Text
startText
;
startText
.
setString
(
"Press the Enter key to start!"
);
startText
.
setCharacterSize
(
40
);
startText
.
setColor
(
sf
::
Color
::
White
);
startText
.
setFont
(
font
);
// Set position based on center
sf
::
FloatRect
textRect
=
startText
.
getLocalBounds
();
startText
.
setOrigin
(
textRect
.
left
+
textRect
.
width
/
2.0
f
,
textRect
.
top
+
textRect
.
height
/
2.0
f
);
startText
.
setPosition
(
sWidth
/
2.0
f
,
sHeight
/
2.0
f
);
// Add scoreText
int
scoreInt
=
0
;
sf
::
Text
scoreText
;
scoreText
.
setString
(
"Score = 0"
);
scoreText
.
setCharacterSize
(
40
);
scoreText
.
setColor
(
sf
::
Color
::
White
);
scoreText
.
setFont
(
font
);
scoreText
.
setPosition
(
10
,
10
);
// Add a time bar
sf
::
RectangleShape
timeBar
;
float
timeBarStartWidth
=
400
;
float
timeBarHeight
=
40
;
timeBar
.
setSize
(
sf
::
Vector2f
(
timeBarStartWidth
,
timeBarHeight
));
timeBar
.
setFillColor
(
sf
::
Color
::
Blue
);
timeBar
.
setPosition
(
sWidth
/
2.0
f
-
(
timeBarStartWidth
/
2.0
f
),
sHeight
-
45
);
sf
::
Time
gameTime
;
float
timeRemains
=
6.0
f
;
float
widthPerSecond
=
timeBarStartWidth
/
timeRemains
;
// Time control
sf
::
Clock
clock
;
// Pause the game
bool
gamePaused
=
true
;
while
(
window
.
isOpen
())
{
if
(
sf
::
Keyboard
::
isKeyPressed
(
sf
::
Keyboard
::
Escape
))
{
...
...
@@ -60,6 +102,15 @@ int main()
window
.
close
();
}
if
(
sf
::
Keyboard
::
isKeyPressed
(
sf
::
Keyboard
::
Return
))
{
// Return key is pressed: pause/unpause.
scoreInt
=
0
;
timeRemains
=
6.0
f
;
gamePaused
=
false
;
}
if
(
sf
::
Mouse
::
isButtonPressed
(
sf
::
Mouse
::
Right
))
{
// right mouse button is pressed: exit
window
.
close
();
...
...
@@ -71,8 +122,23 @@ int main()
window
.
close
();
}
if
(
!
gamePaused
)
{
sf
::
Time
dt
=
clock
.
restart
();
// Update the time bar.
timeRemains
-=
dt
.
asSeconds
();
timeBar
.
setSize
(
sf
::
Vector2f
(
widthPerSecond
*
timeRemains
,
timeBarHeight
));
if
(
timeRemains
<=
0.0
f
)
{
gamePaused
=
true
;
startText
.
setString
(
"Time Over! Press Enter to play again!"
);
sf
::
FloatRect
textRect
=
startText
.
getLocalBounds
();
startText
.
setOrigin
(
textRect
.
left
+
textRect
.
width
/
2.0
f
,
textRect
.
top
+
textRect
.
height
/
2.0
f
);
startText
.
setPosition
(
sWidth
/
2.0
f
,
sHeight
/
2.0
f
);
}
if
(
!
beeActive
)
{
// Since the bee is not active.
// Seed our random number with time from system.
...
...
@@ -91,7 +157,7 @@ int main()
spriteBee
.
getPosition
().
y
);
}
if
(
spriteBee
.
getPosition
().
x
>
1
3
00
)
{
if
(
spriteBee
.
getPosition
().
x
>
sWidth
+
100
)
{
// The bee is now off screen. Let's move it back.
beeActive
=
false
;
// This will then call the setup for the bee again.
...
...
@@ -106,7 +172,7 @@ int main()
// Now set the Clouds's starting position.
srand
((
int
)
time
(
0
)
*
10
);
float
height
=
((
rand
()
%
300
)
+
10
);
spriteCloudOne
.
setPosition
(
1
3
00
,
height
);
//off screen on
lef
t.
spriteCloudOne
.
setPosition
(
sWidth
+
100
,
height
);
//off screen on
righ
t.
cloudOneActive
=
true
;
// Make the cloud active, so only runs once.
}
else
{
// Since the Cloud is active, let's move it.
...
...
@@ -130,7 +196,7 @@ int main()
// Now set the Clouds's starting position.
srand
((
int
)
time
(
0
)
*
20
);
float
height
=
((
rand
()
%
200
)
+
10
);
spriteCloudTwo
.
setPosition
(
1
3
00
,
height
);
//off screen on
lef
t.
spriteCloudTwo
.
setPosition
(
sWidth
+
100
,
height
);
//off screen on
righ
t.
cloudTwoActive
=
true
;
// Make the cloud active, so only runs once.
}
else
{
// Since the Cloud is active, let's move it.
...
...
@@ -154,7 +220,7 @@ int main()
// Now set the Clouds's starting position.
srand
((
int
)
time
(
0
)
*
10
);
float
height
=
((
rand
()
%
250
)
+
10
);
spriteCloudThree
.
setPosition
(
1
3
00
,
height
);
//off screen on
lef
t.
spriteCloudThree
.
setPosition
(
sWidth
+
100
,
height
);
//off screen on
righ
t.
cloudThreeActive
=
true
;
// Make the cloud active, so only runs once.
}
else
{
// Since the Cloud is active, let's move it.
...
...
@@ -168,6 +234,11 @@ int main()
cloudThreeActive
=
false
;
// This will then call the setup for the cloud again.
}
}
std
::
stringstream
ss
;
ss
<<
"Score = "
<<
scoreInt
;
scoreText
.
setString
(
ss
.
str
());
window
.
clear
();
window
.
draw
(
spriteBackground
);
...
...
@@ -176,7 +247,15 @@ int main()
window
.
draw
(
spriteCloudThree
);
window
.
draw
(
spriteTree
);
window
.
draw
(
spriteBee
);
window
.
draw
(
scoreText
);
window
.
draw
(
timeBar
);
if
(
gamePaused
)
{
window
.
draw
(
startText
);
}
window
.
display
();
}
return
0
;
...
...
This diff is collapsed.
Click to expand it.
Timber/Timber.o
+
0
−
0
View file @
aa1969af
No preview for this file type
This diff is collapsed.
Click to expand it.
Timber/timber-app
+
0
−
0
View file @
aa1969af
No preview for this file type
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment