我的问题有点复杂,因为我在脑海里跑来跑去,但我会尽力解释它。我有2个python代码,一个是我用Python为Mastermind游戏创建的,另一个是在Pygame中为Mastermind板创建的。我的问题很简单:如何将这两个代码组合成1以使其从Pygame中播放而不是我通常得到的命令提示符窗口?
如果这听起来很乱,我很抱歉,但这就是我的问题。我只想拿出我的Python代码并将其实现到Pygame代码中,让游戏按照预期运行。
下面是游戏的代码:
import random
class InvalidMove(Exception):pass
class Game:
def __init__(self):
self.colors=('r','g','b','y')
self.to_guess=[random.choice(self.colors) for i in range(4)]
def match_guess(self,guess):
if len(guess)!=len(self.to_guess) or [g for g in guess if g not in self.colors]:
raise InvalidMove()
ret=[0,0]
usedindexes=[]
for i,g in enumerate(guess):
if g==self.to_guess[i]:
ret[0]+=1
usedindexes.append(i)
for i,g in enumerate(guess):
if i in usedindexes: continue
for j,c in enumerate(self.to_guess):
if c==g and j not in usedindexes:
ret[1]+=1
usedindexes.append(j)
return ret
class UI:
def make_move(self):
guess=raw_input("Guess: ")
return guess.split()
def main(self):
print("The game begins...")
print("Possible colors (enter first letter): [r]ed [g]reen [b]lue [y]ellow")
print("Enter your guess like: r g b y")
g=Game()
while True:
guess=self.make_move()
try:
bp,wp=g.match_guess(guess)
except InvalidMove:
print("Invalid guess, try again")
continue
print("Black pegs %s"%bp)
print("White pegs %s"%wp)
if bp==4:
print("You won!")
if __name__=="__main__":
u=UI()
u.main()
这是我在Pygame中制作的电路板的代码:
import pygame
from pygame.locals import *
def draw_current(men, turn, spacing, corner):
current = len(men) - 1
pos = corner[0] + current * spacing[0], turn * spacing[1] + corner[1]
screen.blit(images[men[-1]], pos)
images = { K_r: pygame.image.load('red.png'), K_g: pygame.image.load('green.png'),
K_b: pygame.image.load('blue.png'), K_y: pygame.image.load('yellow.png'),
K_SPACE: pygame.image.load('empty.png') }
pygame.init()
SCREEN_SIZE = (640, 480)
background_image_filename = 'mastermind_board.jpg'
screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
background = pygame.image.load(background_image_filename).convert()
screen.blit(background, (0, 0))
pygame.display.update()
men = []
margin = 5, 3
spacing = [x + m for m, x in zip(margin, images[K_r].get_size())]
corner = 74, 74
turn = 0
quit = False
while not quit:
for event in pygame.event.get():
if event.type == QUIT:
quit = True
break
if event.type == KEYUP:
if event.key in images:
#print event.key
men.append(event.key)
# update
draw_current(men,turn, spacing, corner)
if len(men) == 4:
turn += 1
men = []
pygame.display.update()
elif event.key in (K_q, K_ESCAPE):
quit = True
break
pygame.quit()
任何和所有的帮助/建议都将不胜感激。
答案 0 :(得分:1)
我相信你知道,你不能把它全部放在一个文件中并让它运行。你需要封装Mastermind游戏,这样你就可以从pygame循环中运行/更新它。如果你将Mastermind游戏逻辑保存在一个单独的文件中并只是导入它,那就更清晰了,但这不是必需的。
这是一些半假性代码:
import pygame
class Mastermind(object):
def update(self, *args):
pass #do stuff
pygame.init()
while not quit:
if event:
if event == "quit":
quit = True
else:
#update the game
Mastermind.update(args)
#update the board
pygame.display.update()
pygame.quit()
当然,这对你不起作用,但我希望我解释的想法能够实现。
答案 1 :(得分:0)
步骤1.修复您的电路板,使其成为正确的类定义。
步骤2.写出import
棋盘和游戏的第三个文件。
他们现在“合并”了。
现在你必须努力修复游戏以使用你建造的新的,精美的棋盘。