Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame importing classes
#1
I am creating a quiz program in pygame, I have currently created my main menu which stores my background, buttons and mouse interaction in one class. However, am now attempting to create the quiz starting with maths.

I have successfully imported the class into another file, however, I cannot change the text in those buttons in the new file as these buttons will now hold my answers.and it does not appear to run any code after I import the class into the new file.

[1]: https://i.stack.imgur.com/3jd4h.png # This is the main class in my Main menu file

[1]: https://i.stack.imgur.com/kb5zP.png /This is what my tutor suggested but it has not worked and the text within the button remains the same.

Any help is appreciated
Reply
#2
Please, don't post images. Copy/paste your code, full traceback, etc. in the respective tags
Please, read What NOT to include in a post
Reply
#3
Here is the code for my class in the Quiz.py file, apologies for posting images
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 clicked(self):
print("test")

def checkClicked(self):
#click = pygame.mouse.get_pressed()p
x, y = 0,1
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__()

I here is the code in my Mathsquiz.py file

import pygame
import random
print ("hello?@")
import Quiz.py
pygame.init()
print ("hello?@")
mathsButton.text = "answer 1"

The problem is it currently prints the first hello but not the second after the file is called, also I cannot change the button text from the class button as shown above in the button class in my new file.

Here is the traceback, however, there is no error it just prints hello which is the problem in its self as it should print twice.
I also don't understand what you mean back proper tags as I don't understand what the page you linked is asking for in that regard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Importing classes? frequency 11 4,705 Dec-31-2018, 07:21 PM
Last Post: ichabod801
  IDLE not importing pygame (ok outside of IDLE) miner_tom 1 3,280 Sep-14-2018, 07:54 PM
Last Post: Larz60+
  Problems with importing classes in different folder Xeraphim 3 3,339 Nov-08-2017, 03:20 PM
Last Post: Larz60+
  Using classes? Can I just use classes to structure code? muteboy 5 4,978 Nov-01-2017, 04:20 PM
Last Post: metulburr
  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