从这个pygame代码中的错误列表中渲染; Hexgame

时间:2018-05-08 13:38:41

标签: python pygame

我在一个小小的pygame练习中遇到了一个错误,我正在尝试构建一个十六进制的桌面游戏......

我试图最小化代码,但由于我完全没有线索,哪里出了问题,它仍然有点工作:

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)

现在到了bug ... 向下滚动到渲染发生的代码的底部,你会看到我将“heroPos”中的所有项目渲染为纯色,然后在“playFields”中将所有项目渲染为黑色轮廓。

所以无论我多久思考它,我都不明白为什么机芯也会移动黑色轮廓!?!

理论上它应该有效: 我为比赛场地建立了一系列坐标 我为heroPos选择随机坐标的数量n(heroCount) “移动”功能修改了不在playfield

中的heroPos中的条目

我非常感谢任何可以指引我走向正确方向的建议! 干杯啦!

0 个答案:

没有答案