Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # importing libraries
- import pygame
- import time
- import random
- from tkinter import *
- import tkinter.messagebox
- import mysql.connector as mc
- cn=mc.connect(host="localhost",user="alen",password="alen")
- cur=cn.cursor()
- cur.execute("create database if not exists snake_game_dat")
- cur.execute("use snake_game_dat")
- cn.autocommit
- cur.execute("create table if not exists snake_game_score(username varchar(40), password int(5), score int(100))")
- snake_speed = 15
- es1val=15
- def menu_screen():
- global em1,em2,log
- log = Tk()
- label = Label(log, text="Snake Game", font='georgia 100 bold')
- label.pack()
- frame = Frame(log, height=480, width=720)
- frame.pack()
- playin = Button(log, text='Play',font='georgia 10 bold',height=2,width=30, command=play)
- playin.place(x=200,y=200)
- score1 = Button(log, text='High Scores',font='georgia 10 bold',height=2,width=30, command=score)
- score1.place(x=200,y=250)
- settin = Button(log, text='Settings',font='georgia 10 bold',height=2,width=30, command=settings)
- settin.place(x=200,y=300)
- quitpg = Button(log, text="Quit", font='georgia 10 bold',height=2,width=30, command=quitpage)
- quitpg.place(x=200,y=350)
- setin12=False
- log.resizable(False,False)
- log.mainloop()
- def score():
- log.destroy()
- global esc1,esc2,sctk
- sctk= Tk()
- '''cur.execute("select user,score from snake_game_score desc")
- data=cur.fetchall()
- for ktk in data:
- for j in ktk:
- l1 = Label(setin, text='Speed')
- l1.place(x=50,y=300)'''
- def display_text():
- global es1val
- string = es1.get()
- print(string)
- if string=='':
- string=15
- es1val = int(string)
- def menu_screen1():
- setin.destroy()
- menu_screen()
- def settings():
- log.destroy()
- global es1,es2,setin,l1
- setin = Tk()
- label = Label(setin, text='Settings', font='georgia 100 bold')
- label.pack()
- frame = Frame(setin, height=480, width=720)
- frame.pack()
- l1 = Label(setin, text='Speed')
- l1.place(x=50,y=300)
- es1 = tkinter.Entry(setin)
- es1.focus_set()
- es1.pack()
- es1.place(x=140,y=300)
- butt2 = Button(setin, text= "Okay",width= 20, command= display_text)
- butt2.place(x=200,y=300)
- butt1 = Button(setin,text='Main menu',font='georgia 10 bold',height=2,width=30, command=menu_screen1)
- butt1.place(x=200,y=200)
- setin.resizable(False,False)
- setin.mainloop()
- def login_get():
- uname = e1.get()
- pwod = int(e2.get())
- cur.execute("select username,password from snake_game_score")
- R=cur.fetchall()
- if (uname,pwod) in R:
- log1.destroy()
- play()
- else:
- global logg
- logg = Tk()
- label = Label(setin, text='Error', font='georgia 20 bold')
- label.pack()
- frame = Frame(setin, height=180, width=120)
- l3 = Label(logg, text='Wrong username or password')
- l3.place(x=140,y=180)
- loggbu = Button(logg, text='Back to login',font='georgia 10 bold',height=2,width=30,command=destlogg)
- loggbu.place(x=200,y=200)
- logg.resizabale(False,False)
- logg.mainloop()
- def destlogg():
- logg.destroy()
- def login():
- log.destroy()
- global e1,e2,log1
- log = Tk()
- label = Label(log1, text='Login', font='georgia 100 bold')
- label.pack()
- frame = Frame(log1, height=480, width=720)
- frame.pack()
- l1 = Label(log1, text='Username')
- l1.place(x=50,y=100)
- e1 = tkinter.Entry(log1)
- e1.place(x=140,y=100)
- l2 = Label(log1, text='Password')
- l2.place(x=50,y=150)
- e2 = tkinter.Entry(log1)
- e2.place(x=140,y=150)
- butt = Button(log1, text='Login',command=login_get)
- butt.place(x=150,y=200)
- sg = Label(log1, text="Don't have an account?")
- sg.place(x=100,y=240)
- sigup = Button(log1, text='Sign Up',font='georgia 10 bold',height=2,width=30, command=signup_page)
- sigup.place(x=200,y=200)
- log.resizable(False,False)
- log.mainloop()
- def quitpage():
- log.destroy()
- quit()
- def signup_page():
- global et1,et2,log1
- log.destroy()
- signup = Tk()
- label = Label(signup, text='Sign Up', font='georgia 30 bold')
- label.pack()
- frame = Frame(signup, height=300, width=400)
- frame.pack()
- l1 = Label(signup, text='Username')
- l1.place(x=50,y=100)
- et1 = tkinter.Entry(signup)
- et1.place(x=140,y=100)
- l2 = Label(signup, text='Password')
- l2.place(x=50,y=150)
- et2 = tkinter.Entry(signup)
- et2.place(x=140,y=150)
- butt = Button(signup, text='Sign Up')
- butt.place(x=150,y=200)
- signup.resizable(False,False)
- signup.mainloop()
- def save_score():
- #cur.execute("insert into snake_game_score values(uname,pwod,score))")
- cur.execute("INSERT INTO snake_game_score values (%s,%s,%s)", uname, pwod, score)
- # Main Function
- def play():
- log.destroy()
- snake_speed = es1val
- # Window size
- window_x = 720
- window_y = 480
- # defining colors
- black = pygame.Color(0, 0, 0)
- white = pygame.Color(255, 255, 255)
- red = pygame.Color(255, 0, 0)
- green = pygame.Color(0, 0, 255)
- blue = pygame.Color(0, 255, 0)
- # Initialising pygame
- pygame.init()
- # Initialise game window
- pygame.display.set_caption('Snakes Game')
- game_window = pygame.display.set_mode((window_x, window_y))
- # FPS (frames per second) controller
- fps = pygame.time.Clock()
- # defining snake default position
- snake_position = [100, 50]
- # defining first 4 blocks of snake
- # body
- snake_body = [ [100, 50],[90, 50],[80, 50],[70, 50]]
- # fruit position
- fruit_position = [random.randrange(1, (window_x//10)) * 10,
- random.randrange(1, (window_y//10)) * 10]
- fruit_spawn = True
- # setting default snake direction
- # towards right
- direction = 'RIGHT'
- change_to = direction
- # initial score
- score = 0
- # game over function
- def game_over():
- save_score()
- # creating font object my_font
- my_font = pygame.font.SysFont('georgia', 20)
- my_font1=pygame.font.SysFont('calibri',50)
- # creating a text surface on which text
- # will be drawn
- game_over_surface1 = my_font1.render('Game Over',True,red)
- game_over_surface = my_font.render('Your Score is : ' + str(score), True, red)
- # create a rectangular object for the text
- # surface object
- game_over_rect = game_over_surface.get_rect()
- game_over_rect1=game_over_surface.get_rect()
- # setting position of the text
- game_over_rect1.midtop=(window_x/2.3,window_y/5)
- game_over_rect.midtop = (window_x/2, window_y/3)
- # blit will draw the text on screen
- game_window.blit(game_over_surface1,game_over_rect1)
- game_window.blit(game_over_surface, game_over_rect)
- pygame.display.flip()
- # after 2 seconds we will quit the
- # program
- time.sleep(1)
- pygame.quit()
- menu_screen()
- # displaying Score function
- def show_score(choice, color, font, size):
- # creating font object score_font
- score_font = pygame.font.SysFont(font, size)
- # create the display surface object
- # score_surface
- score_surface = score_font.render('Score : ' + str(score), True, color)
- # create a rectangular object for the
- # text surface object
- score_rect = score_surface.get_rect()
- # displaying text
- game_window.blit(score_surface, score_rect)
- while True:
- # handling key events
- for event in pygame.event.get():
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_UP:
- change_to = 'UP'
- if event.key == pygame.K_DOWN:
- change_to = 'DOWN'
- if event.key == pygame.K_LEFT:
- change_to = 'LEFT'
- if event.key == pygame.K_RIGHT:
- change_to = 'RIGHT'
- # If two keys pressed simultaneously
- # we don't want snake to move into two directions
- # simultaneously
- if change_to == 'UP' and direction != 'DOWN':
- direction = 'UP'
- if change_to == 'DOWN' and direction != 'UP':
- direction = 'DOWN'
- if change_to == 'LEFT' and direction != 'RIGHT':
- direction = 'LEFT'
- if change_to == 'RIGHT' and direction != 'LEFT':
- direction = 'RIGHT'
- # Moving the snake
- if direction == 'UP':
- snake_position[1] -= 10
- if direction == 'DOWN':
- snake_position[1] += 10
- if direction == 'LEFT':
- snake_position[0] -= 10
- if direction == 'RIGHT':
- snake_position[0] += 10
- # Snake body growing mechanism
- # if fruits and snakes collide then scores will be
- # incremented by 10
- snake_body.insert(0, list(snake_position))
- if snake_position[0] == fruit_position[0] and snake_position[1] == fruit_position[1]:
- score += 10
- fruit_spawn = False
- else:
- snake_body.pop()
- if not fruit_spawn:
- fruit_position = [random.randrange(1, (window_x//10)) * 10,
- random.randrange(1, (window_y//10)) * 10]
- fruit_spawn = True
- game_window.fill(black)
- for pos in snake_body:
- pygame.draw.rect(game_window, green, pygame.Rect(
- pos[0], pos[1], 10, 10))
- pygame.draw.rect(game_window, white, pygame.Rect(
- fruit_position[0], fruit_position[1], 10, 10))
- # Game Over conditions
- if snake_position[0] < 0 or snake_position[0] > window_x-10:
- game_over()
- if snake_position[1] < 0 or snake_position[1] > window_y-10:
- game_over()
- # Touching the snake body
- for block in snake_body[1:]:
- if snake_position[0] == block[0] and snake_position[1] == block[1]:
- game_over()
- # displaying score countinuously
- show_score(1, white, 'georgia', 20)
- # Refresh game screen
- pygame.display.update()
- # Frame Per Second /Refresh Rate
- fps.tick(snake_speed)
- while True:
- menu_screen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement