在 pygame 上绘制对象

drawing objects on pygame

我正在重新启动 covid 模拟的一些代码,因为我不能在我当前的代码中使用碰撞功能。我已经能够绘制基本背景,并绘制一个单元格。但是,当我尝试在屏幕上的不同位置创建单元格时,由于某种原因它没有出现。

我的代码如下:

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)

提前感谢您的帮助

主要问题是您的单元格大小为 (50,50),您尝试在位置 (20,70) 上绘制,因此它绘制在矩形 (50, 50) 之外,您看不到它。您必须在矩形 (50, 50) 内部绘制 - 例如在中心 (25,25)。稍后你应该使用 self.rect 在屏幕上移动它。

第二个问题是你保持在 self.x_coord, self.x_coord 的位置但是你应该使用 self.rect.x self.rect.y 因为 Sprite 使用 self.image self.rect 将其绘制在屏幕上。

它显示了第三个问题 - 在 Cell 中,您需要方法 update,它将更改 self.rect 中的值以移动对象。


在随机方向移动 5 个单元格的最小工作示例。

我以不同的方式组织代码并尝试使用 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