Отображение объекта на игровых областях
Я новичок в Pygame и столкнулась с такой проблемой:
import pygame
pygame.init()
'''Создание окна'''
window = pygame.display.set_mode((400, 400))
'''Создание холста'''
screen = pygame.Surface((400, 400)) # создаем игровую область
'''Строка состояния'''
info_string = pygame.Surface((400, 30))
string_zet = pygame.Surface((390, 50))
string_hero = pygame.Surface((390, 175))
class Sprite:
def __init__(self, xpos, ypos, filename):
self.x = xpos
self.y = ypos
self.bitmap = pygame.image.load(filename)
def render(self):
screen.blit(self.bitmap, (self.x, self.y))
def render_z(self):
string_zet.blit(self.bitmap, (self.x, self.y))
def render_h(self):
string_hero.blit(self.bitmap, (self.x, self.y))
'''Столкновение стрелы и цели'''
def Interesct(x1, x2, y1, y2, db1, db2):
if ((x1 > x2 - db1) and (x1 < x2 + db2) and
(y1 > y2 - db1) and (y1 < y2 + db2)):
return 1
else:
return 0
'''Штифты'''
pygame.font.init()
font = pygame.font.SysFont('Comic Sans MS', 24, True)
font2 = pygame.font.SysFont('Comic Sans MS', 24, True)
font3 = pygame.font.SysFont('Berlin Sans FB Demi', 34, True)
'''Описание героя'''
hero = Sprite(100, 125, 'D:/Python/Game/Платформер/Sprait.png')
# hero.up = True
'''Описане цели'''
zet = Sprite(10, 0, 'D:/Python/Game/Платформер/png.png')
zet.right = False
zet.step = 1
'''Описание стрелы'''
strela = Sprite(-10, 125, 'D:/Python/Game/Платформер/Iron.png')
strela.push = False
run = True
pygame.key.set_repeat(1, 1)
enumerator = 0
arrow_colour = 254
while run:
'''Обработка событий'''
for e in pygame.event.get():
if e.type == pygame.QUIT:
run = False
'''Событие - нажатие клавиш'''
if e.type == pygame.KEYDOWN:
'''Перемещение героя '''
if e.key == pygame.K_LEFT:
if hero.x > 0:
hero.x -= 1
if e.key == pygame.K_RIGHT:
if hero.x < 345:
hero.x += 1
if e.key == pygame.K_DOWN:
if hero.y < 125:
hero.y += 1
if e.key == pygame.K_UP:
if hero.y > 0:
hero.y -= 1
'''Запуск стрелы'''
if e.key == pygame.K_SPACE:
if strela.push == False:
strela.x = hero.x + 25
strela.y = hero.y
strela.push = True
'''Событие - пережвижение мыши'''
if e.type == pygame.MOUSEMOTION:
pygame.mouse.set_visible(False)
n = pygame.mouse.get_pos()
if n[0] > 0 and n[0] < 345:
hero.x = n[0]
if n[1] > 0 and n[1] < 125:
hero.y = n[1]
'''Событие - нажатие кнопок мыши'''
if e.type == pygame.MOUSEBUTTONDOWN:
if e.button == 1:
if strela.push == False:
strela.x = hero.x + 25
strela.y = hero.y
strela.push = True
"""Заливка"""
screen.fill((50, 50, 50))
info_string.fill((45, 80, 60))
string_zet.fill((128, 128, 128))
string_hero.fill((128, 128, 128))
'''Изменение цвета надписи'''
arrow_colour += 1
if arrow_colour == 255:
arrow_colour = 100
"""Передвижение цели"""
if zet.right == True:
zet.x -= zet.step
if zet.x <= 0:
zet.right = False
else:
zet.x += zet.step
if zet.x >= 350:
zet.right = True
'''Перемещение стрелы'''
if strela.y < 0:
strela.push = False
enumerator -= 1
if strela.push == False:
strela.x = -10
strela.y = 350
else:
strela.y -= 1
'''Столкновение стрелы и цели'''
if Interesct(strela.x, zet.x, strela.y, zet.y, 15, 50) == True:
strela.push = False
zet.step += 0.2
enumerator += 1
# if Interesct(zet.x, hero.x, zet.y, hero.y) == True:
# hero.up = False
# zet.up = True
"""Отрисовка объектов"""
strela.render()
strela.render_h()
strela.render_z()
hero.render_h()
zet.render_z()
'''Отображение штифта на экран'''
info_string.blit(font.render('Speed: ' + str(zet.step), 1, (210, 120, 200)), (255, -2))
info_string.blit(font2.render('Score: ' + str(enumerator), 1, (210, 120, 200)), (10, -2))
info_string.blit(font.render('ARROW', 0, (arrow_colour, 97, 27)), (140, 0))
'''Отображение холста на экран и тд.'''
window.blit(screen, (0, 0))
window.blit(info_string, (0, 0))
window.blit(string_zet, (5, 35))
window.blit(string_hero, (5, 220))
pygame.display.flip() # обновление событий на экране
pygame.time.delay(5) # скорость цикла
Вверху полностью код моей игры. Меня интересует блок кода, где нужно отображать выпущенную стрелу.
'''Перемещение стрелы'''
if strela.y < 0:
strela.push = False
enumerator -= 1
if strela.push == False:
strela.x = -10
strela.y = 350
else:
strela.y -= 1
Я пробовала отобразить ее во всех игровых областях, но она отображалась одновременно во всех. Оно и понятно.
"""Отрисовка объектов"""
strela.render()
strela.render_h()
strela.render_z()
Я думаю, что нужно сравнивать ее положение относительно каждой из областей, но у меня их несколько, поэтому я не знаю как это сделать. Там так же есть блок столкновения стрелы и цели(там идет сравнение координат) и я теперь не знаю, как он будет действовать, а точнее как сравнивать координаты.