-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
161 lines (110 loc) · 3.65 KB
/
game.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
import pygame, os
from pygame.locals import *
from sys import exit
import time
pygame.init()
pygame.font.init()
font = pygame.font.SysFont("None",48)
class Snake(pygame.sprite.Sprite):
def __init__(self,init_x,init_y):
pygame.sprite.Sprite.__init__(self)
self.x = init_x
self.y = init_y
self.image = pygame.image.load('pics/SnakeN.png')
self.rect = self.image.get_rect()
self.rect.move_ip(self.x,self.y)
self.alive = True
self.timeOfDeath = 0
self.timeUntilBackAlive = 0
def update(self,x,y):
self.rect.move_ip(x,y)
self.x += x
self.y += y
def blit(self):
if self.alive == True:
screen.blit(self.image,(self.x,self.y))
class Picogirl(pygame.sprite.Sprite):
def __init__(self,init_x,init_y):
pygame.sprite.Sprite.__init__(self)
self.x = init_x
self.y = init_y
self.image = pygame.image.load('pics/Picowhip.png')
self.rect = self.image.get_rect()
self.rect.move_ip(self.x,self.y)
def update(self,x,y):
self.rect.move_ip(x,y)
self.x += x
self.y += y
def blit(self):
screen.blit(self.image,(self.x,self.y))
#tests for collision between two objects
def testForCollision( ob1, ob2 ):
if ob1.rect.colliderect( ob2.rect ) == True:
return True
else:
return False
mysnake = Snake(400,520)
picogirl = Picogirl(100,550)
timeUntilBackAlive = 0
bg_image_filename = 'pics/background.png'
screen = pygame.display.set_mode((948,748),0,32)
bg = pygame.image.load(bg_image_filename).convert()
pygame.display.flip()
#music
intro = pygame.mixer.Sound('sounds/fanfare10.ogg')
punch = pygame.mixer.Sound('sounds/hit_p07.ogg')
appear = pygame.mixer.Sound('sounds/blah.ogg')
#counter to save movement coordinates for Picogirl
move_x = 0
#counter to save movement coordinates for the snake
move_x1 = 0
text = "Help PicoGirl defeat Python the Snake"
imageOfText = font.render(text,False,(0,0,0))
screen.blit(bg,(0,0))
screen.blit(imageOfText,(200,50))
pygame.display.update()
time.sleep(2)
pygame.mixer.Sound.play(intro)
#starting game loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
#movement for PicoGirl
if event.type == KEYDOWN:
if event.key == K_LEFT:
move_x = -1
elif event.key == K_RIGHT:
move_x = +1
pygame.mixer.Sound.play(punch)
elif event.type == KEYUP:
if event.key == K_LEFT:
move_x = 0
elif event.key == K_RIGHT:
move_x = 0
#movement for Python the snake
if event.type == KEYDOWN:
if event.key == K_a:
move_x1 = -1
elif event.key == K_s:
move_x1 = +1
elif event.type == KEYUP:
if event.key == K_a:
move_x1 = 0
elif event.key == K_s:
move_x1 = 0
mysnake.update(move_x1,0)
picogirl.update(move_x,0)
if testForCollision( picogirl, mysnake ) == True:
mysnake.alive = False
timeUntilBackAlive = time.time() + 1.0
mysnake.update(200,10)
pygame.mixer.Sound.play(appear)
currentTime = time.time()
if currentTime > timeUntilBackAlive:
mysnake.alive = True
#drawing the images to the screen
screen.blit(bg, (0,0))
mysnake.blit()
picogirl.blit()
pygame.display.update()