Search code examples
pythonpygamedrawingsimulation

drawing objects on pygame


I am restarting some code for a covid simulation as I cant use the collide function in my current one. I have been able to draw the basic background, and draw one cell. However, when i try create the cell in a different place on my screen it does not appear for some reason.

My code is as seen below:

import random
import pygame
# import numpy
import time

pygame.init()

GREEN1 = (0, 255, 0)  # Healthy cells
RED = (255, 0, 0)  # Infected cells
GREEN2 = (0, 100, 0)  # Healthy cells not susecptible
BLACK = (0, 0, 0)  # Dead cells
WHITE = (255, 255, 255)
Bgcolor = (225, 198, 153)

ScreenSize = (800, 800)
Screen = pygame.display.set_mode(ScreenSize)
pygame.display.set_caption("Covid-19 Simualtion")

clock = pygame.time.Clock()

speed = [0.5, -0.5]


class Cells(pygame.sprite.Sprite):
    def __init__(self, color, speed, width, height):
        super().__init__()
        self.color = color
        self.x_cord = random.randint(0, 400)
        self.y_cord = random.randint(50, 700)
        self.radius = 5
        self.speed = speed
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)
        pygame.draw.circle(self.image, self.color, [30, 70], self.radius, width = 0)


        self.rect = self.image.get_rect()
        self.radius = 5

        #x_number = random.randint(0, 1)
        #self.xSpeed = speed[x_number]
        #y_number = random.randint(0, 1)
        #self.ySpeed = speed[y_number]


allCellsList = pygame.sprite.Group()
Cell1 = Cells(GREEN1, 5, 50, 50)
allCellsList.add(Cell1)

End = False
while not End:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            End = True

    Screen.fill(Bgcolor)
    pygame.draw.rect(Screen, BLACK, (0, 50, 400, 700), 3)
    allCellsList.update()

    allCellsList.draw(Screen)

    pygame.display.flip()
    clock.tick(60)

Thanks for any help in advance


Solution

  • The main problem is that your cell has size (50,50) and you try to draw on position (20,70) so it draws outside rectangle (50, 50) and you can't see it. You have to draw inside rectangle (50, 50) - for example in center (25,25). And later you should use self.rect to move it on screen.

    Second problem is that you keep position in self.x_coord, self.x_coord but you should use self.rect.x self.rect.y because Sprite use self.image and self.rect to draw it on screen.

    And it show third problem - in Cell you need method update which will change values in self.rect to move object.


    Minimal working example which move 5 cells in random directions.

    I organize code in different way and try to use PEP 8 - Style Guide for Python Code

    import random
    import pygame
    
    # --- constants --- (UPPER_CASE_NAMES)
    
    GREEN1 = (0, 255, 0)  # Healthy cells
    RED = (255, 0, 0)  # Infected cells
    GREEN2 = (0, 100, 0)  # Healthy cells not susecptible
    BLACK = (0, 0, 0)  # Dead cells
    WHITE = (255, 255, 255)
    
    BACKGROUND_COLOR = (225, 198, 153)
    
    SCREEN_SIZE = (800, 800)
    
    # --- classes --- (CamelCaseNames)
    
    # class keeep only one cell so it should has name `Cell` instead of `Cells`
    
    class Cell(pygame.sprite.Sprite):
    
        def __init__(self, color, speed, width, height):
            super().__init__()
            
            self.color = color
            self.speed = speed
            
            self.image = pygame.Surface([width, height])
            self.image.fill(WHITE)
            self.image.set_colorkey(WHITE)
            
            self.radius = width//2 # 25 
            center = [width//2, height//2]
            pygame.draw.circle(self.image, self.color, center, self.radius, width=0)
    
            self.rect = self.image.get_rect()
            self.rect.x = random.randint(0, 400)
            self.rect.y = random.randint(50, 700)
    
        def update(self):
            self.rect.x += random.randint(-10, 10)
            self.rect.y += random.randint(-10, 10)
    
    # --- functions --- (lower_case_names)
    
    # empty
    
    # --- main --- (lower_case_names)
    
    pygame.init()
    
    screen = pygame.display.set_mode(SCREEN_SIZE)
    pygame.display.set_caption("Covid-19 Simualtion")
    
    speed = [0.5, -0.5]
    
    # - objects - 
    
    all_cells = pygame.sprite.Group()  # PEP8: lower_case_name
    
    for _ in range(5):
        cell = Cell(GREEN1, 5, 50, 50)     # PEP8: lower_case_name
        all_cells.add(cell)
    
    # - loop -
    
    clock = pygame.time.Clock()
    
    end = False   
    while not end:
    
        # - events -
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                end = True
    
        # - upadates (without draws) -
        
        all_cells.update()
    
        # - draws (without updates) -
            
        screen.fill(BACKGROUND_COLOR)
        pygame.draw.rect(screen, BLACK, (0, 50, 400, 700), 3)
        
        all_cells.draw(screen)
    
        pygame.display.flip()
        clock.tick(30)  # to use less CPU
    
    # - end
    
    pygame.quit()  # some system may need it to close window