-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.py
More file actions
executable file
·41 lines (37 loc) · 1.45 KB
/
GameObject.py
File metadata and controls
executable file
·41 lines (37 loc) · 1.45 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
'''
GameObject.py
implements the base GameObject class, which defines the wraparound motion
Lukas Peraza, 2015 for 15-112 Pygame Lecture
'''
import pygame
class GameObject(pygame.sprite.Sprite):
def __init__(self, x, y, image, radius):
super(GameObject, self).__init__()
# x, y define the center of the object
self.x, self.y, self.image, self.radius = x, y, image, radius
self.baseImage = image.copy() # non-rotated version of image
w, h = image.get_size()
self.updateRect()
self.downSpeed = 2
self.velocity = (0, 0)
def updateRect(self):
# update the object's rect attribute with the new x,y coordinates
w, h = self.image.get_size()
self.width, self.height = w, h
self.rect = pygame.Rect(self.x - w / 2, self.y - h / 2, w, h)
def update(self,screenWidth,screenHeight):
# self.image = pygame.transform.rotate(self.baseImage, self.angle)
vx, vy = self.velocity
self.x += vx
self.y += vy
self.updateRect()
# wrap around, and update the rectangle again
#if self.rect.left > screenWidth:
# self.x -= screenWidth + self.width
#elif self.rect.right < 0:
# self.x += screenWidth + self.width
#if self.rect.top > screenHeight:
# self.y -= screenHeight + self.height
#elif self.rect.bottom < 0:
# self.y += screenHeight + self.height
#self.updateRect()