我需要在一些文字后面画一个圆圈。
在棋盘上,我设法显示了三个字母:一个是长枪手的“ P”,一个是骑士的“ K”,以及一个弓箭手的“ A”。
目前,我试图在棋盘上的某个地方显示一个圆圈,一旦我真正看到它,我将传递正确的坐标。
这些字母是红色的,仅用于测试目的,但是将来,它们将是黑色或白色。因此需要一个圆圈,这样我才能在黑色正方形上看到黑色字母。
我不在乎圆现在是什么颜色,但是我也考虑不实际填充圆,而是画轮廓。
Unit是父类,Pikeman,Archer和Knight是子类。我宁愿从Unit类中画出一个圆,因为这是三个子类共有的一个“变量”(如果需要的话)。
我试图在Unit类中创建一个曲面,然后从那里绘制一个圆。无法使其工作。
我试图在Unit类内部创建一个曲面,并从子类中绘制它。无法使其工作。
因此,我决定创建一个CircleSurface类,创建该类的实例并将其传递给draw方法。不,不行!
如我所说,我宁愿从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
对象cs
的实例时,不限定Pikeman
,Archer
并且Knight
被构造。
只需创建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)
对Archer
和执行相同的操作Knight
。
此外,您必须确保圆形表面的像素格式将包含每个像素的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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句