我做了一个简单的连接4游戏使用PyGame。我对python还不熟悉,而且还没有完全完善它。我知道这很低效,但我不知道怎么解决它?有人能帮我吗?我们会非常感激的。
import pygame
from colors import *
import random
pygame.init()
size = (320, 280)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Connect 4 but Worse")
done = False
clock = pygame.time.Clock()
turn = 0
class Space():
def __init__(self):
self.color = WHITE
self.height = 40
self.width = 40
self.x = 5
self.y = 5
def Draw(self, screen):
pygame.draw.ellipse(screen, self.color, [self.x, self.y, 40, 40])
x_list = [5, 50, 95, 140, 185, 230, 275]
y_list = [5, 50, 95, 140, 185, 230]
spaces_list = []
x_counter = 0
y_counter = 0
for i in range(42):
slot = Space()
slot.color = WHITE
slot.height = 40
slot.width = 40
slot.x = x_list[x_counter]
slot.y = y_list[y_counter]
spaces_list.append(slot)
x_counter += 1
if x_counter == 7:
y_counter +=1
x_counter = 0
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
(posx,posy) = pygame.mouse.get_pos()
if posx > 5 and posx < 45:
if spaces_list[35].color == WHITE:
if turn == 0:
spaces_list[35].color = RED
turn = 1
elif turn == 1:
spaces_list[35].color = YELLOW
turn = 0
elif spaces_list[28].color == WHITE:
if turn == 0:
spaces_list[28].color = RED
turn = 1
elif turn == 1:
spaces_list[28].color = YELLOW
turn = 0
elif spaces_list[21].color == WHITE:
if turn == 0:
spaces_list[21].color = RED
turn = 1
elif turn == 1:
spaces_list[21].color = YELLOW
turn = 0
elif spaces_list[14].color == WHITE:
if turn == 0:
spaces_list[14].color = RED
turn = 1
elif turn == 1:
spaces_list[14].color = YELLOW
turn = 0
elif spaces_list[7].color == WHITE:
if turn == 0:
spaces_list[7].color = RED
turn = 1
elif turn == 1:
spaces_list[7].color = YELLOW
turn = 0
elif spaces_list[0].color == WHITE:
if turn == 0:
spaces_list[0].color = RED
turn = 1
elif turn == 1:
spaces_list[0].color = YELLOW
turn = 0
if posx > 50 and posx < 90:
if spaces_list[36].color == WHITE:
if turn == 0:
spaces_list[36].color = RED
turn = 1
elif turn == 1:
spaces_list[36].color = YELLOW
turn = 0
elif spaces_list[29].color == WHITE:
if turn == 0:
spaces_list[29].color = RED
turn = 1
elif turn == 1:
spaces_list[29].color = YELLOW
turn = 0
elif spaces_list[22].color == WHITE:
if turn == 0:
spaces_list[22].color = RED
turn = 1
elif turn == 1:
spaces_list[22].color = YELLOW
turn = 0
elif spaces_list[15].color == WHITE:
if turn == 0:
spaces_list[15].color = RED
turn = 1
elif turn == 1:
spaces_list[15].color = YELLOW
turn = 0
elif spaces_list[8].color == WHITE:
if turn == 0:
spaces_list[8].color = RED
turn = 1
elif turn == 1:
spaces_list[8].color = YELLOW
turn = 0
elif spaces_list[1].color == WHITE:
if turn == 0:
spaces_list[1].color = RED
turn = 1
elif turn == 1:
spaces_list[1].color = YELLOW
turn = 0
if posx > 95 and posx < 135:
if spaces_list[37].color == WHITE:
if turn == 0:
spaces_list[37].color = RED
turn = 1
elif turn == 1:
spaces_list[37].color = YELLOW
turn = 0
elif spaces_list[30].color == WHITE:
if turn == 0:
spaces_list[30].color = RED
turn = 1
elif turn == 1:
spaces_list[30].color = YELLOW
turn = 0
elif spaces_list[23].color == WHITE:
if turn == 0:
spaces_list[23].color = RED
turn = 1
elif turn == 1:
spaces_list[23].color = YELLOW
turn = 0
elif spaces_list[16].color == WHITE:
if turn == 0:
spaces_list[16].color = RED
turn = 1
elif turn == 1:
spaces_list[16].color = YELLOW
turn = 0
elif spaces_list[9].color == WHITE:
if turn == 0:
spaces_list[9].color = RED
turn = 1
elif turn == 1:
spaces_list[9].color = YELLOW
turn = 0
elif spaces_list[2].color == WHITE:
if turn == 0:
spaces_list[2].color = RED
turn = 1
elif turn == 1:
spaces_list[2].color = YELLOW
turn = 0
if posx > 140 and posx < 180:
if spaces_list[38].color == WHITE:
if turn == 0:
spaces_list[38].color = RED
turn = 1
elif turn == 1:
spaces_list[38].color = YELLOW
turn = 0
elif spaces_list[31].color == WHITE:
if turn == 0:
spaces_list[31].color = RED
turn = 1
elif turn == 1:
spaces_list[31].color = YELLOW
turn = 0
elif spaces_list[24].color == WHITE:
if turn == 0:
spaces_list[24].color = RED
turn = 1
elif turn == 1:
spaces_list[24].color = YELLOW
turn = 0
elif spaces_list[17].color == WHITE:
if turn == 0:
spaces_list[17].color = RED
turn = 1
elif turn == 1:
spaces_list[17].color = YELLOW
turn = 0
elif spaces_list[10].color == WHITE:
if turn == 0:
spaces_list[10].color = RED
turn = 1
elif turn == 1:
spaces_list[10].color = YELLOW
turn = 0
elif spaces_list[3].color == WHITE:
if turn == 0:
spaces_list[3].color = RED
turn = 1
elif turn == 1:
spaces_list[3].color = YELLOW
turn = 0
if posx > 185 and posx < 225:
if spaces_list[39].color == WHITE:
if turn == 0:
spaces_list[39].color = RED
turn = 1
elif turn == 1:
spaces_list[39].color = YELLOW
turn = 0
elif spaces_list[32].color == WHITE:
if turn == 0:
spaces_list[32].color = RED
turn = 1
elif turn == 1:
spaces_list[32].color = YELLOW
turn = 0
elif spaces_list[25].color == WHITE:
if turn == 0:
spaces_list[25].color = RED
turn = 1
elif turn == 1:
spaces_list[25].color = YELLOW
turn = 0
elif spaces_list[18].color == WHITE:
if turn == 0:
spaces_list[18].color = RED
turn = 1
elif turn == 1:
spaces_list[18].color = YELLOW
turn = 0
elif spaces_list[11].color == WHITE:
if turn == 0:
spaces_list[11].color = RED
turn = 1
elif turn == 1:
spaces_list[11].color = YELLOW
turn = 0
elif spaces_list[4].color == WHITE:
if turn == 0:
spaces_list[4].color = RED
turn = 1
elif turn == 1:
spaces_list[4].color = YELLOW
turn = 0
if posx > 230 and posx < 270:
if spaces_list[40].color == WHITE:
if turn == 0:
spaces_list[40].color = RED
turn = 1
elif turn == 1:
spaces_list[40].color = YELLOW
turn = 0
elif spaces_list[33].color == WHITE:
if turn == 0:
spaces_list[33].color = RED
turn = 1
elif turn == 1:
spaces_list[33].color = YELLOW
turn = 0
elif spaces_list[26].color == WHITE:
if turn == 0:
spaces_list[26].color = RED
turn = 1
elif turn == 1:
spaces_list[26].color = YELLOW
turn = 0
elif spaces_list[19].color == WHITE:
if turn == 0:
spaces_list[19].color = RED
turn = 1
elif turn == 1:
spaces_list[19].color = YELLOW
turn = 0
elif spaces_list[12].color == WHITE:
if turn == 0:
spaces_list[12].color = RED
turn = 1
elif turn == 1:
spaces_list[12].color = YELLOW
turn = 0
elif spaces_list[5].color == WHITE:
if turn == 0:
spaces_list[5].color = RED
turn = 1
elif turn == 1:
spaces_list[5].color = YELLOW
turn = 0
if posx > 275:
if spaces_list[41].color == WHITE:
if turn == 0:
spaces_list[41].color = RED
turn = 1
elif turn == 1:
spaces_list[41].color = YELLOW
turn = 0
elif spaces_list[34].color == WHITE:
if turn == 0:
spaces_list[34].color = RED
turn = 1
elif turn == 1:
spaces_list[34].color = YELLOW
turn = 0
elif spaces_list[27].color == WHITE:
if turn == 0:
spaces_list[27].color = RED
turn = 1
elif turn == 1:
spaces_list[27].color = YELLOW
turn = 0
elif spaces_list[20].color == WHITE:
if turn == 0:
spaces_list[20].color = RED
turn = 1
elif turn == 1:
spaces_list[20].color = YELLOW
turn = 0
elif spaces_list[13].color == WHITE:
if turn == 0:
spaces_list[13].color = RED
turn = 1
elif turn == 1:
spaces_list[13].color = YELLOW
turn = 0
elif spaces_list[6].color == WHITE:
if turn == 0:
spaces_list[6].color = RED
turn = 1
elif turn == 1:
spaces_list[6].color = YELLOW
turn = 0
screen.fill(BLUE)
counter = 0
for items in spaces_list:
spaces_list[counter].Draw(screen)
counter += 1
pygame.display.flip()
clock.tick(60)
pygame.quit()发布于 2019-12-19 01:35:41
我希望我能对while not done:下的巨大质量提出一个更全面的建议。那里的大量重复是不可持续的。虽然我很累,所以我不得不坚持更简单的问题。
在这一部分中,您做了一些类似的事情,很多次:
if turn == 0:
spaces_list[#].color = RED
turn = 1
elif turn == 1:
spaces_list[#].color = YELLOW
turn = 0至少,我会试着去解决这个问题。类似于创建一个toggle_state函数:
def toggle_function(index: int) -> None:
global turn
if turn == 0:
spaces_list[index].color = RED
turn = 1
elif turn == 1:
spaces_list[index].color = YELLOW
turn = 0现在,你可以
if spaces_list[35].color == WHITE:
toggle_state(35)
elif spaces_list[28].color == WHITE:
toggle_state(28)而不是
if spaces_list[35].color == WHITE:
if turn == 0:
spaces_list[35].color = RED
turn = 1
elif turn == 1:
spaces_list[35].color = YELLOW
turn = 0
elif spaces_list[28].color == WHITE:
if turn == 0:
spaces_list[28].color = RED
turn = 1
elif turn == 1:
spaces_list[28].color = YELLOW
turn = 0您甚至可以通过可读性(尽管它仍然会更好)来推动您的运气,并让toggle_state返回,不管它是否做了更改:
def toggle_state(index: int) -> bool:
global turn
if spaces_list[index].color == WHITE:
if turn == 0:
spaces_list[index].color = RED
turn = 1
elif turn == 1:
spaces_list[index].color = YELLOW
turn = 0
return True
else:
return False现在,你可以:
toggle_state(35) or toggle_state(28) or . . .或者,就像
any(toggle_state(i) for i in [35, 28, . . .]不过,要注意的是:
== WHITE检查中以及在将这个数字传递给toggle_state时引用索引号。这种复制可能会导致错误,尽管它仍然比当前的解决方案更好。toggle_state会返回,不管它做了什么?因为我想不出更干净的方法。这是没有意义的,但它仍然允许更干净的代码。global turn,这立即表明代码很臭(确实如此)。理想情况下,像返回一个包括turn和spaces_list在内的状态是更合适的,但我认为这会削弱这里的要点:减少重复。除此之外,你有太多的神奇数字在周围浮动,没有明确的目的。理想情况下,您应该使用循环或类似的构造来简化该块。不过,只要你的一切都是僵化的、硬编码的,那就很难了。
https://codereview.stackexchange.com/questions/234286
复制相似问题