Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Connect the required modules
- import pygame
- from random import randint
- pygame.init()
- # Creating a game window
- clock = pygame.time.Clock()
- back = (255, 255, 255) # Background color (R, G, B)
- mw = pygame.display.set_mode((500, 500)) # Main window size
- mw.fill(back) # Fill window with background colour
- # Preset colours to use throughout code
- BLACK = (0, 0, 0)
- LIGHT_BLUE = (200, 200, 255)
- class TextArea():
- def __init__(self, x=0, y=0, width=10, height=10, color=None):
- # Rectangle properties
- self.rect = pygame.Rect(x, y, width, height)
- # Fill color - either the passed parameter or the overall background color
- self.fill_color = color
- # Definition - Text settings
- def set_text(self, text, fsize=12, text_color=BLACK):
- self.text = text
- self.image = pygame.font.SysFont('Montserrat', fsize).render(text, True, text_color)
- # Draw a rectangle with text
- def draw(self, shift_x=0, shift_y=0):
- pygame.draw.rect(mw, self.fill_color, self.rect)
- mw.blit(self.image, (self.rect.x + shift_x, self.rect.y + shift_y))
- # Definitions are done, now we will create the cards!
- # TextArea needs (x, y, width, height, colour)
- quest_card = TextArea(120, 100, 290, 70, LIGHT_BLUE)
- quest_card.set_text("Question", 75)
- ans_card = TextArea(120, 240, 290, 70, LIGHT_BLUE)
- ans_card.set_text("Answer", 75)
- # Move these two commands out of the game loop
- quest_card.draw(10,10) # Question text alignment
- ans_card.draw(10,10) # Answer test alignment
- # Update the game loop to process Events - Q and A pressed on keyboard
- # You may clear the old loop and copy this version!
- while True:
- pygame.display.update()
- for event in pygame.event.get():
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_q:
- num = randint(1,3)
- if num == 1:
- quest_card.set_text('What do you study at Algorithmics?', 24)
- if num == 2:
- quest_card.set_text('What language is spoken in Brazil?', 24)
- if num == 3:
- quest_card.set_text('What grows on an apple tree?', 24)
- quest_card.draw(10, 25)
- if event.key == pygame.K_a:
- num = randint(1,3)
- if num == 1:
- ans_card.set_text('Python', 35)
- if num == 2:
- ans_card.set_text('Portuguese', 35)
- if num == 3:
- ans_card.set_text('Apples', 35)
- ans_card.draw(10, 25)
- clock.tick(20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement