-
Notifications
You must be signed in to change notification settings - Fork 1
/
ground.py
59 lines (45 loc) · 1.81 KB
/
ground.py
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
"""Module defining the Ground class.
The Ground class represents the ground in the game. It handles the ground's
movement behavior.
Classes:
Ground: Represents the player character in the game.
"""
import pygame
class Ground(pygame.sprite.Sprite):
"""A class representing the ground in the game."""
def __init__(self, winwidth, winheight, surface, image_path):
"""Initialize the Ground object.
Args:
winwidth (int): The width of the game window.
winheight (int): The height of the game window.
surface (pygame.Surface): The surface on which the ground will be
drawn.
image_path (str): The file path to the image of the ground.
"""
super().__init__()
self.image = pygame.image.load(image_path)
self.rect = self.image.get_rect()
self.winwidth = winwidth
self.winheight = winheight
self._surface = surface
self.rect.y = self.winheight - self.rect.height
self.x_1 = 0
self.x_2 = self.rect.width
def update(self, speed):
"""Update the position of the ground.
Args:
speed (int): The speed at which the ground moves horizontally.
"""
self.x_1 -= speed
self.x_2 -= speed
if self.x_1 + self.rect.width < 0:
self.x_1 = self.x_2 + self.rect.width
if self.x_2 + self.rect.width < 0:
self.x_2 = self.x_1 + self.rect.width
def draw_ground(self):
"""Draw the ground on the surface."""
self._surface.blit(self.image, (self.x_1, self.rect.y))
self._surface.blit(self.image, (self.x_2, self.rect.y))
def get_rect(self):
"""Return the rectangular area occupied by the ground."""
return pygame.Rect(0, self.rect.y, self.winwidth, 0.1 * self.winheight)