-
Notifications
You must be signed in to change notification settings - Fork 0
/
mygame.py
214 lines (163 loc) · 5.53 KB
/
mygame.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import pygame
import random
import mysql.connector
from prettytable import PrettyTable
BLACK = ( 255, 51, 51)
WHITE = (0, 204, 204)
RED = (51, 255, 51)
GREEN = (0,0,0)
BLUE = (15,15,200)
print()
name=input('Enter name ')
print()
class Block(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
pygame.init()
size=(900,600)
screen = pygame.display.set_mode((size), pygame.RESIZABLE)
block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
l1=[]
l2=[]
for i in range(1):
block = Block(BLACK, 50, 50)
block.rect.x = random.randrange(200,830)
block.rect.y = random.randrange(200,530)
block_list.add(block)
all_sprites_list.add(block)
for k in range(15):
xchange=random.randrange(0,5)
l1.append(xchange)
ychange=random.randrange(0,5)
l2.append(ychange)
player = Block(RED, 20, 20)
player.rect.left=11
player.rect.top=11
all_sprites_list.add(player)
game_loop = True
clock = pygame.time.Clock()
wall_touch_count=0
score = 100
level=1
while game_loop == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_loop = False
screen.fill(WHITE)
i=0
pygame.draw.line(screen,GREEN,[0,0],[0,600],20)
pygame.draw.line(screen,GREEN,[0,0],[900,0],20)
pygame.draw.line(screen,GREEN,[0,600],[900,600],20)
pygame.draw.line(screen,GREEN,[900,0],[900,500],20)
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
all_sprites_list.draw(screen)
pygame.display.flip()
for block in block_list:
block.rect.x += l1[i]
block.rect.y += l2[i]
i=i+1
if block.rect.x>=840 or block.rect.x<10:
l1[i-1] = l1[i-1]*(-1)
if block.rect.y>=540 or block.rect.y<10:
l2[i-1] = l2[i-1]*(-1)
if player.rect.left<11:
wall_touch_count+=1
print(wall_touch_count)
player.rect.left=11
if player.rect.right>890 and player.rect.top<=500:
wall_touch_count+=1
print(wall_touch_count)
player.rect.right=890
if player.rect.top<11:
wall_touch_count+=1
print(wall_touch_count)
player.rect.top=11
if player.rect.bottom>590:
wall_touch_count+=1
print(wall_touch_count)
player.rect.bottom=590
if player.rect.left>=900:
score= score+1000
level=level+1
if level<7:
font = pygame.font.SysFont('comicsansms', 85, True, False)
text = font.render("NEXT LEVEL", True, BLUE)
screen.blit(text, [180, 200])
pygame.display.flip()
pygame.time.wait(3000)
if level ==7:
font = pygame.font.SysFont('comicsansms', 45, True, False)
text = font.render("YOU COMPLETED THE GAME", True, BLUE)
screen.blit(text, [180, 200])
pygame.display.flip()
pygame.time.wait(3000)
FINAL_SCORE=(score-wall_touch_count)
print('YOUR FINAL SCORE IS ',FINAL_SCORE)
game_loop = False
for j in range(level):
block = Block(BLACK, 50, 50)
block.rect.x = random.randrange(100,830)
block.rect.y = random.randrange(100,530)
block_list.add(block)
all_sprites_list.add(block)
player.rect.left=11
player.rect.top=11
if event.type==pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.rect.x -= 3
if event.key == pygame.K_RIGHT:
player.rect.x += 3
if event.key == pygame.K_UP:
player.rect.y -= 3
if event.key == pygame.K_DOWN:
player.rect.y += 3
for block in blocks_hit_list:
font = pygame.font.SysFont('comicsansms', 85, True, False)
text = font.render("YOU CRASHED", True, BLUE)
screen.blit(text, [150, 200])
pygame.display.flip()
pygame.time.wait(3000)
FINAL_SCORE=(score-wall_touch_count)
print()
print('GAME OVER\nYOUR FINAL SCORE IS ',FINAL_SCORE)
print()
game_loop = False
clock.tick(60)
pygame.quit()
def insertrecord_mysql():
mydb=mysql.connector.connect(host='localhost', user='username', passwd='password', database='pygame')
mycursor=mydb.cursor()
mycursor.execute("SELECT MAX(SNO) FROM SCORES")
myrecords=mycursor.fetchall()
for x in myrecords:
if(x==None):
new_SNo =1
else:
new_SNo = x[0]+1
myquery="INSERT INTO SCORES VALUES(%s, %s, %s, %s)"
mydata=(new_SNo, name, level, FINAL_SCORE)
mycursor.execute(myquery,mydata)
mydb.commit()
insertrecord_mysql()
def leaderboard():
check=input('Enter 0 to see leaderboard ')
print()
if check=='0':
mydb=mysql.connector.connect(host='localhost', user='username', passwd='password', database='pygame')
mycursor=mydb.cursor()
mycursor.execute("SHOW COLUMNS FROM SCORES")
mycolumns=mycursor.fetchall()
myTable = PrettyTable(["SNo", "Name", "Level", "Score"])
#for x in mycolumns:
# print(x[0],end=' ')
#print()
mycursor.execute("SELECT * FROM SCORES ORDER BY SCORE DESC")
myrecords=mycursor.fetchall()
for y in myrecords:
myTable.add_row(y)
print(myTable)
leaderboard()