我正在开发一款名为“鲍鱼”的游戏,我似乎无法同时选择多个硬币。如果我选择一种硬币然后单击另一种硬币,则该选择会重置,并且只会选择最新选择的硬币。
我在下面附加了我的代码。
import pygame
from pygame.locals import *
import sys
import random
import math
pygame.init()
win=pygame.display.set_mode((1000,600))
colorname=(128,128,128)
display_width=1000
display_height=600
red=(255,0,0)
blue=(0,0,255)
green=(0,255,0)
pygame.display.set_caption(("Abalone"))
pygame.draw.polygon(win,(0,0,0),[(650,0),(1000,0),(1000,600),(600,600)],0)
pygame.display.update()
def abalone_board():
pygame.draw.polygon(win, (165,42,42),[(150,20),(450,20),(600,300),(450,580),(150,580),(0,300)],0)
pygame.draw.polygon(win, (0,0,0),[(150,20),(450,20),(600,300),(450,580),(150,580),(0,300)],5)
for i in range(172,434,64):
pygame.draw.circle(win,(237, 187, 153),(i, 60),30,0)
for i in range(140,499,64):
pygame.draw.circle(win,(237, 187, 153),(i, 120),30,0)
for i in range(110,499,64):
pygame.draw.circle(win,(237, 187, 153),(i, 180),30,0)
for i in range(80,563,64):
pygame.draw.circle(win,(237, 187, 153),(i, 240),30,0)
for i in range(50,563,64):
pygame.draw.circle(win,(237, 187, 153),(i, 300),30,0)
for i in range(80,563,64):
pygame.draw.circle(win,(237, 187, 153),(i, 360),30,0)
for i in range(110,499,64):
pygame.draw.circle(win,(237, 187, 153),(i, 420),30,0)
for i in range(140,499,64):
pygame.draw.circle(win,(237, 187, 153),(i, 480),30,0)
for i in range(172,434,64):
pygame.draw.circle(win,(237, 187, 153),(i, 540),30,0)
for i in range(172,434,64):
pygame.draw.circle(win,(0,0,0),(i, 60),30,2)
for i in range(140,499,64):
pygame.draw.circle(win,(0,0,0),(i, 120),30,2)
for i in range(110,499,64):
pygame.draw.circle(win,(0,0,0),(i, 180),30,2)
for i in range(80,563,64):
pygame.draw.circle(win,(0,0,0),(i, 240),30,2)
for i in range(50,563,64):
pygame.draw.circle(win,(0,0,0),(i, 300),30,2)
for i in range(80,563,64):
pygame.draw.circle(win,(0,0,0),(i, 360),30,2)
for i in range(110,499,64):
pygame.draw.circle(win,(0,0,0),(i, 420),30,2)
for i in range(140,499,64):
pygame.draw.circle(win,(0,0,0),(i, 480),30,2)
for i in range(172,434,64):
pygame.draw.circle(win,(0,0,0),(i, 540),30,2)
return abalone_board
def leader_board():
pygame.draw.polygon(win,(192,192,192),[(650,0),(1000,0),(1000,600),(650,600)],0)
pygame.draw.polygon(win,(0,0,0),[(650,5),(995,5),(995,595),(650,595)],10)
return leader_board
def marble_red():
pygame.draw.circle(win,(255,0,0),(172, 60),23,0)
return marble_red
def ini_pos_red():
r1=pygame.draw.circle(win,(255,0,0),(428,540),23,0)
r2=pygame.draw.circle(win,(255,0,0),(364,540),23,0)
r3=pygame.draw.circle(win,(255,0,0),(300,540),23,0)
r4=pygame.draw.circle(win,(255,0,0),(236,540),23,0)
r5=pygame.draw.circle(win,(255,0,0),(172,540),23,0)
r6=pygame.draw.circle(win,(255,0,0),(460,480),23,0)
r7=pygame.draw.circle(win,(255,0,0),(396,480),23,0)
r8=pygame.draw.circle(win,(255,0,0),(332,480),23,0)
r9=pygame.draw.circle(win,(255,0,0),(268,480),23,0)
r10=pygame.draw.circle(win,(255,0,0),(204,480),23,0)
r11=pygame.draw.circle(win,(255,0,0),(140,480),23,0)
r12=pygame.draw.circle(win,(255,0,0),(238,420),23,0)
r13=pygame.draw.circle(win,(255,0,0),(302,420),23,0)
r14=pygame.draw.circle(win,(255,0,0),(366,420),23,0)
return ini_pos_red
def ini_pos_blue():
b1=pygame.draw.circle(win,(0,0,255),(428,60),23,0)
b2=pygame.draw.circle(win,(0,0,255),(364,60),23,0)
b3=pygame.draw.circle(win,(0,0,255),(300,60),23,0)
b4=pygame.draw.circle(win,(0,0,255),(236,60),23,0)
b5=pygame.draw.circle(win,(0,0,255),(172,60),23,0)
b6=pygame.draw.circle(win,(0,0,255),(460,120),23,0)
b7=pygame.draw.circle(win,(0,0,255),(396,120),23,0)
b8=pygame.draw.circle(win,(0,0,255),(332,120),23,0)
b9=pygame.draw.circle(win,(0,0,255),(268,120),23,0)
b10=pygame.draw.circle(win,(0,0,255),(204,120),23,0)
b11=pygame.draw.circle(win,(0,0,255),(140,120),23,0)
b12=pygame.draw.circle(win,(0,0,255),(238,180),23,0)
b13=pygame.draw.circle(win,(0,0,255),(302,180),23,0)
b14=pygame.draw.circle(win,(0,0,255),(366,180),23,0)
return ini_pos_blue
def text_object(text,color,size):
if size=='small':
textSurface=smallfont.render(text,True,color)
if size=='medium':
textSurface=medfont.render(text,True,color)
if size=='large':
textSurface=largefont.render(text,True,color)
return textSurface, textSurface.get_rect()
def message_to_screen(msg,color,y_displace=0,size='small'):
textSurf,textRect=text_object(msg,color,size)
textRect.center=(display_width/2),(display_height/2)+y_displace
win.blit(textSurf,textRect)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_c:
intro = False
if event.key==pygame.K_q:
pygame.quit()
sys.exit(0)
quit()
win.fill((255,255,255))
message_to_screen("Welcome to Abalone",red,-100,"large")
message_to_screen("The Objective of this game is to push your opponents marbles outside",blue,-30)
message_to_screen("At a trun , you can either move one , two or three marbles",blue,10)
message_to_screen("you can push your opponent's marbles that are in a line ",blue,50)
message_to_screen("to their own with an in-line move only.you can only push ",blue,90)
message_to_screen("if the pushing line has more marbles than the pushed line ",blue,130)
message_to_screen("Press C to play and Q to exit ",green,200)
pygame.display.update()
x1=random.randint(0,61)
marble_red()
position=[(172,60),(236,60),(300,60),(364,60),(428,60),(140,120),(204,120),(268,120),(332,120),
(396,120),(460,120),(110,180),(174,180),(238,180),(302,180),(366,180),(430,180),
(494,180),(80,240),(144,240),(208,240),(272,240),(336,240),(400,240),(464,240),
(528,240),(50,300),(114,300),(178,300),(242,300),(306,300),(370,300),(434,300),
(498,300),(562,300),(80,360),(144,360),(208,360),(272,360),(336,360),(400,360),
(464,360),(528,360),(110,420),(174,420),(238,420),(302,420),(366,420),(430,420),
(494,420),(140,480),(204,480),(268,480),(332,480),(396,480),(460,480),(172,540),
(236,540),(300,540),(300,540),(364,540),(428,540)]
smallfont = pygame.font.SysFont("comicsansms", 25)
medfont = pygame.font.SysFont("comicsansms", 50)
largefont = pygame.font.SysFont("comicsansms", 80)
font = pygame.font.SysFont("comicsansms", 68)
text = font.render("ABALONE", True, (0, 0, 0))
grid=[
[1,1,1,1,1],
[1,1,1,1,1,1],
[0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,2,2,2,0,0],
[2,2,2,2,2,2],
[2,2,2,2,2]
]
class circle:
def __init__(self, pos, colour):
self.pos = pos
self.colour = colour
self.radius = 23
def circle_clicked_on(pos, circles):
for c in circles:
if distance(c.pos, pos) < c.radius:
return c
return False
def distance(p1,p2):
return math.sqrt(((p1[0] - p2[0])**2) + ((p1[1] - p2[1])**2))
circles=[]
for p in position:
circles.append(circle(p, red))
posn=pygame.mouse.get_pos()
def gameloop():
red=(255,0,0)
run=True
current_circle = False
while run:
pygame.time.delay(250)
ms=pygame.mouse.get_pressed()
for event in pygame.event.get():
if event.type==pygame.QUIT:
run=False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
run = False
elif event.type==pygame.MOUSEBUTTONDOWN :
current_circle = circle_clicked_on(pygame.mouse.get_pos(), circles)
keys=pygame.key.get_pressed()
win.fill((colorname))
abalone_board()
leader_board()
ini_pos_red()
ini_pos_blue()
if current_circle and win.get_at(current_circle.pos)==red :
pygame.draw.circle(win,(255,255,255),current_circle.pos,23,5)
win.blit(text, (659,0))
pygame.display.update()
pygame.quit()
sys.exit(0)
game_intro()
gameloop()
答案 0 :(得分:0)
您将所选硬币存储在变量current_circle
中,并且每次玩家单击硬币时,变量都会被覆盖。
您可以改用列表,如下所示:
...
run=True
clicked_circles = [] # use a list here
while run:
...
elif event.type==pygame.MOUSEBUTTONDOWN :
# store clicked coins in a list
current_circle = circle_clicked_on(pygame.mouse.get_pos(), circles)
if current_circle and win.get_at(current_circle.pos)==red:
clicked_circles.append(current_circle)
keys=pygame.key.get_pressed()
win.fill((colorname))
abalone_board()
leader_board()
ini_pos_red()
ini_pos_blue()
for current_circle in clicked_circles: # iterate over list
pygame.draw.circle(win,(255,255,255),current_circle.pos,23,5)
...