Advertisement
shh_algo_PY

Q and A Game - Module 6

Jun 18th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. # Connect the required modules
  2.  
  3. import pygame
  4. from random import randint
  5. pygame.init()
  6.  
  7. # Creating a game window
  8.  
  9. clock = pygame.time.Clock()
  10. back = (255, 255, 255)                      # Background color (R, G, B)
  11. mw = pygame.display.set_mode((500, 500))    # Main window size
  12. mw.fill(back)                               # Fill window with background colour
  13.  
  14. # Preset colours to use throughout code
  15.  
  16. BLACK = (0, 0, 0)
  17. LIGHT_BLUE = (200, 200, 255)
  18.  
  19. class TextArea():
  20.     def __init__(self, x=0, y=0, width=10, height=10, color=None):
  21.        
  22.         # Rectangle properties
  23.         self.rect = pygame.Rect(x, y, width, height)
  24.        
  25.         # Fill color - either the passed parameter or the overall background color
  26.         self.fill_color = color
  27.  
  28.     # Definition - Text settings
  29.  
  30.     def set_text(self, text, fsize=12, text_color=BLACK):
  31.         self.text = text
  32.         self.image = pygame.font.SysFont('Montserrat', fsize).render(text, True, text_color)
  33.      
  34.     # Draw a rectangle with text
  35.    
  36.     def draw(self, shift_x=0, shift_y=0):
  37.         pygame.draw.rect(mw, self.fill_color, self.rect)
  38.         mw.blit(self.image, (self.rect.x + shift_x, self.rect.y + shift_y))  
  39.  
  40. # Definitions are done, now we will create the cards!
  41. # TextArea needs (x, y, width, height, colour)
  42.  
  43. quest_card = TextArea(120, 100, 290, 70, LIGHT_BLUE)
  44. quest_card.set_text("Question", 75)
  45.  
  46. ans_card = TextArea(120, 240, 290, 70, LIGHT_BLUE)
  47. ans_card.set_text("Answer", 75)
  48.  
  49. # Move these two commands out of the game loop
  50.  
  51. quest_card.draw(10,10)  # Question text alignment
  52. ans_card.draw(10,10)    # Answer test alignment
  53.  
  54. # Update the game loop to process Events - Q and A pressed on keyboard
  55. # You may clear the old loop and copy this version!
  56.  
  57. while True:
  58.     pygame.display.update()
  59.     for event in pygame.event.get():
  60.         if event.type == pygame.KEYDOWN:
  61.  
  62.             if event.key == pygame.K_q:
  63.                 num = randint(1,3)
  64.                 if num == 1:
  65.                     quest_card.set_text('What do you study at Algorithmics?', 24)
  66.                 if num == 2:
  67.                     quest_card.set_text('What language is spoken in Brazil?', 24)
  68.                 if num == 3:
  69.                     quest_card.set_text('What grows on an apple tree?', 24)      
  70.  
  71.                 quest_card.draw(10, 25)
  72.  
  73.             if event.key == pygame.K_a:
  74.                 num = randint(1,3)
  75.                 if num == 1:
  76.                     ans_card.set_text('Python', 35)
  77.                 if num == 2:
  78.                     ans_card.set_text('Portuguese', 35)
  79.                 if num == 3:
  80.                     ans_card.set_text('Apples', 35)      
  81.  
  82.                 ans_card.draw(10, 25)
  83.     clock.tick(20)          
  84.  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement