Advertisement
ALENTL

Untitled

Oct 20th, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.23 KB | None | 0 0
  1. # importing libraries
  2. import pygame
  3. import time
  4. import random
  5. from tkinter import *
  6. import tkinter.messagebox
  7. import mysql.connector as mc
  8.  
  9. cn=mc.connect(host="localhost",user="alen",password="alen")
  10. cur=cn.cursor()
  11. cur.execute("create database if not exists snake_game_dat")
  12. cur.execute("use snake_game_dat")
  13. cn.autocommit
  14.  
  15. cur.execute("create table if not exists snake_game_score(username varchar(40), password int(5), score int(100))")
  16. snake_speed = 15
  17. es1val=15
  18.  
  19. def menu_screen():
  20.  
  21. global em1,em2,log
  22. log = Tk()
  23. label = Label(log, text="Snake Game", font='georgia 100 bold')
  24. label.pack()
  25. frame = Frame(log, height=480, width=720)
  26. frame.pack()
  27. playin = Button(log, text='Play',font='georgia 10 bold',height=2,width=30, command=play)
  28. playin.place(x=200,y=200)
  29. score1 = Button(log, text='High Scores',font='georgia 10 bold',height=2,width=30, command=score)
  30. score1.place(x=200,y=250)
  31. settin = Button(log, text='Settings',font='georgia 10 bold',height=2,width=30, command=settings)
  32. settin.place(x=200,y=300)
  33. quitpg = Button(log, text="Quit", font='georgia 10 bold',height=2,width=30, command=quitpage)
  34. quitpg.place(x=200,y=350)
  35. setin12=False
  36.  
  37. log.resizable(False,False)
  38. log.mainloop()
  39.  
  40. def score():
  41. log.destroy()
  42. global esc1,esc2,sctk
  43. sctk= Tk()
  44. '''cur.execute("select user,score from snake_game_score desc")
  45. data=cur.fetchall()
  46. for ktk in data:
  47. for j in ktk:
  48. l1 = Label(setin, text='Speed')
  49. l1.place(x=50,y=300)'''
  50.  
  51.  
  52. def display_text():
  53. global es1val
  54. string = es1.get()
  55. print(string)
  56. if string=='':
  57. string=15
  58. es1val = int(string)
  59.  
  60. def menu_screen1():
  61. setin.destroy()
  62. menu_screen()
  63. def settings():
  64. log.destroy()
  65. global es1,es2,setin,l1
  66. setin = Tk()
  67. label = Label(setin, text='Settings', font='georgia 100 bold')
  68. label.pack()
  69. frame = Frame(setin, height=480, width=720)
  70. frame.pack()
  71. l1 = Label(setin, text='Speed')
  72. l1.place(x=50,y=300)
  73. es1 = tkinter.Entry(setin)
  74. es1.focus_set()
  75. es1.pack()
  76. es1.place(x=140,y=300)
  77. butt2 = Button(setin, text= "Okay",width= 20, command= display_text)
  78. butt2.place(x=200,y=300)
  79.  
  80. butt1 = Button(setin,text='Main menu',font='georgia 10 bold',height=2,width=30, command=menu_screen1)
  81. butt1.place(x=200,y=200)
  82.  
  83. setin.resizable(False,False)
  84. setin.mainloop()
  85. def login_get():
  86. uname = e1.get()
  87. pwod = int(e2.get())
  88. cur.execute("select username,password from snake_game_score")
  89. R=cur.fetchall()
  90. if (uname,pwod) in R:
  91. log1.destroy()
  92. play()
  93. else:
  94. global logg
  95. logg = Tk()
  96. label = Label(setin, text='Error', font='georgia 20 bold')
  97. label.pack()
  98. frame = Frame(setin, height=180, width=120)
  99.  
  100. l3 = Label(logg, text='Wrong username or password')
  101. l3.place(x=140,y=180)
  102. loggbu = Button(logg, text='Back to login',font='georgia 10 bold',height=2,width=30,command=destlogg)
  103. loggbu.place(x=200,y=200)
  104.  
  105. logg.resizabale(False,False)
  106. logg.mainloop()
  107.  
  108. def destlogg():
  109. logg.destroy()
  110.  
  111.  
  112. def login():
  113. log.destroy()
  114. global e1,e2,log1
  115. log = Tk()
  116. label = Label(log1, text='Login', font='georgia 100 bold')
  117. label.pack()
  118. frame = Frame(log1, height=480, width=720)
  119. frame.pack()
  120. l1 = Label(log1, text='Username')
  121. l1.place(x=50,y=100)
  122. e1 = tkinter.Entry(log1)
  123. e1.place(x=140,y=100)
  124.  
  125. l2 = Label(log1, text='Password')
  126. l2.place(x=50,y=150)
  127. e2 = tkinter.Entry(log1)
  128. e2.place(x=140,y=150)
  129. butt = Button(log1, text='Login',command=login_get)
  130. butt.place(x=150,y=200)
  131. sg = Label(log1, text="Don't have an account?")
  132. sg.place(x=100,y=240)
  133. sigup = Button(log1, text='Sign Up',font='georgia 10 bold',height=2,width=30, command=signup_page)
  134. sigup.place(x=200,y=200)
  135.  
  136. log.resizable(False,False)
  137. log.mainloop()
  138.  
  139. def quitpage():
  140. log.destroy()
  141. quit()
  142.  
  143. def signup_page():
  144. global et1,et2,log1
  145. log.destroy()
  146. signup = Tk()
  147. label = Label(signup, text='Sign Up', font='georgia 30 bold')
  148. label.pack()
  149. frame = Frame(signup, height=300, width=400)
  150. frame.pack()
  151. l1 = Label(signup, text='Username')
  152. l1.place(x=50,y=100)
  153. et1 = tkinter.Entry(signup)
  154. et1.place(x=140,y=100)
  155. l2 = Label(signup, text='Password')
  156. l2.place(x=50,y=150)
  157. et2 = tkinter.Entry(signup)
  158. et2.place(x=140,y=150)
  159. butt = Button(signup, text='Sign Up')
  160. butt.place(x=150,y=200)
  161.  
  162. signup.resizable(False,False)
  163. signup.mainloop()
  164.  
  165.  
  166. def save_score():
  167. #cur.execute("insert into snake_game_score values(uname,pwod,score))")
  168. cur.execute("INSERT INTO snake_game_score values (%s,%s,%s)", uname, pwod, score)
  169.  
  170. # Main Function
  171. def play():
  172. log.destroy()
  173. snake_speed = es1val
  174. # Window size
  175. window_x = 720
  176. window_y = 480
  177.  
  178. # defining colors
  179. black = pygame.Color(0, 0, 0)
  180. white = pygame.Color(255, 255, 255)
  181. red = pygame.Color(255, 0, 0)
  182. green = pygame.Color(0, 0, 255)
  183. blue = pygame.Color(0, 255, 0)
  184.  
  185. # Initialising pygame
  186. pygame.init()
  187.  
  188. # Initialise game window
  189. pygame.display.set_caption('Snakes Game')
  190. game_window = pygame.display.set_mode((window_x, window_y))
  191.  
  192. # FPS (frames per second) controller
  193. fps = pygame.time.Clock()
  194.  
  195. # defining snake default position
  196. snake_position = [100, 50]
  197.  
  198. # defining first 4 blocks of snake
  199. # body
  200. snake_body = [ [100, 50],[90, 50],[80, 50],[70, 50]]
  201. # fruit position
  202. fruit_position = [random.randrange(1, (window_x//10)) * 10,
  203. random.randrange(1, (window_y//10)) * 10]
  204. fruit_spawn = True
  205.  
  206. # setting default snake direction
  207. # towards right
  208. direction = 'RIGHT'
  209. change_to = direction
  210.  
  211. # initial score
  212. score = 0
  213. # game over function
  214. def game_over():
  215.  
  216. save_score()
  217. # creating font object my_font
  218. my_font = pygame.font.SysFont('georgia', 20)
  219. my_font1=pygame.font.SysFont('calibri',50)
  220. # creating a text surface on which text
  221. # will be drawn
  222. game_over_surface1 = my_font1.render('Game Over',True,red)
  223.  
  224. game_over_surface = my_font.render('Your Score is : ' + str(score), True, red)
  225.  
  226. # create a rectangular object for the text
  227. # surface object
  228. game_over_rect = game_over_surface.get_rect()
  229. game_over_rect1=game_over_surface.get_rect()
  230. # setting position of the text
  231. game_over_rect1.midtop=(window_x/2.3,window_y/5)
  232. game_over_rect.midtop = (window_x/2, window_y/3)
  233.  
  234. # blit will draw the text on screen
  235. game_window.blit(game_over_surface1,game_over_rect1)
  236. game_window.blit(game_over_surface, game_over_rect)
  237. pygame.display.flip()
  238.  
  239. # after 2 seconds we will quit the
  240. # program
  241. time.sleep(1)
  242. pygame.quit()
  243. menu_screen()
  244. # displaying Score function
  245. def show_score(choice, color, font, size):
  246.  
  247. # creating font object score_font
  248. score_font = pygame.font.SysFont(font, size)
  249.  
  250. # create the display surface object
  251. # score_surface
  252. score_surface = score_font.render('Score : ' + str(score), True, color)
  253.  
  254. # create a rectangular object for the
  255. # text surface object
  256. score_rect = score_surface.get_rect()
  257.  
  258. # displaying text
  259. game_window.blit(score_surface, score_rect)
  260. while True:
  261.  
  262.  
  263.  
  264. # handling key events
  265. for event in pygame.event.get():
  266. if event.type == pygame.KEYDOWN:
  267. if event.key == pygame.K_UP:
  268. change_to = 'UP'
  269. if event.key == pygame.K_DOWN:
  270. change_to = 'DOWN'
  271. if event.key == pygame.K_LEFT:
  272. change_to = 'LEFT'
  273. if event.key == pygame.K_RIGHT:
  274. change_to = 'RIGHT'
  275.  
  276. # If two keys pressed simultaneously
  277. # we don't want snake to move into two directions
  278. # simultaneously
  279. if change_to == 'UP' and direction != 'DOWN':
  280. direction = 'UP'
  281. if change_to == 'DOWN' and direction != 'UP':
  282. direction = 'DOWN'
  283. if change_to == 'LEFT' and direction != 'RIGHT':
  284. direction = 'LEFT'
  285. if change_to == 'RIGHT' and direction != 'LEFT':
  286. direction = 'RIGHT'
  287.  
  288.  
  289. # Moving the snake
  290. if direction == 'UP':
  291. snake_position[1] -= 10
  292. if direction == 'DOWN':
  293. snake_position[1] += 10
  294. if direction == 'LEFT':
  295. snake_position[0] -= 10
  296. if direction == 'RIGHT':
  297. snake_position[0] += 10
  298.  
  299. # Snake body growing mechanism
  300. # if fruits and snakes collide then scores will be
  301. # incremented by 10
  302. snake_body.insert(0, list(snake_position))
  303. if snake_position[0] == fruit_position[0] and snake_position[1] == fruit_position[1]:
  304. score += 10
  305. fruit_spawn = False
  306. else:
  307. snake_body.pop()
  308.  
  309. if not fruit_spawn:
  310. fruit_position = [random.randrange(1, (window_x//10)) * 10,
  311. random.randrange(1, (window_y//10)) * 10]
  312.  
  313. fruit_spawn = True
  314. game_window.fill(black)
  315.  
  316. for pos in snake_body:
  317. pygame.draw.rect(game_window, green, pygame.Rect(
  318. pos[0], pos[1], 10, 10))
  319.  
  320. pygame.draw.rect(game_window, white, pygame.Rect(
  321. fruit_position[0], fruit_position[1], 10, 10))
  322.  
  323. # Game Over conditions
  324. if snake_position[0] < 0 or snake_position[0] > window_x-10:
  325. game_over()
  326. if snake_position[1] < 0 or snake_position[1] > window_y-10:
  327. game_over()
  328.  
  329. # Touching the snake body
  330. for block in snake_body[1:]:
  331. if snake_position[0] == block[0] and snake_position[1] == block[1]:
  332. game_over()
  333.  
  334. # displaying score countinuously
  335. show_score(1, white, 'georgia', 20)
  336.  
  337.  
  338. # Refresh game screen
  339. pygame.display.update()
  340.  
  341. # Frame Per Second /Refresh Rate
  342. fps.tick(snake_speed)
  343. while True:
  344. menu_screen()
  345.  
  346.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement