Python Forum
class has no idea what i am talking about, and i have no idea how to give it the info
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class has no idea what i am talking about, and i have no idea how to give it the info
#1
i have two py files. her they are:

import pygame


class button:
    def __init__(self):
        pygame.draw.rect(screen, 10, 10, 10, 10)
        print("fuckyouuuu")

    def __call__(self):
        print("FHDFDEDEDDDFDFDF")
import Button
import pygame
import sys
from pygame.locals import *
x = 0
y = 30
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)


pygame.init()
infoObject = pygame.display.Info()
correctedscreenheight = (infoObject.current_h - 30)
screen = pygame.display.set_mode((infoObject.current_w, correctedscreenheight))
pygame.display.set_caption('Dnd reimagined')
mainscreen = pygame.image.load("dnd.png").convert()
WHITE = (25 , 25, 25)
fuckoff = Button.button()



while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
    mainscreen = pygame.transform.scale(mainscreen, (infoObject.current_w, infoObject.current_h))
    screen.blit(mainscreen, (0, 0))


    pygame.display.update()
the button file obviously does not know what i am talking about when i tell it to draw a rectangle to screen, because screen is defined elsewhere. i have no idea how to fix this, it seems like i am not allowed to have any other classes if i am using a screen, which i desperately hope is not correct. Please help me figure this out, powers that be, before i put my head through a wall.
Reply
#2
I am a noob and don't know what I am talking about but this is what I would try, and what I have learned along my learning route.

Define screen locally and see if you can get it to work, you may need to comment it out in the code block that we cannot see right now as you may get an error for def it two times. Also I have learned as a beginner to test my code very often before I type too many code blocks making it too difficult to find my error.

Here on the forum please use python tags around your code so proper indentations will give others a chance to see your code blocks as actually intended, indentations for beginners is a problem so they need to be properly formatted here in your post.

python within square brackets
PUT ALL YOUR CODE HERE
/python within square brackets
Reply
#3
(Aug-15-2018, 03:40 PM)TheNumericDolfin Wrote: the button file obviously does not know what i am talking about when i tell it to draw a rectangle to screen, because screen is defined elsewhere. i have no idea how to fix this
fuckoff = Button.button(screen)
You need to pass the screen to your button class as well as add that parameter in your button class dunder init method __init__

Here is a button class example in our tutorials
https://python-forum.io/Thread-PyGame-User-Interface
In this case the screen is passed to the buttons draw method in the main game loop
Quote:
    btn.draw(screen)
Quote:
class Button:
...
    def draw(self, surf):
        surf.blit(self.image, self.rect)
Recommended Tutorials:
Reply
#4
Thank you, you are basically python Jesus. Worked flawlessly.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have an idea and don't know where to start... Bubba123 1 1,507 Jul-25-2020, 06:47 AM
Last Post: Windspar

Forum Jump:

User Panel Messages

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