Hexagon没有显示

时间:2017-07-02 21:50:42

标签: python pygame sprite

我的代码有问题,我试图绘制六边形多边形,但是n出现了。当我尝试使用eclipse或其他pygame.draw函数时,问题就在于多边形。这是我的代码。我认为整个程序运行正常,问题出在这里:

hexagon = Hexagon.hexagon_generator(40,self.rect.x,self.rect.y)
pygame.draw.polygon(self.image,(0,225,0),list(hexagon),0)

整个计划:

import pygame
import random
import Hexagon

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

class Player(pygame.sprite.Sprite):
    def __init__(self,x,y):
        super().__init__()
        self.image = pygame.Surface([100,100])
        self.rect = self.image.get_rect()
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)
        self.rect.x = x
        self.rect.y = y
        hexagon = Hexagon.hexagon_generator(40,self.rect.x,self.rect.y)
        pygame.draw.polygon(self.image,(0,225,0),list(hexagon),0)

        self.velocity_y = 0
        self.velocity_x = 0

    def move(self, x, y):
        self.velocity_x += x
        self.velocity_y += y

    def update(self):
        self.rect.x += self.velocity_x
        self.rect.y += self.velocity_y
        if self.rect.x >785:
            self.rect.x =785
        elif self.rect.x <0:
            self.rect.x =0
        elif self.rect.y > 585:
            self.rect.y = 585
        elif self.rect.y < 0:
            self.rect.y = 0
        elif self.rect.x<0 and self.rect.y < 0:
            self.rect.x = 0
            self.rect.y = 0

pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode([screen_width, screen_height])
all_sprites_list = pygame.sprite.Group()

player = Player(200,200)
all_sprites_list.add(player)
done = False
clock = pygame.time.Clock()

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                player.move(0,5)
            if event.key == pygame.K_UP:
                player.move(0, -5)
            if event.key == pygame.K_LEFT:
                player.move(-5, 0)
            if event.key == pygame.K_RIGHT:
                player.move(5, 0)
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_DOWN:
                player.move(0, -5)
            if event.key == pygame.K_UP:
                player.move(0, 5)
            if event.key == pygame.K_LEFT:
                player.move(5, 0)
            if event.key == pygame.K_RIGHT:
                player.move(-5, 0)

    screen.fill(WHITE)
    all_sprites_list.update()

    all_sprites_list.draw(screen)

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

有一个Hexagon模块:

import math

def hexagon_generator(edge_length, x,y):
    for angle in range(0, 360, 60):
        x += math.cos(math.radians(angle)) * edge_length
        y += math.sin(math.radians(angle)) * edge_length
        yield x, y

1 个答案:

答案 0 :(得分:0)

如果您打印hexagon_generator产生的值,您将看到这些点位于图像之外(大小为100 * 100像素)。

[(240.0, 200.0), (260.0, 234.64101615137753), (240.0, 269.28203230275506), (200.0, 269.28203230275506), (179.99999999999997, 234.64101615137753), (199.99999999999997, 200.0)]

您不应将播放器的世界坐标用作起始x, y值。如果六边形应居中,您只需将图像的半边长度传递给生成器并将其添加到xy

def hexagon_generator(edge_length):
    for angle in range(0, 360, 60):
        x = math.cos(math.radians(angle)) * edge_length + edge_length
        y = math.sin(math.radians(angle)) * edge_length + edge_length
        yield x, y