这个问题有点令人困惑,但为了使其更易于理解,我将首先发布感兴趣的代码块:
# Event loop (inner)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# mouse handling
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_position = pygame.mouse.get_pos()
if selected_unit:
selected_unit = None
else:
for unit in white_army:
if unit.tile_area.collidepoint(mouse_position):
selected_unit = unit
# highlight the selected unit
if selected_unit:
highlight_tile(selected_unit.tile_area)
mouse_position = pygame.mouse.get_pos()
for unit in white_army:
current_position = selected_unit.position
if on_target():
print('ON TARGET -------------------------------------------')
if event.type == pygame.MOUSEBUTTONDOWN:
print('MOUSE BUTTON PRESSED++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
基本上,我在棋盘上选择一个砖块(以选择一个单位),突出显示它,然后尝试选择所选砖块的[[在顶部上方]],然后在其上重新绘制单位] >。User Rabbid76给了我一个很好的选择(突出显示)瓷砖并取消选择(平铺回到正常状态)的好方法,但是不幸的是涉及到有关MOUSEBUTTONDOWN的检查。
因此,每次我选择上面带有白色单位的图块(目前仅适用于白色单位,如果是长矛手,效果更好),则该图块将根据需要突出显示,但是当我尝试选择棋盘上的下一个图块,循环将把它当作我取消选择第一个图块的方法。
我整个上午都花了整个时间试图找出解决方案,创建新变量,并在循环中切换行的顺序。
[此外,我已经设法实际检查过,一旦选中一个图块,鼠标是否会停在右下一个图块上。不幸的是,我现在要尝试的是检查是否再次按下了鼠标按钮,如果是,则打印“ MOUSE BUTTON PRESSED”,但是我无法使其工作。
这里是完整代码
import pygame
import sys
from coordinator import coordinator
# set 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)
# declare letters and numbers
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
numbers = ['1', '2', '3', '4', '5', '6', '7', '8']
# create coordinates
coordinates = []
for item_letter in letters:
letter = item_letter
for item_number in numbers:
number = item_number
coordinates.append(letter + number)
# create coordinates values components
x_values = []
for number in range(0, 800, 100):
x = number
x_values.append(x)
y_values = []
for number in range(0, 800, 100):
y = number
y_values.append(y)
# create coordinate values
coordinate_values = []
for x in x_values:
for y in y_values:
coordinate_values.append((x, y))
# assign values to coordinates
squares_coordinates = dict(zip(coordinates, coordinate_values))
# Background for units
class CircleSurface:
def __init__(self):
self.circle_surface = pygame.Surface((100, 100), flags=pygame.SRCALPHA)
pygame.draw.circle(self.circle_surface, (255, 0, 0), (50, 50), 45)
# define colours
black = (0, 0, 0)
white = (255, 255, 255)
gold = (153, 153, 0)
green = (0, 255, 0)
dark_green = (0, 200, 0)
class Unit:
def __init__(self, colour, position):
# define Unit colour
self.colour = colour
# define Unit position
self.position = position
class Knight(Unit):
def __init__(self, colour, position):
# draw circle, inline, and outline
super().__init__(colour, position)
self.center_x = position[0]
self.center_y = position[1]
self.colour = colour
self.position = position
# define tile position
self.tile_area = pygame.Rect(position[0] - 50, position[1] - 50, 100, 100)
self.circle_radius = 40
def draw_unit(self):
pygame.draw.circle(game_window, self.colour, self.position, self.circle_radius)
pygame.draw.circle(game_window, gold, self.position, self.circle_radius, 5)
pygame.draw.circle(game_window, gold, self.position, (self.circle_radius - 7), 2)
# draw letter
pygame.font.init()
my_font_size = 50
my_font = pygame.font.SysFont('Time New Roman', my_font_size)
text_surface = my_font.render('K', 1, gold)
center_text = text_surface.get_rect(center=(self.center_x, self.center_y))
game_window.blit(text_surface, center_text)
class Archer(Unit):
def __init__(self, colour, first_point, second_point, third_point):
self.colour = colour
self.first_point = first_point
self.second_point = second_point
self.third_point = third_point
self.position = self.first_point, self.second_point, self.third_point
super().__init__(colour, self.position)
self.center_x = self.second_point[0]
self.center_y = (self.second_point[1] + ((self.first_point[1] - self.second_point[1]) / 2)) + 10
self.inline_position = [(self.first_point[0] + 10, self.first_point[1] - 5),
(self.second_point[0], self.second_point[1] + 10),
(self.third_point[0] - 10, self.third_point[1] - 5)]
# define_tile_centre
self.tile_area = pygame.Rect(self.first_point[0] - 10, self.second_point[1] - 10, 100, 100)
if self.colour == black:
self.first_point = self.first_point[0], self.first_point[1] - 80
self.second_point = self.second_point[0], self.second_point[1] + 80
self.third_point = self.third_point[0], self.third_point[1] - 80
self.position = [self.first_point, self.second_point, self.third_point]
self.center_y = (self.second_point[1] + ((self.first_point[1] - self.second_point[1])
/ 2)) - 10
self.inline_position = [(self.first_point[0] + 10, self.first_point[1] + 5),
(self.second_point[0], self.second_point[1] - 10),
(self.third_point[0] - 10, self.third_point[1] + 5)]
def draw_unit(self):
pygame.draw.polygon(game_window, self.colour, self.position)
pygame.draw.polygon(game_window, gold, self.position, 5)
pygame.draw.polygon(game_window, gold, self.inline_position, 2)
# draw letter
pygame.font.init()
my_font_size = 50
my_font = pygame.font.SysFont('Time New Roman', my_font_size)
text_surface = my_font.render('A', 1, gold)
center_text = text_surface.get_rect(center=(self.center_x, self.center_y))
game_window.blit(text_surface, center_text)
class Pikeman(Unit):
def __init__(self, colour, position):
# draw circle, inline, and outline
super().__init__(colour, position)
self.dimension = (80, 80)
self.center_x = position[0] + self.dimension[0] / 2
self.center_y = position[1] + self.dimension[1] / 2
self.colour = colour
self.position = position
# define_tile_centre
self.tile_area = pygame.Rect(position[0] - 10, position[1] - 10, 100, 100)
self.position_and_dimension = self.position + self.dimension
self.inline_position_and_dimension = (self.position[0] + 5, self.position[1] + 5),\
(self.dimension[0] - 10, self.dimension[1] - 10)
def draw_unit(self):
pygame.draw.rect(game_window, self.colour, self.position_and_dimension, )
pygame.draw.rect(game_window, gold, self.position_and_dimension, 5)
pygame.draw.rect(game_window, gold, self.inline_position_and_dimension, 2)
# draw letter
pygame.font.init()
my_font_size = 50
my_font = pygame.font.SysFont('Time New Roman', my_font_size)
text_surface = my_font.render('P', 1, gold)
center_text = text_surface.get_rect(center=(self.center_x, self.center_y))
game_window.blit(text_surface, center_text)
def highlight_tile(tile_area):
pygame.draw.rect(game_window, dark_green, tile_area, 5)
# 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()
cs = CircleSurface()
# Draws the chessboard
cb.draw()
# set up white units
white_knight_1 = Knight(white, (150, 650))
white_knight_2 = Knight(white, (650, 650))
white_archer_1 = Archer(white, (210, 790), (250, 710), (290, 790))
white_archer_2 = Archer(white, (310, 790), (350, 710), (390, 790))
white_archer_3 = Archer(white, (410, 790), (450, 710), (490, 790))
white_archer_4 = Archer(white, (510, 790), (550, 710), (590, 790))
white_pikeman_1 = Pikeman(white, (210, 610))
white_pikeman_2 = Pikeman(white, (310, 610))
white_pikeman_3 = Pikeman(white, (410, 610))
white_pikeman_4 = Pikeman(white, (510, 610))
# set up black units
black_knight_1 = Knight(black, (150, 150))
black_knight_1.draw_unit()
black_knight_2 = Knight(black, (650, 150))
black_knight_2.draw_unit()
black_archer_1 = Archer(black, (210, 90), (250, 10), (290, 90))
black_archer_2 = Archer(black, (310, 90), (350, 10), (390, 90))
black_archer_3 = Archer(black, (410, 90), (450, 10), (490, 90))
black_archer_4 = Archer(black, (510, 90), (550, 10), (590, 90))
black_pikeman_1 = Pikeman(black, (210, 110))
black_pikeman_2 = Pikeman(black, (310, 110))
black_pikeman_3 = Pikeman(black, (410, 110))
black_pikeman_4 = Pikeman(black, (510, 110))
black_army = [black_knight_1, black_knight_2,
black_archer_1, black_archer_2, black_archer_3, black_archer_4,
black_pikeman_1, black_pikeman_2, black_pikeman_3, black_pikeman_4]
white_army = [white_knight_1, white_knight_2,
white_archer_1, white_archer_2, white_archer_3, white_archer_4,
white_pikeman_1, white_pikeman_2, white_pikeman_3, white_pikeman_4]
army = [white_army, black_army]
print(squares_coordinates.get(letter))
# draw units
for white_unit in white_army:
for black_unit in black_army:
white_unit.draw_unit()
black_unit.draw_unit()
def redraw_units():
Knight(white, white_knight_1.position)
white_knight_1.draw_unit()
Knight(white, white_knight_2.position)
white_knight_2.draw_unit()
Archer(white, white_archer_1.position[0], white_archer_1.position[1], white_archer_1.position[2])
white_archer_1.draw_unit()
Archer(white, white_archer_2.position[0], white_archer_2.position[1], white_archer_2.position[2])
white_archer_2.draw_unit()
Archer(white, white_archer_3.position[0], white_archer_3.position[1], white_archer_3.position[2])
white_archer_3.draw_unit()
Archer(white, white_archer_4.position[0], white_archer_4.position[1], white_archer_4.position[2])
white_archer_4.draw_unit()
Pikeman(white, white_pikeman_1.position)
white_pikeman_1.draw_unit()
Pikeman(white, white_pikeman_2.position)
white_pikeman_2.draw_unit()
Pikeman(white, white_pikeman_3.position)
white_pikeman_3.draw_unit()
Pikeman(white, white_pikeman_4.position)
white_pikeman_4.draw_unit()
pygame.display.update()
selected_unit = None
destination_tile = None
def on_target():
if (current_position[0] < mouse_position[0] < current_position[0] + 100) and \
(current_position[1] - 100) < mouse_position[1] < (current_position[1]):
return True
# Event loop (outer)
while 1:
cb.draw()
# set up white units
white_knight_1 = Knight(white, (150, 650))
white_knight_1.draw_unit()
white_knight_2 = Knight(white, (650, 650))
white_knight_2.draw_unit()
white_archer_1 = Archer(white, (210, 790), (250, 710), (290, 790))
white_archer_1.draw_unit()
white_archer_2 = Archer(white, (310, 790), (350, 710), (390, 790))
white_archer_2.draw_unit()
white_archer_3 = Archer(white, (410, 790), (450, 710), (490, 790))
white_archer_3.draw_unit()
white_archer_4 = Archer(white, (510, 790), (550, 710), (590, 790))
white_archer_4.draw_unit()
white_pikeman_1 = Pikeman(white, (210, 610))
white_pikeman_1.draw_unit()
white_pikeman_2 = Pikeman(white, (310, 610))
white_pikeman_2.draw_unit()
white_pikeman_3 = Pikeman(white, (410, 610))
white_pikeman_3.draw_unit()
white_pikeman_4 = Pikeman(white, (510, 610))
white_pikeman_4.draw_unit()
# set up black units
black_knight_1 = Knight(black, (150, 150))
black_knight_1.draw_unit()
black_knight_2 = Knight(black, (650, 150))
black_knight_2.draw_unit()
black_archer_1 = Archer(black, (210, 90), (250, 10), (290, 90))
black_archer_1.draw_unit()
black_archer_2 = Archer(black, (310, 90), (350, 10), (390, 90))
black_archer_2.draw_unit()
black_archer_3 = Archer(black, (410, 90), (450, 10), (490, 90))
black_archer_3.draw_unit()
black_archer_4 = Archer(black, (510, 90), (550, 10), (590, 90))
black_archer_4.draw_unit()
black_pikeman_1 = Pikeman(black, (210, 110))
black_pikeman_1.draw_unit()
black_pikeman_2 = Pikeman(black, (310, 110))
black_pikeman_2.draw_unit()
black_pikeman_3 = Pikeman(black, (410, 110))
black_pikeman_3.draw_unit()
black_pikeman_4 = Pikeman(black, (510, 110))
black_pikeman_4.draw_unit()
# create armies
black_army = [black_knight_1, black_knight_2,
black_archer_1, black_archer_2, black_archer_3, black_archer_4,
black_pikeman_1, black_pikeman_2, black_pikeman_3, black_pikeman_4]
white_army = [white_knight_1, white_knight_2,
white_archer_1, white_archer_2, white_archer_3, white_archer_4,
white_pikeman_1, white_pikeman_2, white_pikeman_3, white_pikeman_4]
army = [white_army, black_army]
# Event loop (inner)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# mouse handling
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_position = pygame.mouse.get_pos()
if selected_unit:
selected_unit = None
else:
for unit in white_army:
if unit.tile_area.collidepoint(mouse_position):
selected_unit = unit
# highlight the selected unit
if selected_unit:
highlight_tile(selected_unit.tile_area)
mouse_position = pygame.mouse.get_pos()
for unit in white_army:
current_position = selected_unit.position
if on_target():
print('ON TARGET -------------------------------------------')
if event.type == pygame.MOUSEBUTTONDOWN:
print('MOUSE BUTTON PRESSED++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
pygame.display.update()
任何帮助将不胜感激。
这个问题有点令人困惑,但为了使其更容易理解,我将首先发布感兴趣的代码块:#pygame.event.get()中事件的事件循环(内部):if event.type == pygame .. ..
selected_unit
)。根据此状态,您可以在事件循环中处理不同的操作,并且可以在主应用程序循环中进行条件绘制:while 1:
for event in pygame.event.get():
if selected_unit:
# do something
else:
# do something else
if selected_unit:
# draw something
else:
# draw something different
pygame.display.update()