Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 六边形不显示_Python_Pygame_Sprite - Fatal编程技术网

Python 六边形不显示

Python 六边形不显示,python,pygame,sprite,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 rando

我的代码有一个问题,我试图画一个六边形,但是出现了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\u generator
,您将看到这些点位于图像之外(图像大小为100*100像素)

您不应该使用玩家的世界坐标作为起始
x,y
值。如果六边形应该居中,您可以将图像的半边长度传递给生成器,并将其添加到
x
y

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

我刚刚修复了格式,但您仍然应该尝试将代码简化为一个新的格式。这两个模块也可以连接起来。
[(240.0, 200.0), (260.0, 234.64101615137753), (240.0, 269.28203230275506), (200.0, 269.28203230275506), (179.99999999999997, 234.64101615137753), (199.99999999999997, 200.0)]
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