forked from cis3296f23/Project-02-checkers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.py
98 lines (79 loc) · 3.85 KB
/
Player.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
"""
Player.py
The Player File holds the Player class which is responsible for managing the players. This file holds a global user scores hashmap.
"""
import pygame
Width, Height = 1000, 700
background_image = pygame.image.load("checkers.jpg")
background_image = pygame.transform.scale(background_image, (Width, Height))
screen = pygame.display.set_mode([Width, Height])
# Global map for user scores
user_scores = {}
class Player:
"""
The Player class is responsible for managing the players, and contains functions to initialize a player, update the win and loss, and get the player name.
"""
def __init__(self, username, score):
"""
The init function initializes the player object with a username and score.
"""
self.username = username
self.score = 0
self.win = 0
def draw_text_input(self, player_name, error_msg="name error"):
"""
The draw text input function draws the text input box on the screen.
"""
font = pygame.font.Font(None, 32)
# Render the background box
pygame.draw.rect(screen, (128, 128, 128), (Width // 2 - 225, Height // 2 - 25, 450, 50)) # Adjust size and position as needed
# Render the text on the background box
input_text = font.render("Enter Your Name: " + player_name, True, (255, 255, 255))
input_rect = input_text.get_rect(center=(Width // 2, Height // 2))
screen.blit(input_text, input_rect)
# Render the error message below the input box
if error_msg:
error_font = pygame.font.Font(None, 24)
error_text = error_font.render(error_msg, True, (255, 0, 0)) # Red color for error message
error_rect = error_text.get_rect(center=(Width // 2, Height // 2 + 30)) # Adjust position as needed
screen.blit(error_text, error_rect)
def get_player_name(self):
"""
The get player name function gets the player name from the user and returns the player name.
"""
input_active = True
player_name = ""
error_msg = ""
while input_active:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
#if player_name.isalnum() and len(player_name) <= 20:
if (event.unicode.isalnum() or event.unicode.isspace()) and len(player_name) < 20:
input_active = False # Stop taking input when Enter is pressed and the name is valid
else:
error_msg = "Invalid name. Please use English letters or numbers, and keep it under 20 characters."
elif event.key == pygame.K_BACKSPACE:
player_name = player_name[:-1] # Remove the last character when Backspace is pressed
else:
# Check if the typed character is an English letter, a number, or a space
if (event.unicode.isalnum() or event.unicode.isspace()) and len(player_name) < 20:
player_name += event.unicode # Append the typed character to the player_name
screen.blit(background_image, (0, 0))
self.draw_text_input(player_name, error_msg)
pygame.display.flip()
self.username = player_name
return player_name
def update_win(self):
"""
The update win function updates the win for a player by setting the win attribute to one.
"""
self.win = 1
def update_loss(self):
"""
The update loss function updates the loss for a player by setting the win attribute to zero.
"""
self.win = 0