텍스트 뒤에 원을 그려야합니다.
체스 판에 나는 pikeman을위한 '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
.
또한 원 표면의 픽셀 형식에 픽셀 당 알파가 포함되어 있는지 확인하고 원 표면에 원을 그려야합니다.
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] 삭제
몇 마디 만하겠습니다