Как заполнить поле плитками (спрайты) чтобы заполнить всё пространство?
Мне надо, чтобы поле игры (800 на 600) было заполнено спрайтами (48 на 48) без пропусков и та плитка, на которую наведена мышь имела белую окантовку. (не 2D майнкрафт!) Код который я писал приведён ниже. (можете заменить наложение текстур на рисовку квадрата.)
Кста, о функции Render_texture, я делал свою библиотечку, не удивляйтесь. Render_texture(текстура, X, Y, rect от пайгейм, название окна из(pygame.display.set_mode))
try:
import pygame
except ImportError:
import os
os.system("pip install pygame")
import pygame
import random
from Lib import Render_texture
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_caption('My game')
FPS = 60
clock = pygame.time.Clock()
texture = pygame.image.load('Textures/EXPbattery_mine.png')
texture2 = pygame.image.load('Textures/Copper_block_mine.png')
texture4 = pygame.image.load('Textures/DirtTitle_mine.png')
texture5 = pygame.image.load('Textures/Iron_ore_mine.png')
block = 48
rect = pygame.Rect(48, 48, 48, 48)
blocks = pygame.sprite.Group()
background = 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():
stx8 = round(random.randrange(0 + block, dis_width - block * 2) / float(block)) * float(block)
sty8 = round(random.randrange(0 + block, dis_height - block * 2) / float(block)) * float(block)
return stx8, sty8
class background_tile(pygame.sprite.Sprite):
def __init__(self, X, Y):
pygame.sprite.Sprite.__init__(self)
self.image = texture4.convert_alpha()
self.rect = self.image.get_rect(center=(X + 24, Y + 24))
self.X = X
self.Y = Y
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():
k234 = 0
k235 = 0
slist = []
for u in range(0, 7):
f, f2 = rnd()
ores = ore_deposit(f, f2)
slist.append(ores)
blocks.add(ores)
slist2 = []
for u in range(0, 8):
f = k234
f2 = k235
k234 += 48
if k234 >= 816:
k234 = 0
k235 += 48
if k235 >= 624:
break
oresg = background_tile(f, f2)
slist2.append(oresg)
background.add(oresg)
game = True
while game:
for event in pygame.event.get(): # события и управление
if event.type == pygame.QUIT:
game = False
dis.fill(gray)
background.draw(dis)
blocks.draw(dis)
spd = pygame.mouse.get_pos()
for i in range(0, len(blocks)):
if (coll(spd, slist[i])) == 1:
Render_texture(texture2, (slist[i]).X, (slist[i]).Y, rect, dis)
for j in range(0, len(background)):
if (coll(spd, slist2[j])) == 1:
Render_texture(texture5, (slist2[i]).X, (slist2[i]).Y, rect, dis)
pygame.display.update()
pygame.display.flip()
clock.tick(FPS)
gameloop()
Ответы (1 шт):
Автор решения: МихаилПитон
→ Ссылка
Не знаю почему, но теперь заработало.
Вот код после проблемы (его я уже улучшил):
import sys
try:
import pygame
except ImportError:
import os
os.system("pip install pygame")
import pygame
import random
from IronForge import Render_texture
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')
FPS = 60
k = 0
clock = pygame.time.Clock()
sound_click_1 = pygame.mixer.Sound('sound/click/standart_ckick.mp3')
sound_click_2 = pygame.mixer.Sound('sound/click/dolgiy_click.mp3')
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')
texture_frame = pygame.image.load('Textures/Item_frame_mine.png')
block = 48
rect = pygame.Rect(48, 48, 48, 48) # ore deposit
blocks = pygame.sprite.Group()
background = pygame.sprite.Group()
def coll(spd, ore, sound=sound_click_2):
global k
if spd[0] > ore.X and spd[0] < ore.X + block and spd[1] > ore.Y and spd[1] < ore.Y + block:
sop = pygame.mouse.get_pressed()
if sop[0] == True:
if k != 1:
sound.play()
k = 1
elif sop[0] == False:
k = 0
return 1
def rnd():
stx8 = round(random.randrange(0 + block, dis_width - block * 2) / float(block)) * float(block)
sty8 = round(random.randrange(0 + block, dis_height - block * 2) / float(block)) * float(block)
return stx8, sty8
class background_tile(pygame.sprite.Sprite):
def __init__(self, X, Y):
pygame.sprite.Sprite.__init__(self)
self.image = texture4.convert_alpha()
self.rect = self.image.get_rect(center=(X + 24, Y + 24))
self.X = X
self.Y = Y
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():
stop = False
k234 = 0
k235 = 0
slist = []
for u in range(0, 7):
f, f2 = rnd()
ores = ore_deposit(f, f2)
slist.append(ores)
blocks.add(ores)
slist2 = []
for u in range(0, 232):
f = k234
f2 = k235
k234 += 48
if k234 >= 816:
k234 = 0
k235 += 48
if k235 >= 624:
break
oresg = background_tile(f, f2)
slist2.append(oresg)
background.add(oresg)
game = True
while game:
while stop:
for event in pygame.event.get(): # события и управление
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
stop = False
for event in pygame.event.get(): # события и управление
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
stop = True
dis.fill(gray)
background.draw(dis)
blocks.draw(dis)
spd = pygame.mouse.get_pos()
for i in range(0, len(blocks)):
if (coll(spd, slist[i])) == 1:
Render_texture(texture_frame, (slist[i]).X, (slist[i]).Y, rect, dis)
for j in range(0, len(background)):
if (coll(spd, slist2[j], sound_click_1)) == 1:
Render_texture(texture_frame, (slist2[j]).X, (slist2[j]).Y, rect, dis)
pygame.display.update()
pygame.display.flip()
clock.tick(FPS)
gameloop()