У меня ошибка в небольшом упражнении по пигме, где я пытаюсь построить настольную игру с шестигранным полем ...
Я пытался минимизировать код, но так как у меня нет абсолютно никакой подсказки,где что-то идет не так, все еще немного для того, чтобы работать:
import pygame, random, math, time, sys
# Important Game Vars - SCREEN and PLAYFIELD
width = 1024
length = 680
hexW = 50
hexH = 60
playFieldSize = 1
midpoint = [int(width / 2), int(length / 2)]
print('midpoint:', midpoint)
playFields = [midpoint]
holderList = []
for i in range(playFieldSize):
for currentpoint in playFields:
field = [currentpoint[0] + hexW, currentpoint[1]]
if field not in playFields and field not in holderList:
holderList.append(field)
field = [currentpoint[0] - hexW, currentpoint[1]]
if field not in playFields and field not in holderList:
holderList.append(field)
field = [currentpoint[0] - hexW / 2, currentpoint[1] + hexH * 0.75]
if field not in playFields and field not in holderList:
holderList.append(field)
field = [currentpoint[0] + hexW / 2, currentpoint[1] + hexH * 0.75]
if field not in playFields and field not in holderList:
holderList.append(field)
field = [currentpoint[0] - hexW / 2, currentpoint[1] - hexH * 0.75]
if field not in playFields and field not in holderList:
holderList.append(field)
field = [currentpoint[0] + hexW / 2, currentpoint[1] - hexH * 0.75]
if field not in playFields and field not in holderList:
holderList.append(field)
print('holder: ', holderList)
for i in holderList:
playFields.append(i)
holderList = []
print('playfields:', playFields)
print('len playfields:', len(playFields))
# Important Game Vars - POSITIONS AND COUNTS
heroPos = []
activeHeroNo = 0
print('heroPos: ', heroPos)
heroCount = 3
for i in (random.choices(playFields, k=heroCount)):
heroPos.append(i)
print('heroPos: ', heroPos)
inputCooldown = 10
inputCooldownCounter = inputCooldown
playSurface = pygame.display.set_mode((width, length))
pygame.display.set_caption('Herobrawl')
# Colors
red = pygame.Color(255, 0, 0)
blue = pygame.Color(0, 0, 255)
green = pygame.Color(0, 255, 0)
white = pygame.Color(255, 255, 255)
black = pygame.Color(0, 0, 0)
grey = pygame.Color(128, 128, 128)
# FPS controller
fpsController = pygame.time.Clock()
# Important Game Functions
def HexDrawer(pos):
return [[pos[0] - hexW / 2, pos[1] - hexH / 4], [pos[0], pos[1] - hexH / 2], [pos[0] + hexW / 2, pos[1] - hexH / 4],
[pos[0] + hexW / 2, pos[1] + hexH / 4], [pos[0], pos[1] + hexH / 2], [pos[0] - hexW / 2, pos[1] + hexH / 4]]
def Move(direction):
if direction == 'RIGHT':
heroPos[activeHeroNo][0] += hexW
if direction == 'LEFT':
heroPos[activeHeroNo][0] -= hexW
if direction == 'UPRIGHT':
heroPos[activeHeroNo][0] += hexW * 0.5
heroPos[activeHeroNo][1] -= hexH * 0.75
if direction == 'DOWNRIGHT':
heroPos[activeHeroNo][0] += hexW * 0.5
heroPos[activeHeroNo][1] += hexH * 0.75
if direction == 'UPLEFT':
heroPos[activeHeroNo][0] -= hexW * 0.5
heroPos[activeHeroNo][1] -= hexH * 0.75
if direction == 'DOWNLEFT':
heroPos[activeHeroNo][0] -= hexW * 0.5
heroPos[activeHeroNo][1] += hexH * 0.75
else:
return
# Main Logic and Loop *************************************************************************************************
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and not keys[pygame.K_DOWN] and not keys[pygame.K_UP]:
if inputCooldownCounter == 0:
Move('LEFT')
inputCooldownCounter = inputCooldown
if keys[pygame.K_RIGHT] and not keys[pygame.K_DOWN] and not keys[pygame.K_UP]:
if inputCooldownCounter == 0:
Move('RIGHT')
inputCooldownCounter = inputCooldown
if keys[pygame.K_DOWN] and keys[pygame.K_RIGHT]:
if inputCooldownCounter == 0:
Move('DOWNRIGHT')
inputCooldownCounter = inputCooldown
if keys[pygame.K_DOWN] and keys[pygame.K_LEFT]:
if inputCooldownCounter == 0:
Move('DOWNLEFT')
inputCooldownCounter = inputCooldown
if keys[pygame.K_UP] and keys[pygame.K_RIGHT]:
if inputCooldownCounter == 0:
Move('UPRIGHT')
inputCooldownCounter = inputCooldown
if keys[pygame.K_UP] and keys[pygame.K_LEFT]:
if inputCooldownCounter == 0:
Move('UPLEFT')
inputCooldownCounter = inputCooldown
if keys[pygame.K_SPACE]: # change hero
if inputCooldownCounter == 0:
if activeHeroNo <= heroCount - 2:
activeHeroNo += 1
else:
activeHeroNo = 0
print('activeHeroNo: ', activeHeroNo)
inputCooldownCounter = inputCooldown
# Rendering
playSurface.fill(grey)
for pos in heroPos: # rendering heroes
if pos == heroPos[activeHeroNo]:
pygame.draw.polygon(playSurface, white, HexDrawer(pos))
else:
pygame.draw.polygon(playSurface, green, HexDrawer(pos))
for field in playFields : # rendering hex fields
pygame.draw.polygon(playSurface, black, HexDrawer(field), 3)
#pygame.draw.polygon(playSurface, black, HexDrawer(pos))
# common main logic parts
if inputCooldownCounter >= 1:
inputCooldownCounter -= 1
pygame.display.flip()
fpsController.tick(30)
Теперь к ошибке ... прокрутите вниз до нижней части кода, где происходит рендеринг, вы увидите, что я рендеринг всеэлементы в "heroPos" - сплошные цвета, а после этого сверху все элементы в "playFields" - черные контуры.
Так что, как бы долго я ни думал об этом, я не понимаю, почему движение также перемещает черный контур!?!
Теоретически это должно работать: я строю список координат для игрового поля, выбираю количество n (heroCount) случайных координат для heroPos Функция «Move» изменяет запись в heroPos, НЕ в игровом поле
Я действительно благодарен за любой совет, который мог бы указать мне правильное направление!Ура, ребята!