-2

The following question is from chapter 12 of the book: "Python Crash Course".

I need to write a class that draws a *.bmp image at the center of the screen.

I had a look here and here, which is a very specific case.

My class, which didn't raise much success, is:

import pygame, sys

class Lion():

    def __init__(self, screen):
        self.screen = screen

        self.screen_width = 1000
        self.screen_height = 500
        self.bg_color = (230, 230, 230)

        self.image = pygame.image.load('C:/Users/yoelp/Desktop/Python Works/lion.bmp') 

        self.rect = self.image.get_rect()

        self.rect.centerx = self.screen_rect.centerx
        self.rect.centery = self.screen_rect.centery

        # Creating an instance of Lion. 
        lion = Lion(screen)

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            self.screen.blit(self.image, self.rect)
            pygame.display.flip()

Any ideas?

Thank you.

Yoel Zajac
  • 453
  • 1
  • 6
  • 11
  • 3
    how do you use it ? Do you get error message ? Always put full error message in question (not comment) as text (not image) – furas Feb 18 '20 at 17:18
  • 1
    why do you create `lion = Lion(screen)` inside `Lion.__init__()` ? It is wrong. OR maybe you have wrong indentations and ``lion = Lion(screen)`` should be outside `class`. Indentations are very important in Python. – furas Feb 18 '20 at 17:19
  • where is `pygame.init()`? Where do you create `screen`? You don't have it. – furas Feb 18 '20 at 17:22
  • I don't get any error messages. It just writes me "Hello from the PyGame community." – Yoel Zajac Feb 18 '20 at 17:25

1 Answers1

2
  • you don't have pygame.init()
  • you don't create screen but your class get screen as argument
  • you create instance of Lion() inside Lion.__init__() which is big mistake

I would write it this way.

import pygame
import sys

# --- constants --- (UPPER_CASE_NAMES)

SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 500

# --- classes --- (CamelCaseNames)

class Lion():

    def __init__(self, screen):

        # get screen
        self.screen = screen

        # get rect from screen
        self.screen_rect = self.screen.get_rect()

        self.bg_color = (230, 230, 230)

        self.image = pygame.image.load('C:/Users/yoelp/Desktop/Python Works/lion.bmp') 

        self.rect = self.image.get_rect()

        self.rect.centerx = self.screen_rect.centerx
        self.rect.centery = self.screen_rect.centery

    def run(self):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()  # close Pygame's window on some systems
                    sys.exit()

            self.screen.blit(self.image, self.rect)
            pygame.display.flip()

# --- functions --- (lower_case_names)

# empty

# --- main ---

pygame.init()

# create screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# send screen to class instance
lion = Lion(screen)
lion.run()

#pygame.quit()

If you want all in one method in class then

import pygame
import sys

# --- classes ---

class Lion():

    def __init__(self): # without `screen` as argument

        pygame.init()

        # create screen
        self.screen = pygame.display.set_mode((1000, 500))

        # get rect from screen
        self.screen_rect = self.screen.get_rect()

        self.bg_color = (230, 230, 230)

        self.image = pygame.image.load('C:/Users/yoelp/Desktop/Python Works/lion.bmp') 

        self.rect = self.image.get_rect()

        self.rect.centerx = self.screen_rect.centerx
        self.rect.centery = self.screen_rect.centery

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

            self.screen.blit(self.image, self.rect)
            pygame.display.flip()

# --- main ---

lion = Lion() # without `screen` as argument
furas
  • 134,197
  • 12
  • 106
  • 148
  • 1
    I added version with all elements inside `__init__` – furas Feb 18 '20 at 17:36
  • Thank you @furas . – Yoel Zajac Feb 18 '20 at 17:37
  • If I have another question regarding this question, where can I write it? – Yoel Zajac Feb 23 '20 at 08:08
  • Here or in another section? – Yoel Zajac Feb 23 '20 at 08:13
  • don't change question on current page. Create new question on new page - you will have more space for code and description. And new people will see it - they may have also answers for your question. – furas Feb 23 '20 at 13:24
  • How can I know whether to write "screen" in __init__ or not? What is the meaning in "creating" a screen? Thanks. – Yoel Zajac Feb 25 '20 at 08:04
  • all depend what you keep in class - if it would be `Player()`, `Enemy()`, `Obstacle()` then I would create `screen` outside classes and send it to classes as argument - this way I create one `screen` and all classes use the same `screen` to draw object. But If it would be class `MyApp()` , `MyGame()` which keeps all information, runs mainloop, create instances of `Player()`, `Enemy()`, `Obstacle()` then I would create it inside this class (to have all code inside classes) – furas Feb 25 '20 at 10:53
  • `screen` is [pygame.surface.Surface()](https://www.pygame.org/docs/ref/surface.html) on which you draw all elements and later you send this surface to Video Card (`pygame.display.flip`) which displays it on monitor. This way you don't draw directly on monitor but in buffer (surface) and send it to VIdeo Card only when all elemens are on surface - it prevents the screen blinking. See [double buffering](https://en.wikipedia.org/wiki/Multiple_buffering#Double_buffering_in_computer_graphics) and [screen flickering](https://en.wikipedia.org/wiki/Flicker_(screen)) – furas Feb 25 '20 at 10:58