Python Forum
Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame Button interaction
#1
Hi Im creating a quiz based game, I currently have a main menu form called Quiz.py as shown below in the code, this is simply to build my main menu. This is then imported into another file called Maths.py wich is currently one of the 3 tests i will be building.

My questions is, how can I code the buttons in my Maths.py file so that when the maths button is clicked by the user they start a cycle of randomly generated maths questions, in which they overwrite the current contents of my 3 buttons with answers.

Here is the code in my quiz file

import random
import time # these are importing everything I will need for my program.
import pygame
pygame.init() # Initiates pygame so it runs.
display_width = 1600 # This sets the boundries of the pygame screen
display_height = 800
gamedisplay = pygame.display.set_mode((display_width,display_height))
white = (255,255,255)
black = (000,000,000)
grey = (169,169,169)
clock = pygame.time.Clock()
buttonheight = 100
buttonwidth = 250
class Button:
x=0
y=0
height=100
width=250

body = None
text="Maths"

def __init__(self):
self.body = pygame.Rect(self.x, self.y, self.width, self.height)

def setText(self, textOfButton):
self.text = textOfButton

def clicked(self):
print("test")

def checkClicked(self):
click = pygame.mouse.get_pressed()
x, y = 0,1
click = 0
if pygame.mouse.get_pos()[x] > self.x and pygame.mouse.get_pos()[x] < self.x + self.width:
if pygame.mouse.get_pos()[y] > self.y and pygame.mouse.get_pos()[y] < self.y + self.height:
self.clicked()

def draw(self):
smallText = pygame.font.Font ('RINGM___.ttf',20, )
TextSurf, TextRect = text_objectsW(self.text,smallText)
TextRect.center = ( (self.x+(self.width/2)), (self.y+(self.height/2)))

pygame.draw.rect(gamedisplay, black, self.body)
gamedisplay.blit(TextSurf, TextRect)

#gamedisplay.blit(smallText.render(self.text, False, white), TextRect)


class mathsButton(Button):
def __init__(self):
self.x = 95
self.y = 400
self.text = "Maths"
super().__init__()

class historyButton(Button):
def __init__(self):
self.x=690
self.y=400
self.text = "History"
super().__init__()


class englishButton (Button):
def __init__(self):
self.x=1255
self.y=400
self.text="English"
super().__init__()

def text_objectsB (text, font): # These 2 functions are the same with the colour of the text being the exception. its renders the text and its colour
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def text_objectsW (text, font):
textSurface = font.render(text, True, white)
return textSurface, textSurface.get_rect()

my_image = pygame.image.load('Background2.jpg').convert()
gamedisplay.blit(my_image, [0,0])
intro = True
btnMaths = mathsButton()
btnHistory = historyButton()
btnEnglish = englishButton()

def main_menu():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
btnMaths.checkClicked()
btnHistory.checkClicked()
btnEnglish.checkClicked()
largetext = pygame.font.Font('RINGM___.ttf',80)
TextSurf, TextRect = text_objectsB("Are you smarter than a 13 year old?", largetext)
TextRect.center = ((display_width/2),(display_height/6))
gamedisplay.blit(TextSurf, TextRect)
#button("Maths Quiz",95,400,black,grey,"Maths")
#button("English Quiz",675,400,black,grey,"English")
#button("History Quiz",1255,400,black,grey,"History")
btnMaths.draw()
btnHistory.draw()
btnEnglish.draw()
pygame.display.update()
clock.tick(60)
And here is the code for my Maths file

import pygame
import random, time
import Quiz
t0 = time.time()
while True:
Quiz.main_menu()
if time.time() - t0 > 5:
Quiz.btnMaths.setText("New Text") # This code is proof on concept that i can change the text in this box
Thanks for any help you can provide.
Reply
#2
There is an example of creating a button class here
https://python-forum.io/Thread-PyGame-User-Interface

The command for the button would just have to randomize a list of questions or something similar.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Odd Python / C Library interaction embedka 0 1,497 Oct-09-2019, 12:21 PM
Last Post: embedka
  RTL_Power interaction 25141928 0 1,672 Apr-08-2019, 07:26 PM
Last Post: 25141928
  Building a class for sqlite3 interaction Nuzvee 5 3,244 Jan-29-2019, 06:47 AM
Last Post: Nuzvee
  Interaction with games such as BO3 nmills688 1 2,694 Oct-16-2017, 08:58 PM
Last Post: nilamo
  Pygame*import pygame ImportError: No module named pygame CASPERHANISCH 1 9,707 Jun-05-2017, 09:50 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020