-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathenemies.py
More file actions
116 lines (94 loc) · 4.19 KB
/
Copy pathenemies.py
File metadata and controls
116 lines (94 loc) · 4.19 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import pygame
import random
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 576
BLACK = (0,0,0)
WHITE = (255,255,255)
BLUE = (0,0,255)
GREEN = (0,255,0)
RED = (255,0,0)
class Block(pygame.sprite.Sprite):
def __init__(self,x,y,color,width,height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width,height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.topleft = (x,y)
class Ellipse(pygame.sprite.Sprite):
def __init__(self,x,y,color,width,height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width,height])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
pygame.draw.ellipse(self.image,color,[0,0,width,height])
self.rect = self.image.get_rect()
self.rect.topleft = (x,y)
class Slime(pygame.sprite.Sprite):
def __init__(self,x,y,change_x,change_y):
pygame.sprite.Sprite.__init__(self)
self.change_x = change_x
self.change_y = change_y
self.image = pygame.image.load("slime.png").convert_alpha()
self.rect = self.image.get_rect()
self.rect.topleft = (x,y)
def update(self,horizontal_blocks,vertical_blocks):
self.rect.x += self.change_x
self.rect.y += self.change_y
if self.rect.right < 0:
self.rect.left = SCREEN_WIDTH
elif self.rect.left > SCREEN_WIDTH:
self.rect.right = 0
if self.rect.bottom < 0:
self.rect.top = SCREEN_HEIGHT
elif self.rect.top > SCREEN_HEIGHT:
self.rect.bottom = 0
if self.rect.topleft in self.get_intersection_position():
direction = random.choice(("left","right","up","down"))
if direction == "left" and self.change_x == 0:
self.change_x = -2
self.change_y = 0
elif direction == "right" and self.change_x == 0:
self.change_x = 2
self.change_y = 0
elif direction == "up" and self.change_y == 0:
self.change_x = 0
self.change_y = -2
elif direction == "down" and self.change_y == 0:
self.change_x = 0
self.change_y = 2
def get_intersection_position(self):
items = []
for i,row in enumerate(enviroment()):
for j,item in enumerate(row):
if item == 3:
items.append((j*32,i*32))
return items
def enviroment():
grid = ((0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0),
(0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0),
(1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,3,1),
(0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0),
(0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0),
(0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0),
(1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,3,1),
(0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0),
(0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0),
(0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0),
(1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,3,1),
(0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0),
(0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0),
(0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0),
(1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,3,1),
(0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0),
(0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0),
(0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,0))
return grid
def draw_enviroment(screen):
for i,row in enumerate(enviroment()):
for j,item in enumerate(row):
if item == 1:
pygame.draw.line(screen, BLUE , [j*32, i*32], [j*32+32,i*32], 3)
pygame.draw.line(screen, BLUE , [j*32, i*32+32], [j*32+32,i*32+32], 3)
elif item == 2:
pygame.draw.line(screen, BLUE , [j*32, i*32], [j*32,i*32+32], 3)
pygame.draw.line(screen, BLUE , [j*32+32, i*32], [j*32+32,i*32+32], 3)