-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanoop.py
More file actions
executable file
·149 lines (149 loc) · 4.87 KB
/
anoop.py
File metadata and controls
executable file
·149 lines (149 loc) · 4.87 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
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
# Import a library of functions called 'pygame'
import pygame
import random
# Initialize the game engine
pygame.init()
black = [ 0, 0, 0]
#Colors used in this game
white = [255,255,255]
green=[0,255,0]
#set the music
music=pygame.mixer.Sound("song.wav")
#music.play()
#Specify the comments display on the screen after the game
basicfont=pygame.font.Font('freesansbold.ttf',32)
gameover=basicfont.render("GAME OVER",1,green)
gameoverrect=gameover.get_rect()
#Display the result on the screen
scoreboard=pygame.font.Font(None,32)
# Set the height and width of the screen
screen = pygame.display.set_mode([600, 500])
pygame.display.set_caption("Point collection")
#coordinates
x_coord=0
y_coord=250
clock = pygame.time.Clock()
#create lists
atkr=[]
point=[]
atkb=[]
# Loop 10 times and add a images in a random x,y position
for i in range(10):
x=random.randrange(200,600)
y=random.randrange(0,500)
atkr.append([x,y])
for i in range(5):
x=random.randrange(0,600)
y=random.randrange(0,500)
point.append([x,y])
for i in range(10):
x=random.randrange(0,600)
y=random.randrange(0,500)
atkb.append([x,y])
#Loop until the user clicks the close button.
done=False
move_x=0
move_y=0
score=0
while done==False:
music.play()
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
# Ketboard used to control the player in left,right,top and bottom
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move_x=-3
if event.key == pygame.K_RIGHT:
move_x= 3
if event.key==pygame.K_UP:
move_y=-3
if event.key==pygame.K_DOWN:
move_y=3
if event.type==pygame.KEYUP:
if event.key==pygame.K_LEFT:
move_x=0
if event.key==pygame.K_RIGHT:
move_x=0
if event.key==pygame.K_UP:
move_y=0
if event.key==pygame.K_DOWN:
move_y=0
#Move the object according to the speed vector.
x_coord=x_coord+move_x
y_coord=y_coord+move_y
screen.fill(black)
# Create player
player=pygame.image.load("man.jpg").convert()
screen.blit(player,[x_coord,y_coord])
rect1=pygame.Rect(x_coord,y_coord,25,35)
player.set_colorkey(black)
# Process each atkr in the list
for i in range(len(atkr)):
atkr_img=pygame.image.load("im.gif").convert()
screen.blit(atkr_img,atkr[i])
# Move the atkr left three pixel
atkr[i][0]-=3
if rect1.collidepoint(atkr[i][0],atkr[i][1]):
score=score-1
y=random.randrange(0,500)
atkr[i][1]=y
x=random.randrange(590,600)
atkr[i][0]=x
# If the atkr has moved off the left of the screen
if atkr[i][0] <0:
# Reset it just right of the screen
y=random.randrange(0,500)
atkr[i][1]=y
x=random.randrange(590,600)
atkr[i][0]=x
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Process each point list elements
for i in range(len(point)):
point_img=pygame.image.load("b2-17.png").convert()
screen.blit(point_img,point[i])
# Move the point image down three pixel
point[i][1]+=3
if rect1.collidepoint(point[i][0],point[i][1]):
score=score+1
y=random.randrange(-500,-10)
point[i][1]=y
x=random.randrange(0,600)
point[i][0]=x
# If the image has moved off the bottom of the screen
if point[i][1] > 500:
# Reset it just above the top
y=random.randrange(-500,-10)
point[i][1]=y
# Give it a new x position
x=random.randrange(0,600)
point[i][0]=x
for i in range(len(atkb)):
atkb_img=pygame.image.load("alien.png").convert()
screen.blit(atkb_img,atkb[i])
atkb_img.set_colorkey(white)
# Move the image top three pixel
atkb[i][1]-=3
if rect1.collidepoint(atkb[i][0],atkb[i][1]):
score=score-1
y=random.randrange(490,500)
atkb[i][1]=y
x=random.randrange(0,600)
atkb[i][0]=x
# If the imaged has moved off the top of the screen
if atkb[i][1] <0:
# Reset it to the new position
y=random.randrange(490,500)
atkb[i][1]=y
x=random.randrange(0,600)
atkb[i][0]=x
screen.blit(scoreboard.render("score"+str(score),4,white),[40,40])
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
clock.tick(20)
if score<-2:
screen.blit(gameover,gameoverrect)
pygame.display.flip()
done=True
pygame.quit ()