Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 14e7346

Browse files
Merge branch 'main' into feature/chat-gpt-discord-bot
2 parents b54eed3 + 6100185 commit 14e7346

File tree

17 files changed

+1221
-351
lines changed

17 files changed

+1221
-351
lines changed

README.md

Lines changed: 192 additions & 178 deletions
Large diffs are not rendered by default.

projects/Quiz Game/main.py

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
# import the sqlite3 to reterive previous scores
2+
import sqlite3
3+
# create Conn to have the database file
4+
def create_connection(db_file):
5+
""" create a database connection to the SQLite database """
6+
conn = None
7+
try:
8+
conn = sqlite3.connect(db_file)
9+
except sqlite3.Error as e:
10+
print(e)
11+
return conn
12+
# store the player's score with their name
13+
def save_score(conn, name, score):
14+
"""
15+
Save the player's score to the database
16+
"""
17+
sql = ''' INSERT INTO scores(name, score)
18+
VALUES(?,?) '''
19+
cur = conn.cursor()
20+
cur.execute(sql, (name, score))
21+
conn.commit()
22+
return cur.lastrowid
23+
# recall the previous scores to display them
24+
def get_all_scores(conn):
25+
"""
26+
Query all rows in the scores table
27+
"""
28+
cur = conn.cursor()
29+
cur.execute("SELECT * FROM scores ORDER BY score DESC")
30+
31+
rows = cur.fetchall()
32+
return rows
33+
34+
# The beginning of the game
135
print("Welcome to AskPython Quiz")
236

337
# Get user's readiness to play the Quiz
@@ -26,23 +60,42 @@
2660
print("Wrong Answer :(") # User's answer is incorrect
2761

2862
# Question 3
29-
answer = input(
30-
"Question 3: What is the name of your favourite website for learning Python?"
31-
)
63+
answer = input("Question 3: What is the name of your favourite website for learning Python?")
3264
if answer.lower() == "askpython":
3365
score += 1
3466
print("correct") # User's answer is correct, increment the score
3567
else:
3668
print("Wrong Answer :(") # User's answer is incorrect
3769

38-
# Display the result and user's score
39-
print(
40-
"Thank you for Playing this small quiz game, you attempted",
41-
score,
42-
"questions correctly!",
43-
)
44-
mark = int((score / total_questions) * 100)
45-
print(f"Marks obtained: {mark}%")
70+
# Display the result and user's score
71+
print("Thank you for Playing this small quiz game, you attempted", score, "questions correctly!")
72+
mark = int((score / total_questions) * 100)
73+
print(f"Marks obtained: {mark}%")
74+
75+
# Getting the player's name and score to insert into the database
76+
player_name = input("Enter your name: ")
77+
player_score = score
78+
79+
database = "quiz_game.db"
80+
81+
# Create a database connection
82+
conn = create_connection(database)
83+
84+
if conn is not None:
85+
# Save the player's score
86+
save_score(conn, player_name, player_score)
87+
88+
# Display all scores
89+
print("Previous scores:")
90+
scores = get_all_scores(conn)
91+
for row in scores:
92+
print(f"Name: {row[1]}, Score: {row[2]}, Date: {row[3]}")
93+
94+
conn.close()
95+
else:
96+
print("Error! Cannot create the database connection.")
97+
else:
98+
print(" Please, when you're ready, enter the game again.")
4699

47100
# Farewell message
48101
print("BYE!")

projects/Quiz Game/setup_db.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sqlite3
2+
3+
def create_database():
4+
# Connect to the SQLite database (it will create the file if it doesn't exist)
5+
conn = sqlite3.connect('quiz_game.db')
6+
7+
# Create a cursor object
8+
cursor = conn.cursor()
9+
10+
# Create a table to store players' scores
11+
cursor.execute('''
12+
CREATE TABLE IF NOT EXISTS scores (
13+
id INTEGER PRIMARY KEY AUTOINCREMENT,
14+
name TEXT NOT NULL,
15+
score INTEGER NOT NULL,
16+
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
17+
)
18+
''')
19+
20+
# Commit the changes and close the connection
21+
conn.commit()
22+
conn.close()
23+
24+
print("Database and table created successfully.")
25+
26+
if __name__ == "__main__":
27+
create_database()

projects/Snake Game/game.py

Lines changed: 0 additions & 162 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from enum import Enum
2+
from collections import namedtuple
3+
4+
5+
class RgbColors:
6+
WHITE = (255, 255, 255)
7+
RED = (200, 0, 0)
8+
BLUE1 = (0, 0, 255)
9+
BLUE2 = (0, 100, 255)
10+
BLACK = (0, 0, 0)
11+
12+
13+
class GameSettings:
14+
BLOCK_SIZE = 20
15+
SPEED = 5
16+
WIDTH = 640
17+
HEIGHT = 480
18+
19+
20+
class Direction(Enum):
21+
RIGHT = 1
22+
LEFT = 2
23+
UP = 3
24+
DOWN = 4
25+
26+
27+
Point = namedtuple('Point', ['x', 'y'])

projects/Snake Game/src/display.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import pygame
2+
from constants import RgbColors, GameSettings
3+
4+
5+
class Display:
6+
"""Manages the display of the game."""
7+
def __init__(self):
8+
pygame.init()
9+
self.width = GameSettings.WIDTH
10+
self.height = GameSettings.HEIGHT
11+
self.font = pygame.font.Font(None, 25)
12+
self.window = pygame.display.set_mode((self.width, self.height))
13+
pygame.display.set_caption("Snake")
14+
self.clock = pygame.time.Clock()
15+
16+
def update_ui(self, snake, food, score, high_score):
17+
"""Updates the UI with the current game state.
18+
19+
Args:
20+
snake (Snake): The snake object that contains the snake body (Snake.blocks).
21+
food (Point): The food object to be displayed.
22+
score (int): The current game score.
23+
high_score: The highest score achieved so far.
24+
"""
25+
self.window.fill(RgbColors.BLACK)
26+
self.draw_snake(snake)
27+
self.draw_food(food)
28+
self.draw_score(score)
29+
self.render_high_score(high_score)
30+
pygame.display.flip()
31+
32+
def draw_snake(self, snake):
33+
for block in snake.blocks:
34+
pygame.draw.rect(
35+
self.window, RgbColors.BLUE1,
36+
pygame.Rect(block.x, block.y, GameSettings.BLOCK_SIZE, GameSettings.BLOCK_SIZE)
37+
)
38+
pygame.draw.rect(
39+
self.window, RgbColors.BLUE2, pygame.Rect(block.x + 4, block.y + 4, 12, 12)
40+
)
41+
42+
def draw_food(self, food):
43+
pygame.draw.rect(
44+
self.window,
45+
RgbColors.RED,
46+
pygame.Rect(food.x, food.y, GameSettings.BLOCK_SIZE, GameSettings.BLOCK_SIZE),
47+
)
48+
49+
def draw_score(self, score):
50+
self.font = pygame.font.Font(None, 25)
51+
score_display = self.font.render(f"Score: {score}", True, RgbColors.WHITE)
52+
self.window.blit(score_display, [0, 0])
53+
54+
def render_game_over(self):
55+
self.font = pygame.font.Font(None, 48)
56+
game_over_display = self.font.render("GAME OVER", True, RgbColors.WHITE)
57+
text_width = game_over_display.get_width()
58+
text_height = game_over_display.get_height()
59+
text_x = (self.width - text_width) // 2
60+
text_y = (self.height // 4) - (text_height // 2)
61+
self.window.blit(game_over_display,
62+
[text_x, text_y])
63+
pygame.display.flip()
64+
65+
def render_play_again(self):
66+
self.font = pygame.font.Font(None, 32)
67+
play_again_display = self.font.render("Play again? (Y/N)", True, RgbColors.WHITE)
68+
display_box = play_again_display.get_rect(center=(self.width // 2, self.height // 2))
69+
self.window.blit(play_again_display, display_box)
70+
pygame.display.flip()
71+
72+
def render_high_score(self, high_score):
73+
high_score_display = self.font.render(f"High Score: {high_score}", True, RgbColors.WHITE)
74+
self.window.blit(high_score_display, [self.width - high_score_display.get_width(), 0])
75+
76+
def render_new_high_score(self, new_high_score):
77+
new_high_score_display = self.font.render(f"New High Score: {new_high_score}", True, RgbColors.WHITE)
78+
text_width = new_high_score_display.get_width()
79+
text_height = new_high_score_display.get_height()
80+
text_x = (self.width - text_width) // 2
81+
text_y = (self.height // 3) - (text_height // 2)
82+
self.window.blit(new_high_score_display,
83+
[text_x, text_y])
84+
pygame.display.flip()

0 commit comments

Comments
 (0)