-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube.py
More file actions
42 lines (33 loc) · 1.24 KB
/
Copy pathcube.py
File metadata and controls
42 lines (33 loc) · 1.24 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
import pygame
class Cube(object):
rows = 40
w = 1000
def __init__(self, start, dirnx = 1, dirny = 0, color = (255,0,0)):
# current position of cube
self.pos = start
# horizontal direction of movement of cube
self.dirnx = 1
# vertical direction of movement of cube
self.dirny = 0
# color of cube
self.color = color
def draw(self, surface, head = False):
dist = self.w // self.rows
i = self.pos[0]
j = self.pos[1]
pygame.draw.rect(surface, self.color, (i*dist+1, j*dist+1, dist-2, dist-2))
if head:
self.color = (255,255,255)
def move(self, dirnx, dirny):
self.dirnx = dirnx
self.dirny = dirny
# updating the position of the cube
if self.dirnx == -1 and self.pos[0] <= 0:
self.pos = (self.rows-1, self.pos[1])
elif self.dirnx == 1 and self.pos[0] >= self.rows-1:
self.pos = (0,self.pos[1])
elif self.dirny == 1 and self.pos[1] >= self.rows-1:
self.pos = (self.pos[0], 0)
elif self.dirny == -1 and self.pos[1] <= 0:
self.pos = (self.pos[0],self.rows-1)
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)