Python Forum
[PyGame] Screen is all black and nothing is updating, but I can't figure out why.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Screen is all black and nothing is updating, but I can't figure out why.
#10
(Oct-23-2020, 12:13 AM)SheeppOSU Wrote: The main problem I'm having is my pygame window being completely black when I run it, but running just fine when I use an online source for running pygame.
Im not sure what you mean. When i run your code i see the button. I do see that when you resize the screen you have not accounted for aspect ratio.
I would look at @Mekire repos for an example of retaining aspect ratio. You have to constantly update the screen's dimensions. The best way to do this is the create a screen pygame rect that is updated with the screens dimensions. Then apply all objects within that rect. Its been a long time since i did pygame so i could not make a specific example anymore.

Also can you please make a runnable example next time so people can copy and paste in one shot to run your code instead of piecing it together. Ill do that here.

import pygame
import sys
#from PygameFuncs import Button, message_display
from functools import partial
 
settings = {
    'fps': 60,
    'caption': "PyLine",
}
 
H = 600
W = 800

def text_objects(text, font, color):
    textSurface = font.render(text, True, color)
    return textSurface, textSurface.get_rect()

class Button:
    def __init__(self, **kwargs):
        self.rect = pygame.Rect(kwargs.get('rect', [0, 0, 100, 100]))
        self.font = pygame.font.SysFont(kwargs.get('textfont', 'arial'), kwargs.get('textsize', 10))
        self.color = kwargs.get('color', [255, 255, 255])
        self.hoverColor = kwargs.get('hoverColor', [200, 200, 200])
        self.textSurf, self.textRect = text_objects(kwargs.get("text", "Hello World"),
                                                    self.font, kwargs.get('textcolor', [0, 0, 0]))
        self.textRect.center = ((self.rect[0] + self.rect[2]/2, self.rect[1] + self.rect[3]/2),)
 
    def resize(self, rect):
        self.rect = pygame.Rect(rect)
         
    def draw(self, screen, hover=False):
        color = self.color
        if hover:
            color = self.hoverColor
        pygame.draw.rect(screen, color, self.rect)
        screen.blit(self.textSurf, self.textRect)
 
    def run(self, screen, *args):
        mouse = pygame.mouse.get_pressed()
        if self.rect.collidepoint(pygame.mouse.get_pos()) and mouse[0]:
            self.draw(screen, True)
            for command in args:
                command()
            return True
        return False
 
 
class StartScreen:
    def __init__(self, controller):
        self.colors = {
            'background': (255, 255, 255),
        }
        self.objects = {
            Button(rect=[W*0.5-(W*3/16), H*0.5-(H/6), W*3/8, H/3], color=[0, 0, 255], hoverColor=[0, 0, 200],
                   textsize=15, text="Start", textcolor=[0, 0, 0]):
            {
                'scales': [0.5, 0.5, 3 / 8, 1 / 3],
                'offsets': [-W*3/16, -H/6, 0, 0],
                'finish_funcs': [],
            },
        }
 
    def resize(self, screen):
        for obj, info in self.objects.items():
            w = screen.get_width()
            h = screen.get_height()
            scales = info['scales']
            offsets = info['offsets']
            obj.resize([w*scales[0]+offsets[0], h*scales[1]+offsets[1],
                        w*scales[2]+offsets[2], h*scales[3]+offsets[3]])
 
    def draw(self, screen):
        screen.fill(self.colors['background'])
        for obj in self.objects.keys():
            obj.draw(screen)
 
    def run(self, controller):
        for obj, info in self.objects.items():
            obj.run(controller.screen, *info['finish_funcs'])
 
 
class Controller:
    def __init__(self, settings, startingWindow):
        pygame.init()
        self.screen = pygame.display.set_mode((W, H), pygame.RESIZABLE)
        pygame.display.set_caption(settings['caption'])
 
        self.currentWindow = startingWindow
        self.running = False
        self.clock = pygame.time.Clock()
 
        self.settings = settings
 
    def new_window(self, window):
        self.running = False
        self.currentWindow = window
        self.run()
 
    def run(self):
        self.running = True
        self.currentWindow = self.currentWindow(self)
        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
                    break
                if event.type == pygame.VIDEORESIZE:
                    self.screen = pygame.transform.scale(self.screen, (event.w, event.h))
                    self.currentWindow.resize(self.screen)
            self.currentWindow.draw(self.screen)
            self.currentWindow.run(self)
            pygame.display.update()
            self.clock.tick(self.settings['fps'])
 
 
if __name__ == '__main__':
    app = Controller(settings, StartScreen)
    try:
        app.run()
    except KeyboardInterrupt:
        print("KeyboardInterrupt")
    pygame.quit()
    sys.exit()
else:
    print("This file must be run directly for the game to display and work correctly.")
Recommended Tutorials:
Reply


Messages In This Thread
RE: Screen is all black and nothing is updating, but I can't figure out why. - by metulburr - Oct-23-2020, 11:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  what's not working? black window codeweak 3 928 Dec-07-2023, 02:32 AM
Last Post: Benixon
  [PyGame] When I hit the space bar from the home screen, it sends me to the game over screen JesusisKing 1 1,081 Apr-30-2023, 10:11 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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