3
3
the basic elements of the game.
4
4
"""
5
5
6
+ # pylint: disable=invalid-name
7
+
6
8
import random as rd
7
9
8
10
import pygame as pg
@@ -25,16 +27,20 @@ def __init__(self, x_position: float, y_position: float) -> None:
25
27
self .rect : pg .Rect
26
28
self .current_image : pg .Surface
27
29
self .counter : Counter = Counter ()
30
+ self .OBJECT_SPEED : float = config .object_speed
28
31
29
- def update (self , speed : float = config . object_speed ) -> None :
32
+ def update (self ) -> None :
30
33
"""Update the position of the element in the game."""
31
- self .move (speed )
34
+ self .move ()
32
35
self .rect .update ((self .x_position , self .y_position ), self .rect .size )
33
36
config .window .blit (self .current_image , self .rect )
34
37
35
- def move (self , speed : float ) -> None :
38
+ if self .rect .right <= 0 :
39
+ self .kill ()
40
+
41
+ def move (self ) -> None :
36
42
"""Move the element in the game."""
37
- self .x_position -= speed
43
+ self .x_position -= self . OBJECT_SPEED
38
44
39
45
40
46
class Cactus (GameElement ):
@@ -78,6 +84,18 @@ def __init__(self) -> None:
78
84
self .image = seperate_images (load_image ("birds.png" )[0 ], (2 , 1 ))
79
85
self .rect = self .image [1 ]
80
86
87
+ def update (self ) -> None :
88
+ """Updates the whole Bird element in the game.
89
+
90
+ Bird has two different immages to change between in order to create
91
+ a flying animation. The animation state changes every 40 frames.
92
+ """
93
+ if self .counter .bird_animation_status :
94
+ self .current_image = self .image [0 ][0 ]
95
+ else :
96
+ self .current_image = self .image [0 ][1 ]
97
+ super ().update ()
98
+
81
99
82
100
class Cloud (GameElement ):
83
101
"""This class represents a Cloud element in the game.
@@ -88,6 +106,7 @@ class Cloud(GameElement):
88
106
def __init__ (self ) -> None :
89
107
super ().__init__ (config .display_scale [0 ], rd .randint (50 , 100 ))
90
108
self .current_image , self .rect = load_image ("cloud.png" )
109
+ self .OBJECT_SPEED = 1
91
110
92
111
93
112
class Ground (GameElement ):
@@ -105,3 +124,29 @@ def __init__(self) -> None:
105
124
106
125
self .immage_1 , self .rect_1 = load_image ("ground.png" )
107
126
self .immage_2 , self .rect_2 = load_image ("ground.png" )
127
+
128
+ self .rect_1 .bottomleft = (0 , config .display_scale [1 ])
129
+ self .rect_2 .bottomleft = (self .rect_1 .right , config .display_scale [1 ])
130
+
131
+ def update (self ) -> None :
132
+ """Move the ground in the game.
133
+
134
+ The ground moves as every other element in the game.
135
+ Different from the other elements, the ground exists
136
+ at every time of the game.
137
+ That means two different ground images are needed wich are
138
+ swiched every time one reaches the end.
139
+ If the right end of on of them cross the window border,
140
+ the other image is atteched at it's right side.
141
+ """
142
+ if self .rect_1 .right <= 0 :
143
+ self .rect_1 .left = self .rect_2 .right
144
+
145
+ if self .rect_2 .right <= 0 :
146
+ self .rect_2 .left = self .rect_1 .right
147
+
148
+ self .rect_1 .move_ip (- self .OBJECT_SPEED , 0 )
149
+ self .rect_2 .move_ip (- self .OBJECT_SPEED , 0 )
150
+
151
+ config .window .blit (self .immage_1 , self .rect_1 )
152
+ config .window .blit (self .immage_2 , self .rect_2 )
0 commit comments