在pygame上绘制对象 [英] drawing objects on pygame

查看:89
本文介绍了在pygame上绘制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我无法在当前代码中使用碰撞功能,因此我正在重新启动一些代码以进行covid模拟.我已经能够画出基本的背景,并画出一个单元格.但是,当我尝试在屏幕上的其他位置创建单元格时,由于某种原因,它不会出现.

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.

我的代码如下所示:

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

推荐答案

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

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.

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

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.

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

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

最小的工作示例,它可以随机移动5个单元格.

Minimal working example which move 5 cells in random directions.

我以不同的方式组织代码,并尝试使用 PEP 8-样式指南Python代码

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

这篇关于在pygame上绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆