พยายามที่จะดึงประมวลผลได้หลายตัวของ sprite วัตถุอยู่ใน pygame และคนแรกวาดอย่างถูกต้องแต่งหน้าแสดงพื้นหลังเดียว

0

คำถาม

ฉันกำลังพยายามทำ diagonal แถวของ hexagons แต่ตอนที่ฉันพยายามจะล่อพวกมันเพียงคนแรกทำงาน นี่คือภาพหน้าจอที่จับได้ของ pygame งหน้าต่าง มันควรจะ hexagons ในทุกล่องดำ

นี่คือรหัส:

import pygame
from pygame.locals import *
from sys import exit

pygame.init()

screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height), 0, 32)

class Hex(pygame.sprite.Sprite):
    def __init__(self, s_len, op):
        super().__init__()
        x, y = op
        root3 = 1.73205080
        self.color = (125,200,24)
        self.op = op
        self.s_len = s_len
        self.points = [(x + (.5 * s_len), y),
                       (x + (1.5 * s_len), y),
                       (x + (2 * s_len), y + ((root3 / 2) * s_len)),
                       (x + (1.5 * s_len), y + (root3 * s_len)),
                       (x + (.5 * s_len), y + (root3 * s_len)),
                       (x, y + (root3 / 2) * s_len)]

        self.image = pygame.Surface([2 * s_len, root3 * s_len])
        self.image.fill((0,0,0))
        pygame.draw.polygon(self.image, self.color, self.points)

        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)


hex_group = pygame.sprite.Group()

for i in range(7):
    hex_group.add(Hex(50, (i * 75, i * 86)))


while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

    screen.fill((255,255,255))

    hex_group.draw(screen)

    pygame.display.update()

ฉันคิดว่าประเด็นต้องมีอะไรบางอย่างเกี่ยวข้องกับตัวเอง.แฟ้มถูกสร้างจากกล้องอยู่ในชั้นเรียนเลขฐานสิบห constructor แต่ไม่รู้วิธีที่จะซ่อมมันได้

pygame python
2021-11-22 05:11:32
1

คำตอบที่ดีที่สุด

1

คุณกำลังวาดภาพหลายเหลี่ยมที่อยู่ภาพไม่องจอภาพด้วย ด้านบนซ้ายของ self.image คือ(0,0):

self.points = [(.5 * s_len, 0),
               (1.5 * s_len, 0),
               (2 * s_len, (root3 / 2) * s_len),
               (1.5 * s_len, root3 * s_len),
               (.5 * s_len, root3 * s_len),
               (0, (root3 / 2) * s_len)]

สมบูรณ์ exemple:

import pygame
from pygame.locals import *
from sys import exit

pygame.init()

screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height), 0, 32)

class Hex(pygame.sprite.Sprite):
    def __init__(self, s_len, op):
        super().__init__()
        x, y = op
        root3 = 1.73205080
        self.color = (125,200,24)
        self.op = op
        self.s_len = s_len
        self.points = [(.5 * s_len, 0),
               (1.5 * s_len, 0),
               (2 * s_len, (root3 / 2) * s_len),
               (1.5 * s_len, root3 * s_len),
               (.5 * s_len, root3 * s_len),
               (0, (root3 / 2) * s_len)]

        self.image = pygame.Surface([2 * s_len, root3 * s_len])
        self.image.fill((0,0,0))
        pygame.draw.polygon(self.image, self.color, self.points)

        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)


hex_group = pygame.sprite.Group()

for i in range(7):
    hex_group.add(Hex(50, (i * 75, i * 86)))


while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

    screen.fill((255,255,255))

    hex_group.draw(screen)

    pygame.display.update()
2021-11-22 05:33:28

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่