我这样做是为了我的学校教育的一部分,而且无论我做什么,我都无法解决这个问题。我试图让点击的颜色按顺序显示为四行;然而,我得到的只是一排四种颜色,这不是我编码的。
#MMgame v 2
import pygame
import random
##pygame set up
pygame.init()
screen_width = 1200
screen_height = 800
mm_screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Mastermind')
pygame.display.update()
##global variables
code = []
user_choice = []
##colours
black = (0,0,0)
white = (255,255,255)
purple = (255,0,255)
green = (94,255,94)
orange = (255,125,38)
blue = (60,255,255)
red = (255,0,0)
yellow = (255,255,0)
bg_colour = (255,200,255)
b_colour = (200,191,231)
def user_selection(peg_colour,peg_position):
if peg_position == 1:
mm_screen.blit(peg_colour,(40,700))
pygame.display.update()
if peg_position == 2:
mm_screen.blit(peg_colour,(120,700))
pygame.display.update()
if peg_position == 3:
mm_screen.blit(peg_colour,(200,700))
pygame.display.update()
if peg_position == 4:
mm_screen.blit(peg_colour,(280,700))
pygame.display.update()
def codemaker():
global code
for colour in range(4):
colour_p = random.randrange(1,6)
if colour_p == 1:
code.append('purple')
elif colour_p == 2:
code.append('green')
elif colour_p == 3:
code.append('orange')
elif colour_p == 4:
code.append('blue')
elif colour_p == 5:
code.append('red')
elif colour_p == 6:
code.append('yellow')
print code
def play():
print 'playing mm'
play = True
while play == True:
peg_pos = 1
global user_choice
mm_screen.fill(bg_colour)
purple_p = pygame.image.load('purple_p.png').convert()
green_p = pygame.image.load('green_p.png').convert()
orange_p = pygame.image.load('orange_p.png').convert()
blue_p = pygame.image.load('blue_p.png').convert()
red_p = pygame.image.load('red_p.png').convert()
yellow_p = pygame.image.load('yellow_p.png').convert()
purplepeg = pygame.image.load('purplepeg.png').convert()
greenpeg = pygame.image.load('greenpeg.png').convert()
orangepeg = pygame.image.load('orangepeg.png').convert()
bluepeg = pygame.image.load('bluepeg.png').convert()
redpeg = pygame.image.load('redpeg.png').convert()
yellowpeg = pygame.image.load('yellowpeg.png').convert()
mm_screen.blit(purple_p,(928,614))
mm_screen.blit(green_p,(1014,614))
mm_screen.blit(orange_p,(1100,614))
mm_screen.blit(blue_p,(928,700))
mm_screen.blit(red_p,(1014,700))
mm_screen.blit(yellow_p,(1100,700))
pygame.display.update()
for event in pygame.event.get():
if event == pygame.QUIT:
pygame.quit()
quit()
#finding the position of the cursor
mp = pygame.mouse.get_pos()
#purple selected
if pygame.MOUSEBUTTONDOWN and 928 <= mp[0] <= 1008 and 614 <= mp[1] <= 694:
if peg_pos == 1:
user_selection(purplepeg,1)
user_choice.append('purple')
peg_pos += 1
if peg_pos == 2:
user_selection(purplepeg,2)
user_choice.append('purple')
peg_pos += 1
if peg_pos == 3:
user_selection(purplepeg,3)
user_choice.append('purple')
peg_pos += 1
if peg_pos == 4:
user_selection(purplepeg,4)
user_choice.append('purple')
peg_pos += 1
#green selected
if pygame.MOUSEBUTTONDOWN and 1014 <= mp[0] <= 1094 and 614 <= mp[1] <= 694:
if peg_pos == 1:
user_selection(greenpeg,1)
user_choice.append('green')
peg_pos += 1
if peg_pos == 2:
user_selection(greenpeg,2)
user_choice.append('green')
peg_pos += 1
if peg_pos == 3:
user_selection(greenpeg,3)
user_choice.append('green')
peg_pos += 1
if peg_pos == 4:
user_selection(greenpeg,4)
user_choice.append('green')
peg_pos += 1
#orange selected
if pygame.MOUSEBUTTONDOWN and 1100 <= mp[0] <= 1180 and 614 <= mp[1] <= 694:
if peg_pos == 1:
user_selection(orangepeg,1)
user_choice.append('orange')
peg_pos += 1
if peg_pos == 2:
user_selection(orangepeg,2)
user_choice.append('orange')
peg_pos += 1
if peg_pos == 3:
user_selection(orangepeg,3)
user_choice.append('orange')
peg_pos += 1
if peg_pos == 4:
user_selection(orangepeg,4)
user_choice.append('orange')
peg_pos += 1
#blue selected
if pygame.MOUSEBUTTONDOWN and 928 <= mp[0] <= 1008 and 700 <= mp[1] <= 780:
if peg_pos == 1:
user_selection(bluepeg,1)
user_choice.append('blue')
peg_pos += 1
if peg_pos == 2:
user_selection(bluepeg,2)
user_choice.append('blue')
peg_pos += 1
if peg_pos == 3:
user_selection(bluepeg,3)
user_choice.append('blue')
peg_pos += 1
if peg_pos == 4:
user_selection(bluepeg,4)
user_choice.append('blue')
peg_pos += 1
#red selected
if pygame.MOUSEBUTTONDOWN and 1014 <= mp[0] <= 1094 and 700 <= mp[1] <= 780:
if peg_pos == 1:
user_selection(redpeg,1)
user_choice.append('red')
peg_pos += 1
if peg_pos == 2:
user_selection(redpeg,2)
user_choice.append('red')
peg_pos += 1
if peg_pos == 3:
user_selection(redpeg,3)
user_choice.append('red')
peg_pos += 1
if peg_pos == 4:
user_selection(redpeg,4)
user_choice.append('red')
peg_pos += 1
#yellow selected
if pygame.MOUSEBUTTONDOWN and 1100 <= mp[0] <= 1180 and 700 <= mp[1] <= 780:
if peg_pos == 1:
user_selection(yellowpeg,1)
user_choice.append('yellow')
peg_pos += 1
if peg_pos == 2:
user_selection(yellowpeg,2)
user_choice.append('yellow')
peg_pos += 1
if peg_pos == 3:
user_selection(yellowpeg,3)
user_choice.append('yellow')
peg_pos += 1
if peg_pos == 4:
user_selection(yellowpeg,4)
user_choice.append('yellow')
peg_pos += 1
print user_choice
codemaker()
play()
答案 0 :(得分:1)
您的线路闪烁,因为您在编码代码后立即清除屏幕。要解决此问题,请执行以下操作:
...
mm_screen.fill(bg_colour)
#loop over code list here and blit every color
...
修改强>
创建一个类似的函数:
def printCode():
i = 0
for color in code:
if color == "green":
x = green_p #make a reference to your image
...
mm_screen.blit(x, (40+80*i, 700)) #finally blit the color
i += 1 #modify the above calculation for the next color
然后你称之为 AFTER mm_screen.fill(bg_colour)
。
但我会给你一些进一步的建议......
while play == True: #yours while play: #mine
没有必要将play
与True
进行比较,因为play
是一个布尔
并自动评估。
if pygame.MOUSEBUTTONDOWN ... #yours if event.type == pygame.MOUSEBUTTONDOWN: #mine
您的选择无法正常工作。在这种情况下,您已
比较event.type
,因为pygame.MOUSEBUTTONDOWN
不 a
布尔。它是一个恒定值(即使python不支持实常数)!
为了保存您和程序的时间,我建议您嵌套if-blocks,如:
... if event.type == pygame.MOUSEBUTTONDOWN: if 614 <= mp[1] <= 694: if 928 <= mp[0] <= 1008: ... if 1014 <= mp[0] <= 1094: ... ... if 700 <= mp[1] <= 780: if 928 <= mp[0] <= 1008: ...
也不要犹豫做简单的计算。通过执行以下操作可以轻松缩短此代码:
def user_selection(peg_colour,peg_position): mm_screen.blit(peg_colour,(40+80*(peg_position-1),700)) pygame.display.update() #yours: 13 lines | mine: 3 lines
不要在主循环中加载图像。 你在浪费表现!
#<----------------- to here ------- print 'playing mm' # | play = True # | while play: # | # | peg_pos = 1 # | global user_choice # | # | mm_screen.fill(bg_colour) # | # | #move loading from here -------
还有更多的事情你应该改变但是这个答案会变得太长了。^^正如我说你正在做的工作,你的电脑可以为你做的......