gpt4 book ai didi

python - 画圆不显示

转载 作者:行者123 更新时间:2023-12-01 00:27:44 27 4
gpt4 key购买 nike

我需要在一些文本后面画一个圆圈。

在棋盘上,我成功地显示了三个字母:“P”代表长枪兵,“K”代表骑士,“A”代表弓箭手。

目前我正在尝试在棋盘上的某个位置显示一个圆圈,一旦我真正看到它,我就会传递正确的坐标。

这些字母是红色的,只是为了测试目的,但将来它们将是黑色或白色。因此需要这个圆圈,以便我能够看到黑色方 block 上的黑色字母。

我现在不在乎圆圈是什么颜色,但我也在想不要实际填充圆圈,而只是画轮廓。

Unit是父类,Pikeman、Archer、Knight是子类。我宁愿从 Unit 类中绘制圆圈,因为它是三个子类共有的一个“变量”(如果我们愿意的话)。

我尝试在 Unit 类中创建一个曲面,然后从那里画一个圆。无法使其工作。

我尝试在 Unit 类中创建一个表面并从子类中绘制它。无法使其工作。

因此,我决定创建一个 CircleSurface 类,创建该类的一个实例并将其传递给绘制方法。不,不起作用!

正如我所说,我宁愿从 Unit 类中绘制圆,而不必创建 CircleSurface 类。

import pygame
import sys
from coordinator import coordinator

# Sets up the display
pygame.init()
window_size = (800, 800)
game_window = pygame.display.set_mode(size=window_size)
pygame.display.set_caption('My Game')


# Defines classes and related methods
class WhiteSquare:
def __init__(self):
self.height = int(window_size[0] / 8)
self.width = int(window_size[1] / 8)
self.white_square = pygame.Surface((self.height, self.width))
self.white_square.fill((255, 255, 255))


class BlackSquare:
def __init__(self):
self.height = int(window_size[0] / 8)
self.width = int(window_size[1] / 8)
self.black_square = pygame.Surface((self.height, self.width))
self.black_square.fill((0, 0, 0))


class ChessBoard:
def __init__(self):
self.ws = ws
self.bs = bs
self.white_columns = white_columns
self.black_columns = black_columns

def draw(self):
for w_columns in self.white_columns:
game_window.blit(self.ws.white_square, w_columns)

for b_columns in self.black_columns:
game_window.blit(self.bs.black_square, b_columns)


# class SquareNames:
# letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
# numbers = ['1', '2', '3', '4', '5', '6', '7', '8']
# square_names = []
# for letter in letters:
# for number in numbers:
# square_name = letter + number
# square_names.append(square_name)
# print((square_names))
# # for coordinate in
# # coordinates = (square_name : coordinate)

class CircleSurface:
def __init__(self):
self.circle_surface = pygame.Surface((100, 100))
self.circle_surface.fill((0, 0, 255))


class Unit:
def __init__(self):
self.surface = pygame.Surface((100, 100))
self.my_font = pygame.font.SysFont('Time New Roman', 100)
self.cs = cs

def draw_circle(self):
pygame.draw.circle(self.cs.circle_surface, (0, 255, 0), (200, 200), 50)


class Pikeman(Unit):
unit_type = 'P'
destination = (125, 125)

def __init__(self):
super().__init__()
self.img = self.my_font.render(self.unit_type, 1, (255, 0, 0))

def draw(self, surface):
surface.blit(self.img, self.destination)


class Archer(Unit):
unit_type = 'A'
destination = (525, 525)

def __init__(self):
super().__init__()
self.img = self.my_font.render(self.unit_type, 1, (255, 0, 0))

def draw(self, surface):
surface.blit(self.img, self.destination)


class Knight(Unit):
unit_type = 'K'
destination = (325, 525)

def __init__(self):
super().__init__()
self.img = self.my_font.render(self.unit_type, 1, (255, 0, 0))

def draw(self, surface):
surface.blit(self.img, self.destination)


# Sets and gets the coordinates for black and white squares
coordinator = coordinator()
black_columns = coordinator[2] + coordinator[3]
white_columns = coordinator[0] + coordinator[1]

# Creates needed objects
ws = WhiteSquare()
bs = BlackSquare()
cb = ChessBoard()
p = Pikeman()
a = Archer()
k = Knight()
cs = CircleSurface()


# Event loop (outer)
while 1:

# Event loop (inner)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Draws needed objects and updates display
cb.draw()
p.draw(game_window)
p.draw_circle()
k.draw(game_window)
a.draw(game_window)
pygame.display.update()

显示的错误如下:

Traceback (most recent call last):
File "C:/Users/oricc/PycharmProjects/designAChessboardChallange/display.py", line 117, in <module>
p = Pikeman()
File "C:/Users/oricc/PycharmProjects/designAChessboardChallange/display.py", line 77, in __init__
super().__init__()
File "C:/Users/oricc/PycharmProjects/designAChessboardChallange/display.py", line 66, in __init__
self.cs = cs
NameError: name 'cs' is not defined

最佳答案

构造PikemanArcherKnight实例时, undefined object cs

只需之前创建CircleSurface实例即可解决问题。例如:

cs = CircleSurface()
p = Pikeman()
a = Archer()
k = Knight()
<小时/>

但我建议将 CircleSurface 对象作为参数传递:

class Unit:
def __init__(self, cs):
self.surface = pygame.Surface((100, 100))
self.my_font = pygame.font.SysFont('Time New Roman', 100)
self.cs = cs

def draw_circle(self):
pygame.draw.circle(self.cs.circle_surface, (0, 255, 0), (200, 200), 50)

class Pikeman(Unit):
unit_type = 'P'
destination = (125, 125)

def __init__(self, cs):
super().__init__(cs)
self.img = self.my_font.render(self.unit_type, 1, (255, 0, 0))

def draw(self, surface):
surface.blit(self.img, self.destination)
cs = CircleSurface()
p = Pikeman(cs)

弓箭手骑士执行相同的操作。

<小时/>

此外,您还要确保圆表面的像素格式将包含每像素 alpha,并且您必须在圆表面上绘制圆:

class CircleSurface:
def __init__(self):
self.circle_surface = pygame.Surface((100, 100), flags=pygame.SRCALPHA)
self.circle_surface.fill((0, 0, 0, 0))
pygame.draw.circle(self.circle_surface, (0, 255, 0), (50, 50), 50)

blit 圆表面到窗口表面

class Unit:
# [...]

def draw_circle(self, surface):
surface.blit(self.cs.circle_surface, (200, 200))
p.draw_circle(game_window)

关于python - 画圆不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58425181/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com