forked from thaleaschlender/GAME_AI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoin.py
More file actions
92 lines (76 loc) · 4.03 KB
/
Coin.py
File metadata and controls
92 lines (76 loc) · 4.03 KB
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
import random
import pygame
# Color of the coin (YELLOW)
COIN_COLOR = (255,255,0)
class VirtualCoin(pygame.sprite.Sprite):
"""Virtual parent class of coin that has all the same variables and functions
except anything required with drawing to screen."""
def __init__(self, DISPLAYSURF, speed, width, height, center):
super().__init__()
self.DISPLAYSURF = DISPLAYSURF #defines as a surface object
self.SCREEN_WIDTH, self.SCREEN_HEIGHT = self.DISPLAYSURF.get_size()
self.speed = speed
self.visible = True
self.surf = pygame.Surface((width, height))
self.rect = self.surf.get_rect(center=center)
#updates object exactly one tick
def update(self):
self.rect.move_ip(0, self.speed)
if (self.rect.top > self.SCREEN_HEIGHT):
self.resetToTopOfScreen()
# Check if coin overlaps with the obstacles
def check_obstacle_overlap(self):
# Check for collision with small cube obstacle
if (self.rectangleObstacle.rect.colliderect(self.rect)):
# Check if there is room on the left of obstacle
if (self.rectangleObstacle.rect.left - 100 < 40):
# Put the coin to the right
self.rect.left = self.rectangleObstacle.rect.right + 40
# Check if there is room on the right of obstacle
elif (self.rectangleObstacle.rect.right + 100 > self.SCREEN_WIDTH - 40):
# Put the coin to the left
self.rect.right = self.rectangleObstacle.rect.left - 100
# There is room on both sides of the obstacle, decide randomly on which side to put
elif (random.random() < 0.5):
self.rect.left = self.rectangleObstacle.rect.right + 100
else:
self.rect.right = self.rectangleObstacle.rect.left - 100
# Check overlap with the vertical obstacle
if (self.verticalObstacle.rect.top - 100 < self.rect.bottom):
# Move coin above the obstacle
self.rect.bottom = self.verticalObstacle.rect.top - 100
def resetToTopOfScreen(self):
self.rect.center = (random.randint(40, self.SCREEN_WIDTH - 40), 0)
self.check_obstacle_overlap()
self.visible = True
# Set the obstacles of the game, needed for avoiding overlaps between the coins and obstacles.
def setObstacles(self, verticalObstacle, rectangleObstacle):
self.verticalObstacle = verticalObstacle
self.rectangleObstacle = rectangleObstacle
# Set if coin is visible. Related to avoiding overlaps with obstacles. This way the coin is always
# reset to top only when reaching the bottom of the screen. And it avoids cases where coin is put to the
# top of the screen, but obstacle is put on top of or near the coin. Preferred it over adding Coin dependency to
# the obstacle class.
def setVisible(self, isVisible):
self.visible = isVisible
#return virtualcopy of self for gamestate
def get_virtual_copy(self):
new_coin = VirtualCoin(self.DISPLAYSURF, self.speed,
self.surf.get_width(), self.surf.get_height(), self.rect.center)
new_coin.setVisible(self.visible)
return new_coin
class Coin(VirtualCoin):
def __init__(self, DISPLAYSURF, speed, width, height, height_position): #initalisitation of an obstacle object with its width, height and height_position as parameters
super().__init__(DISPLAYSURF, speed, width, height, (0, height_position))
#Add appearence
self.image = pygame.Surface([width, height]) #defines the surface of the coin elements
self.image.fill(COIN_COLOR) #fills the coin elements with the coin color
# determines the position
width_position = random.randint(40, self.SCREEN_WIDTH - 40)
self.rect.centerx = width_position
def update(self):
if self.visible:
self.draw() #add the element to the screen
super().update()
def draw(self):
self.DISPLAYSURF.blit(self.image, self.rect)