Как обратится к pygame Group?
Вот проба кода:
try:
import pygame
except ImportError:
import os
os.system("pip install pygame")
import pygame
import random
pygame.init()
white = (254, 254, 254) # цвета тут --> COLORS <--
yellow = (253, 255, 102)
black = (1, 1, 1)
gray = (120, 120, 120)
dark = (100, 100, 100)
brown = (109, 100, 90)
red = (252, 39, 39)
green = (10, 180, 10)
lightblue = (80, 150, 255)
blue = (53, 125, 231)
orange = (255, 100, 130)
dis_width = 800
dis_height = 600
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_icon(pygame.image.load("Textures/Logo.ico"))
pygame.display.set_caption('Mine game')
dis.fill(gray)
clock = pygame.time.Clock()
texture = pygame.image.load('Textures/EXPbattery_mine.png')
texture2 = pygame.image.load('Textures/Copper_block_mine.png')
texture3 = pygame.image.load('Textures/Copper_ore_mine.png')
texture4 = pygame.image.load('Textures/DirtTitle_mine.png')
texture5 = pygame.image.load('Textures/Iron_ore_mine.png')
texture6 = pygame.image.load('Textures/Leadmetall_mine.png')
texture7 = pygame.image.load('Textures/Minerr_mine.png')
block = 48
rect = pygame.Rect(48, 48, 48, 48) # ore deposit
blocks = pygame.sprite.Group()
def coll(spd, ore):
if spd[0] > ore.X and spd[0] < ore.X + block and spd[1] > ore.Y and spd[1] < ore.Y + block:
return 1
def rnd():
f = round(random.randrange(block, dis_height - block * 2) / 10.0) * 10.0
return f
class ore_deposit(pygame.sprite.Sprite):
def __init__(self, X, Y):
pygame.sprite.Sprite.__init__(self)
self.image = texture.convert_alpha()
self.rect = self.image.get_rect(center=(X + 24, Y + 24))
self.X = X
self.Y = Y
def gameloop():
ore = ore_deposit(rnd(), rnd())
blocks.add(ore_deposit(rnd(), rnd()), ore_deposit(rnd(), rnd()))
game = True
while game:
for event in pygame.event.get(): # события и управление
if event.type == pygame.QUIT:
game = False
dis.fill(gray)
blocks.draw(dis)
spd = pygame.mouse.get_pos()
for i in range(0, len(blocks)): # как сделать, чтобы он проверял каждый
if (coll(spd, blocks)) == 1: # элемент из группы blocks?
dis.fill(red)
pygame.display.update()
pygame.display.flip()
clock.tick(60)
gameloop()