-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcart.py
More file actions
executable file
·105 lines (86 loc) · 4.52 KB
/
cart.py
File metadata and controls
executable file
·105 lines (86 loc) · 4.52 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
92
93
94
95
96
97
98
99
from collections import defaultdict
import pygame
import config
from enums.cart_state import CartState
from enums.direction import Direction
from helper import can_interact_default, overlap
from objects import CartLike
from render_game import render_text
class Cart(CartLike):
def __str__(self):
return "a shopping cart"
def class_string(self):
return "cart"
def __init__(self, x_position, y_position, owner, direction, capacity=12):
super(Cart, self).__init__(x_position, y_position, owner, capacity)
self.direction = direction
self.width = .01
self.height = .01
self.render_offset_x = 0
self.render_offset_y = 0
self.set_direction(direction)
def set_direction(self, direction):
self.direction = direction
if direction == Direction.NORTH or direction == Direction.SOUTH:
self.render_offset_x = -0.37
self.render_offset_y = -0.25
self.width = 0.5
self.height = 0.75
else:
self.render_offset_x = -0.20
self.render_offset_y = -0.47
self.width = 0.75
self.height = 0.4
def render(self, screen, camera):
image = None
if self.state == CartState.EMPTY or self.state == CartState.PURCHASED:
if self.direction == Direction.NORTH:
image = pygame.transform.scale(pygame.image.load("images/cart/shoppingcartEMPTYup.png"),
(config.SCALE, config.SCALE))
elif self.direction == Direction.SOUTH:
image = pygame.transform.scale(pygame.image.load("images/cart/shoppingcartEMPTYdown.png"),
(config.SCALE, config.SCALE))
elif self.direction == Direction.EAST:
image = pygame.transform.scale(pygame.image.load("images/cart/shoppingcartEMPTYright.png"),
(config.SCALE, config.SCALE))
elif self.direction == Direction.WEST:
image = pygame.transform.scale(pygame.image.load("images/cart/shoppingcartEMPTYleft.png"),
(config.SCALE, config.SCALE))
elif self.state == CartState.FULL:
if self.direction == Direction.NORTH:
image = pygame.transform.scale(pygame.image.load("images/cart/shoppingcartFULLup.png"),
(config.SCALE, config.SCALE))
elif self.direction == Direction.SOUTH:
image = pygame.transform.scale(pygame.image.load("images/cart/shoppingcartFULLdown.png"),
(config.SCALE, config.SCALE))
elif self.direction == Direction.EAST:
image = pygame.transform.scale(pygame.image.load("images/cart/shoppingcartFULLright.png"),
(config.SCALE, config.SCALE))
elif self.direction == Direction.WEST:
image = pygame.transform.scale(pygame.image.load("images/cart/shoppingcartFULLleft.png"),
(config.SCALE, config.SCALE))
rect = pygame.Rect(
(self.position[0] + self.render_offset_x) * config.SCALE - (camera.position[0] * config.SCALE),
(self.position[1] + self.render_offset_y) * config.SCALE - (camera.position[1] * config.SCALE),
config.SCALE, config.SCALE)
screen.blit(image, rect)
def can_interact(self, player):
return player.curr_cart != self and can_interact_default(self, player)
def update_position(self, x_position, y_position):
if self.direction == Direction.NORTH:
self.position[0] = x_position+0.05
self.position[1] = y_position-0.85
elif self.direction == Direction.SOUTH:
self.position[0] = x_position+0.05
self.position[1] = y_position+0.45
elif self.direction == Direction.EAST:
self.position[0] = x_position+0.65
self.position[1] = y_position
elif self.direction == Direction.WEST:
self.position[0] = x_position-0.8
self.position[1] = y_position
def collision(self, obj, x_position, y_position):
return overlap(self.position[0], self.position[1], self.width, self.height,
x_position, y_position, obj.width, obj.height)
def can_toggle(self, player):
return player.direction == self.direction and can_interact_default(self, player, 0.3)