'How do I create a login for my game using sqlite3?
I am currently trying to figure out how to add a login screen to my game that takes the entered player name and keeps track of the players high score, any help with this?
Solution 1:[1]
SQLite: How to make a table, insert some values and read it with python:
import sqlite3
# open connection
connection = sqlite3.connect("db.db")
cursor = connection.cursor()
# create table
cursor.execute("CREATE TABLE if not exists scores (player text, score int)")
connection.commit()
# insert value
cursor.execute("INSERT INTO scores VALUES (?,?)", ("player1", 180))
cursor.execute("INSERT INTO scores VALUES (?,?)", ("player2", 50))
cursor.execute("INSERT INTO scores VALUES (?,?)", ("player3", 300))
connection.commit()
# get value
cursor.execute("SELECT score FROM scores WHERE player = 'player2'")
output = cursor.fetchall()
print(output)
# close connection
connection.close()
First, I open the file db.db. Then I create a table and save it with commit(). Then I inserted some values. Then I got the values and print them to the console.
How to do the pygame window thing:
import pygame
# set window size
X = 400
Y = 400
# init pygame window
pygame.init()
display_surface = pygame.display.set_mode((X, Y))
pygame.display.set_caption('Window title')
# set pygame colors
white = (255, 255, 255)
black = (0, 0, 0)
# init text
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render('Text', True, black, white)
# center text
textRect = text.get_rect()
textRect.center = (X // 2, Y // 2)
# loop
while True:
display_surface.fill(white)
display_surface.blit(text, textRect)
# if close button quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# update window
pygame.display.update()
and if you combine it, the code will look like this:
import pygame
import sqlite3
# sqlite
# open connection
connection = sqlite3.connect("db.db")
cursor = connection.cursor()
# create table
cursor.execute("CREATE TABLE if not exists scores (player text, score int)")
connection.commit()
# insert value
cursor.execute("INSERT INTO scores VALUES (?,?)", ("player1", 180))
cursor.execute("INSERT INTO scores VALUES (?,?)", ("player2", 50))
cursor.execute("INSERT INTO scores VALUES (?,?)", ("player3", 300))
connection.commit()
# get value
cursor.execute("SELECT score FROM scores WHERE player = 'player2'")
output = str(cursor.fetchall()[0][0])
# pygame
# set window size
X = 400
Y = 400
# init pygame window
pygame.init()
display_surface = pygame.display.set_mode((X, Y))
pygame.display.set_caption('Window title')
# set pygame colors
white = (255, 255, 255)
black = (0, 0, 0)
# init text
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render(output, True, black, white)
# center text
textRect = text.get_rect()
textRect.center = (X // 2, Y // 2)
# loop
while True:
display_surface.fill(white)
display_surface.blit(text, textRect)
# if close button quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
connection.close()
pygame.quit()
quit()
# update window
pygame.display.update()
and the window like this:
Please comment, if this has worked for you, and accept the answer to help others with the same problem. If you are still struggling, comment what is not working.
Have a good day!:)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

