I am facing difficulties in rendering different text beside the buttons in question for different difficulty level. I am a newbie with not much knowledge on Python Pygame. Please help me.
Asked
Active
Viewed 27 times
0
-
Right now I have rendered the text beside the button, and I am stuck. I need help rendering different text for different questions and difficulty level – Jervinn Gohh Dec 14 '22 at 07:56
1 Answers
0
import pygame
import pygame
import pygame.freetype
import random
class SimpleScene:
FONT = None
def __init__(self, next_scene, *text):
self.background = pygame.Surface((800, 600))
self.background =
pygame.image.load('Spacebackground.png').convert_alpha()
y = 80
if text:
if SimpleScene.FONT == None:
SimpleScene.FONT = pygame.freetype.SysFont(None, 32)
for line in text:
SimpleScene.FONT.render_to(self.background, (210, y), line,
pygame.Color('black'))
SimpleScene.FONT.render_to(self.background, (209, y-1),
line, pygame.Color('white'))
y += 50
self.next_scene = next_scene
self.additional_text = None
def start(self, text):
self.additional_text = text
def draw(self, screen):
screen.blit(self.background, (0, 0))
if self.additional_text:
y = 180
for line in self.additional_text:
SimpleScene.FONT.render_to(screen, (250, y), line,
pygame.Color('black'))
SimpleScene.FONT.render_to(screen, (249, y-1), line,
pygame.Color('white'))
y += 50
def update(self, events, dt):
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
return (self.next_scene, None)
class GameState:
def __init__(self, difficulty):
self.difficulty = difficulty
if difficulty == 1:
self.questions = [
('How many legs has a cow?', 4),
('How many legs has a bird?', 2),
('What is 1 x 1 ?', 1)
]
self.current_question = None
self.right = 0
self.wrong = 0
elif difficulty == 2:
self.questions = [
('How many legs has a dog?', 4),
('How many legs has a human?', 2),
('What is 1 + 1 ?', 2)
]
self.current_question = None
self.right = 0
self.wrong = 0
elif difficulty == 3:
self.questions = [
('How many legs has a cat?', 4),
('How many legs has a flamingo?', 2),
('What is 2 - 1 ?', 1)
]
self.current_question = None
self.right = 0
self.wrong = 0
else:
self.questions = [
('How many legs has a rabbit?', 4),
('How many legs has a cheetah?', 4),
('What is 3 - 2 ?', 1)
]
self.current_question = None
self.right = 0
self.wrong = 0
def pop_question(self):
# Return the first question in the list and remove it from the list
q = self.questions[0]
self.questions.remove(q)
self.current_question = q
return q
def difficulty(self):
d = self.difficulty[0]
def answer(self, answer):
if answer == self.current_question[1]:
self.right += 1
else:
self.wrong += 1
def get_result(self):
return f'{self.right} answers correct', f'{self.wrong}
answers wrong', '', 'Good!' if self.right > self.wrong else 'You
can do better!'
class SettingScene:
def __init__(self):
self.background = pygame.Surface((800, 600))
self.background =
pygame.image.load('Spacebackground.png').convert_alpha()
if SimpleScene.FONT == None:
SimpleScene.FONT = pygame.freetype.SysFont(None, 32)
SimpleScene.FONT.render_to(self.background, (120, 50),
'Select your difficulty level', pygame.Color('black'))
SimpleScene.FONT.render_to(self.background, (119, 49),
'Select your difficulty level', pygame.Color('white'))
self.rects = []
x = 120
y = 120
for n in range(4):
rect = pygame.Rect(x, y, 80, 80)
self.rects.append(rect)
y += 100
self.font = pygame.freetype.SysFont(None, 32)
# draw the text for the buttons
self.font.render_to(self.background, (300, 150), 'Easy + 100 coins', pygame.Color('white'))
self.font.render_to(self.background, (300, 250), 'Medium + 200 coins', pygame.Color('white'))
self.font.render_to(self.background, (300, 350), 'Hard + 500 coins', pygame.Color('white'))
self.font.render_to(self.background, (300, 450), 'Insane + 1000 coins', pygame.Color('white'))
def start(self, *args):
pass
def draw(self, screen):
screen.blit(self.background, (0, 0))
n = 1
for rect in self.rects:
if rect.collidepoint(pygame.mouse.get_pos()):
pygame.draw.rect(screen, pygame.Color('darkgrey'), rect)
pygame.draw.rect(screen, pygame.Color('darkgrey'), rect, 5)
SimpleScene.FONT.render_to(screen, (rect.x+30, rect.y+30), str(n), pygame.Color('black'))
SimpleScene.FONT.render_to(screen, (rect.x+29, rect.y+29), str(n), pygame.Color('white'))
n+=1
def update(self, events, dt):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
n = 1
for rect in self.rects:
if rect.collidepoint(event.pos):
return ('GAME', GameState(n))
n += 1
class GameScene:
def __init__(self):
if SimpleScene.FONT == None:
SimpleScene.FONT = pygame.freetype.SysFont(None, 32)
self.rects = []
x = 120
y = 150
for n in range(4):
rect = pygame.Rect(x, y, 80, 80)
self.rects.append(rect)
#change to X-axis if you want to display in horizontal and Y-axis for vertical display
y += 100
def start(self, gamestate):
self.background = pygame.Surface((800, 600))
self.background = pygame.image.load('background.png').convert_alpha()
self.gamestate = gamestate
question, answer = gamestate.pop_question()
SimpleScene.FONT.render_to(self.background, (120, 50), question, pygame.Color('black'))
SimpleScene.FONT.render_to(self.background, (119, 49), question, pygame.Color('white'))
if GameState.difficulty == 1 :
self.font = pygame.freetype.SysFont(None, 32)
# draw the text for the buttons
text_options = ['Bag', 'Pen', 'Eraser', 'Pencil Case']
self.font.render_to(self.background, (300, 180), text_options[0], pygame.Color('white'))
self.font.render_to(self.background, (300, 280), text_options[1], pygame.Color('white'))
self.font.render_to(self.background, (300, 380), text_options[2], pygame.Color('white'))
self.font.render_to(self.background, (300, 480), text_options[3], pygame.Color('white'))
else:
self.font = pygame.freetype.SysFont(None, 32)
# draw the text for the buttons
text_options = ['Leg', 'Arm', 'Hair', 'Head']
self.font.render_to(self.background, (300, 180), text_options[0], pygame.Color('white'))
self.font.render_to(self.background, (300, 280), text_options[1], pygame.Color('white'))
self.font.render_to(self.background, (300, 380), text_options[2], pygame.Color('white'))
self.font.render_to(self.background, (300, 480), text_options[3], pygame.Color('white'))
# function that draws the button options inside
def draw(self, screen):
screen.blit(self.background, (0, 0))
n = 1
for rect in self.rects:
if rect.collidepoint(pygame.mouse.get_pos()):
pygame.draw.rect(screen, pygame.Color('darkgrey'), rect)
pygame.draw.rect(screen, pygame.Color('darkgrey'), rect, 5)
SimpleScene.FONT.render_to(screen, (rect.x+30, rect.y+30), str(n), pygame.Color('black'))
SimpleScene.FONT.render_to(screen, (rect.x+29, rect.y+29), str(n), pygame.Color('white'))
n+=1
def update(self, events, dt):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
n = 1
for rect in self.rects:
if rect.collidepoint(event.pos):
self.gamestate.answer(n)
if self.gamestate.questions:
return ('GAME', self.gamestate)
else:
return ('RESULT', self.gamestate.get_result())
n += 1
def main():
pygame.init()
screen = pygame.display.set_mode((800 , 600))
clock = pygame.time.Clock()
dt = 0
scenes = {
'TITLE': SimpleScene('SETTING', 'Welcome to the quiz', '', '', '', 'press [SPACE] to start'),
'SETTING': SettingScene(),
'GAME': GameScene(),
'RESULT': SimpleScene('TITLE', 'Here is your result:'),
}
scene = scenes['TITLE']
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
result = scene.update(events, dt)
if result:
next_scene, state = result
if next_scene:
scene = scenes[next_scene]
scene.start(state)
scene.draw(screen)
pygame.display.flip()
dt = clock.tick(60)
if __name__ == '__main__':
main()

Jervinn Gohh
- 1
- 2