-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-Copy.py
164 lines (110 loc) · 4.22 KB
/
game-Copy.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import sys, pygame
pygame.init()
blocks=64
sw=16*blocks
sh=8*blocks
up=0
down=sh
left=0
right=sw
screen = pygame.display.set_mode((sw,sh))
#title
pygame.display.set_caption('Clockworks game')
#fps
fps = 60
#gameobj class
class objec(pygame.sprite.Sprite):
#init (poz, dimens :width, height)
def __init__(self, xi=50, yi=50, w=blocks, h=blocks, movable = False, title = 'blackball.png', group = None):
super (objec, self).__init__()
self.image = pygame.image.load(title)
pygame.transform.scale(self.image, (w,h))
self.rect = self.image.get_rect()
self.x=xi
self.y=yi
self.w=w
self.h=h
self.movable = movable
self.oldx=self.x
self.oldy=self.y
self.rect=pygame.Rect(self.x,self.y,self.w,self.h)
if group != None:
group.add(self)
def move (self,event = None):
if(self.movable ==True):
if event.type == pygame.KEYDOWN:
self.oldx=self.x
self.oldy=self.y
if event.key==pygame.K_DOWN and self.y+self.h<down:
pygame.draw.rect(screen, (0,0,0), [self.x,self.y,self.w,self.h])
self.y+=self.h
if event.key==pygame.K_UP and self.y>0:
pygame.draw.rect(screen, (0,0,0), [self.x,self.y,self.w,self.h])
self.y-=self.h
if event.key==pygame.K_LEFT and self.x>0:
pygame.draw.rect(screen, (0,0,0), [self.x,self.y,self.w,self.h])
self.x-=self.w
if event.key==pygame.K_RIGHT and self.x+self.w<right:
pygame.draw.rect(screen, (0,0,0), [self.x,self.y,self.w,self.h])
self.x+=self.w
self.rect=pygame.Rect(self.x,self.y,self.w,self.h)
def collide (self, spriteGroup):
if pygame.sprite.spritecollide(self,spriteGroup, False):
self.x=self.oldx
self.y=self.oldy
self.rect=pygame.Rect(self.x,self.y,self.w,self.h)
clock=pygame.time.Clock()
clock.tick(fps)
#group
objec_group = pygame.sprite.Group()
endgroup=pygame.sprite.Group()
b=objec(2*blocks,2*blocks,blocks,blocks,True,'blackball.png', objec_group)
# b.__init__(2*blocks,2*blocks,blocks,blocks,True,'blackball.png')
s=objec(0*blocks,0*blocks,blocks,blocks,True,'shinyball.png', objec_group)
# s.__init__(0*blocks,0*blocks,blocks,blocks,True,'shinyball.png')
# blackball end
be=objec(1*blocks,1*blocks,blocks,blocks,False,'blackball-ep.png', endgroup)
# be.__init__(1*blocks,1*blocks,blocks,blocks,False,'blackball-ep.png')
#shinyball end
se=objec(0*blocks,2*blocks,blocks,blocks,False,'shinyball-ep.png', endgroup)
# se.__init__(0*blocks,2*blocks,blocks,blocks,False,'shinyball-ep.png')
obs1=objec(0*blocks,1*blocks,blocks,blocks,False,'obs.png', objec_group)
# obs1.__init__(0*blocks,1*blocks,blocks,blocks,False,'obs.png')
obs2=objec(1*blocks,2*blocks,blocks,blocks,False,'obs.png', objec_group)
# obs2.__init__(1*blocks,2*blocks,blocks,blocks,False,'obs.png')
obs3=objec(3*blocks,2*blocks,blocks,blocks,False,'obs.png', objec_group)
# obs3.__init__(3*blocks,2*blocks,blocks,blocks,False,'obs.png')
obs4=objec(5*blocks,2*blocks,blocks,blocks,False,'obs.png', objec_group)
win=objec(0,0,sw,sh, False, 'win.png')
# win.__init__(0,0,sw,sh, False, 'win.png')
# objec_group.add(b,s,obs1,obs2)
# objec_group.add(obs3)
# objec_group.add(obs4)
# endgroup.add(se,be)
endgroup.draw(screen)
objec_group.draw(screen)
wingroup=pygame.sprite.Group()
gameexit = False
while not gameexit:
pygame.display.update()
endgroup.draw(screen)
objec_group.draw(screen)
wingroup.draw(screen)
#movement
for event in pygame.event.get():
if ( event.type == pygame.KEYDOWN):
b.move(event)
s.move(event)
for o in objec_group:
objec_group.remove(o)
o.collide(objec_group)
objec_group.add(o)
if (event.key == pygame.K_ESCAPE):
gameexit = True
if(b.x==be.x) and (b.y==be.y) and (s.x==se.x) and (s.y==se.y):
wingroup.add(win)
pygame.display.update()
pygame.quit()
sys.exit()
quit()
#poveste ->