Okay so I am trying to build Rock, Paper, Scissors with PyGame. I have shown many text in the Screen and they all worked except one.
Here’s my code:
import pygame, random
pygame.init()
fps = 60
screen_width = 1200
screen_height = 500
screen = pygame.display.set_mode((screen_width, screen_height))
white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
red = (255, 0, 0)
grey = (128, 128, 128)
font = pygame.font.SysFont('gabriola', 40)
pygame.display.set_caption('Rock, Paper or Scissors?')
class Button:
def __init__(self, text, width, height, pos, elevation):
# Core attributes
self.pressed = False
self.elevation = elevation
self.dynamic_elecation = elevation
self.original_y_pos = pos[1]
# top rectangle
self.top_rect = pygame.Rect(pos, (width, height))
self.top_color = green
# bottom rectangle
self.bottom_rect = pygame.Rect(pos, (width, height))
self.bottom_color = green
# text
self.text_surf = font.render(text, True, "#FFFFFF")
self.text_rect = self.text_surf.get_rect(center=self.top_rect.center)
def draw(self):
# elevation logic
self.top_rect.y = self.original_y_pos - self.dynamic_elecation
self.text_rect.center = self.top_rect.center
self.bottom_rect.midtop = self.top_rect.midtop
self.bottom_rect.height = self.top_rect.height + self.dynamic_elecation
pygame.draw.rect(screen, self.bottom_color, self.bottom_rect, border_radius=12)
pygame.draw.rect(screen, self.top_color, self.top_rect, border_radius=12)
screen.blit(self.text_surf, self.text_rect)
self.check_click()
def check_click(self):
mouse_pos = pygame.mouse.get_pos()
if self.top_rect.collidepoint(mouse_pos):
self.top_color = red
if pygame.mouse.get_pressed()[0]:
self.dynamic_elecation = 0
self.pressed = True
else:
self.dynamic_elecation = self.elevation
if self.pressed == True:
Rock()
else:
self.dynamic_elecation = self.elevation
self.top_color = "#475F77"
class Button2:
def __init__(self, text, width, height, pos, elevation):
# Core attributes
self.pressed = False
self.elevation = elevation
self.dynamic_elecation = elevation
self.original_y_pos = pos[1]
# top rectangle
self.top_rect = pygame.Rect(pos, (width, height))
self.top_color = green
# bottom rectangle
self.bottom_rect = pygame.Rect(pos, (width, height))
self.bottom_color = green
# text
self.text_surf = font.render(text, True, "#FFFFFF")
self.text_rect = self.text_surf.get_rect(center=self.top_rect.center)
def draw(self):
# elevation logic
self.top_rect.y = self.original_y_pos - self.dynamic_elecation
self.text_rect.center = self.top_rect.center
self.bottom_rect.midtop = self.top_rect.midtop
self.bottom_rect.height = self.top_rect.height + self.dynamic_elecation
pygame.draw.rect(screen, self.bottom_color, self.bottom_rect, border_radius=12)
pygame.draw.rect(screen, self.top_color, self.top_rect, border_radius=12)
screen.blit(self.text_surf, self.text_rect)
self.check_click()
def check_click(self):
mouse_pos = pygame.mouse.get_pos()
if self.top_rect.collidepoint(mouse_pos):
self.top_color = red
if pygame.mouse.get_pressed()[0]:
self.dynamic_elecation = 0
self.pressed = True
else:
self.dynamic_elecation = self.elevation
if self.pressed == True:
GameLoop()
else:
self.dynamic_elecation = self.elevation
self.top_color = "#475F77"
class Button_Rock:
def __init__(self, text, width, height, pos, elevation):
# Core attributes
self.pressed = False
self.elevation = elevation
self.dynamic_elecation = elevation
self.original_y_pos = pos[1]
# top rectangle
self.top_rect = pygame.Rect(pos, (width, height))
self.top_color = green
# bottom rectangle
self.bottom_rect = pygame.Rect(pos, (width, height))
self.bottom_color = green
# text
self.text_surf = font.render(text, True, "#FFFFFF")
self.text_rect = self.text_surf.get_rect(center=self.top_rect.center)
def draw(self):
# elevation logic
self.top_rect.y = self.original_y_pos - self.dynamic_elecation
self.text_rect.center = self.top_rect.center
self.bottom_rect.midtop = self.top_rect.midtop
self.bottom_rect.height = self.top_rect.height + self.dynamic_elecation
pygame.draw.rect(screen, self.bottom_color, self.bottom_rect, border_radius=12)
pygame.draw.rect(screen, self.top_color, self.top_rect, border_radius=12)
screen.blit(self.text_surf, self.text_rect)
self.check_click()
def check_click(self):
mouse_pos = pygame.mouse.get_pos()
if self.top_rect.collidepoint(mouse_pos):
self.top_color = red
if pygame.mouse.get_pressed()[0]:
self.dynamic_elecation = 0
self.pressed = True
else:
self.dynamic_elecation = self.elevation
if self.pressed == True:
Rock()
else:
self.dynamic_elecation = self.elevation
self.top_color = "#475F77"
class Button_Paper:
def __init__(self, text, width, height, pos, elevation):
# Core attributes
self.pressed = False
self.elevation = elevation
self.dynamic_elecation = elevation
self.original_y_pos = pos[1]
# top rectangle
self.top_rect = pygame.Rect(pos, (width, height))
self.top_color = green
# bottom rectangle
self.bottom_rect = pygame.Rect(pos, (width, height))
self.bottom_color = green
# text
self.text_surf = font.render(text, True, "#FFFFFF")
self.text_rect = self.text_surf.get_rect(center=self.top_rect.center)
def draw(self):
# elevation logic
self.top_rect.y = self.original_y_pos - self.dynamic_elecation
self.text_rect.center = self.top_rect.center
self.bottom_rect.midtop = self.top_rect.midtop
self.bottom_rect.height = self.top_rect.height + self.dynamic_elecation
pygame.draw.rect(screen, self.bottom_color, self.bottom_rect, border_radius=12)
pygame.draw.rect(screen, self.top_color, self.top_rect, border_radius=12)
screen.blit(self.text_surf, self.text_rect)
self.check_click()
def check_click(self):
mouse_pos = pygame.mouse.get_pos()
if self.top_rect.collidepoint(mouse_pos):
self.top_color = red
if pygame.mouse.get_pressed()[0]:
self.dynamic_elecation = 0
self.pressed = True
else:
self.dynamic_elecation = self.elevation
if self.pressed == True:
Paper()
else:
self.dynamic_elecation = self.elevation
self.top_color = "#475F77"
class Button_Scissors:
def __init__(self, text, width, height, pos, elevation):
# Core attributes
self.pressed = False
self.elevation = elevation
self.dynamic_elecation = elevation
self.original_y_pos = pos[1]
# top rectangle
self.top_rect = pygame.Rect(pos, (width, height))
self.top_color = green
# bottom rectangle
self.bottom_rect = pygame.Rect(pos, (width, height))
self.bottom_color = green
# text
self.text_surf = font.render(text, True, "#FFFFFF")
self.text_rect = self.text_surf.get_rect(center=self.top_rect.center)
def draw(self):
# elevation logic
self.top_rect.y = self.original_y_pos - self.dynamic_elecation
self.text_rect.center = self.top_rect.center
self.bottom_rect.midtop = self.top_rect.midtop
self.bottom_rect.height = self.top_rect.height + self.dynamic_elecation
pygame.draw.rect(screen, self.bottom_color, self.bottom_rect, border_radius=12)
pygame.draw.rect(screen, self.top_color, self.top_rect, border_radius=12)
screen.blit(self.text_surf, self.text_rect)
self.check_click()
def check_click(self):
mouse_pos = pygame.mouse.get_pos()
if self.top_rect.collidepoint(mouse_pos):
self.top_color = red
if pygame.mouse.get_pressed()[0]:
self.dynamic_elecation = 0
self.pressed = True
else:
self.dynamic_elecation = self.elevation
if self.pressed == True:
Scissors()
else:
self.dynamic_elecation = self.elevation
self.top_color = "#475F77"
def Text(text:str, color:tuple, x:int, y:int):
screenText = font.render(text, True, color)
screen.blit(screenText, [x, y])
def WelcomeScreen(run=False):
screen.fill(grey)
Text('Welcome to the game! Press the Below Button to Continue.', green, 250, 150)
welcomeButton = Button2('Continue', 150, 40, (450, 250), 5)
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = True
welcomeButton.draw()
pygame.display.update()
def GameLoop(run=False):
button1 = Button('Click here!', 150, 40, (30, 100), 5)
button2 = Button_Paper('Click here!', 150, 40, (520, 100), 5)
button3 = Button_Scissors('Click here!', 150, 40, (980, 100), 5)
screen.fill(grey)
Text('Rock, Paper or Scissors?', blue, 450, 0)
Text('Rock', grey, 250, 150)
Text('Paper', green, 550, 50)
Text('Scissors', red, 1000, 50)
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = True
button1.draw()
button2.draw()
button3.draw()
pygame.display.update()
def Rock(run=False):
screen.fill(white)
button1 = Button('Click here!', 150, 40, (30, 100), 5)
button2 = Button('Click here!', 150, 40, (520, 100), 5)
button3 = Button('Click here!', 150, 40, (980, 100), 5)
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = True
pygame.display.update()
def Paper(run=False):
pass
def Scissors(run=False):
pass
if __name__ == "__main__":
WelcomeScreen()
In this code, The text ‘Rock’ in the Function named GameLoop isn’t showing in the screen but every other text is showing in the window.
Text('Rock', grey, 250, 150)
Can anyone please help?
>Solution :
The text "Rock" and the background are both the same grey color, so you cannot see the text. Try changing it to a different color, for example, white:
def GameLoop(run=False):
button1 = Button('Click here!', 150, 40, (30, 100), 5)
button2 = Button_Paper('Click here!', 150, 40, (520, 100), 5)
button3 = Button_Scissors('Click here!', 150, 40, (980, 100), 5)
screen.fill(grey) # Same color as rock
Text('Rock, Paper or Scissors?', blue, 450, 0)
Text('Rock', white, 100, 50) # Change color to white and position to match other text
Text('Paper', green, 550, 50)
Text('Scissors', red, 1000, 50)
...