-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI_Bat_Projection.py
73 lines (62 loc) · 2.16 KB
/
GUI_Bat_Projection.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# ------------------------------------------
# DESCRIPTION OF MODULE
# ------------------------------------------
"""
CamPong Bat_Projection class
Represents the projection of the object that user is carrying
in front of the camera to move the bat
It is represented by a rectangle
"""
# ------------------------------------------
# IMPORTS
# ------------------------------------------
try:
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
import pygame.locals
from Constants import *
except ImportError as err:
print ("Error: couldn't load module" + str(err) + ". Exiting...")
exit()
# ------------------------------------------
# CONSTANTS
# ------------------------------------------
# Init state
HEIGHT_DEFAULT = 30
WIDTH_DEFAULT = 30
COLOUR_DEFAULT = COLOUR_WHITE
# ------------------------------------------
# CLASSES DEFINITIONS
# ------------------------------------------
class Bat_Projection(pygame.sprite.Sprite):
"""
Attributes:
- image:Surface
- rect:Rect. Determines position
- area:Rect. Screen the object is moving across
- pos_init:(Int,Int). (X,Y)
- sprite:Sprite
Methods: __init__, update
"""
def __init__(self, width=WIDTH_DEFAULT, height=HEIGHT_DEFAULT, colour=COLOUR_DEFAULT):
# sprite's constructor
pygame.sprite.Sprite.__init__(self)
# image (appearance) and rect (hitbox)
self.image = pygame.Surface((width, height))
self.rect = self.image.get_rect()
pygame.draw.rect(self.image, colour, self.rect)
# area that contains the object (i.e. the board itself)
screen = pygame.display.get_surface()
self.area = screen.get_rect()
# initial position
self.pos_init = self.area.center
self.rect.center = self.pos_init
# sprite
self.sprite = pygame.sprite.RenderPlain(self)
def update(self, screen, background, x, y):
# Move the object to a new position (x,y)
if (screen != None and background != None):
self.rect.center = (x,y)
screen.blit(background, self.rect, self.rect)
self.sprite.draw(screen)