我正在使用pygame创建一个连接四游戏。我创建了一个名为playerOne的函数,其中包含该播放器的所有代码。我有一个名为playerTwo的第二个函数,其中包含第二个玩家的所有代码。在我的游戏循环中,我想调用第一个函数,让该玩家移动,然后让第二个玩家移动。这应该循环直到游戏结束。问题在于,无论我尝试多少,它都只运行一个功能,永远不会进入下一个功能。在下面的代码中,我删除了大部分功能代码,因为它只是重复自己。感谢先进的任何查看此代码的人!
import pygame as pg
#window Size, Sprites, Colors, etc.
display = pg.display.set_mode((500, 500))
white = (255, 255 , 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
board = pg.image.load('board.png')
chipRed = pg.Rect(10, 10, 10, 10)
chipBlack = pg.Rect(10, 10, 10, 10)
display.fill(white)
display.blit(board, (100, 100)) # draws the blank board
playerOneOver = False # Defaults to having player one's turn be NOT over
#board in list form, so the game can later store were chips are located
boardRowOne = ['b', 'b', 'b', 'b', 'b', 'b', 'b']
boardRowTwo = ['b', 'b', 'b', 'b', 'b', 'b', 'b']
boardRowThree = ['b', 'b', 'b', 'b', 'b', 'b', 'b']
boardRowFour = ['b', 'b', 'b', 'b', 'b', 'b', 'b']
boardRowFive = ['b', 'b', 'b', 'b', 'b', 'b', 'b']
boardRowSix = ['b', 'b', 'b', 'b', 'b', 'b', 'b']
#Player ones move
def playerOne():
#player settings
chipColor = red
#display player one is up
pg.draw.rect(display, chipColor, (230, 5, 20, 20))
#Colum selection
mousePressed = pg.mouse.get_pressed()
mousePos = pg.mouse.get_pos()
for event in pg.event.get():
if event.type == pg.QUIT: #Exit Out
pg.quit()
exit()
if columOne.collidepoint(mousePos) and mousePressed[0]: # if colum one was selected
if 'b' in boardRowSix[0]: # if nothing is in row 6 colum one
del(boardRowSix[0])
boardRowSix.insert(0, 'r')
pg.draw.rect(display, chipColor, (110, 320, 20, 20))
playerOneOver = True
elif 'b' in boardRowFive[0]: # if nothing is in row 5 colum one
del(boardRowFive[0])
boardRowFive.insert(0, 'r')
pg.draw.rect(display, chipColor, (110, 280, 20, 20))
playerOneOver = True
#The above elif is repated for every other colum and row, but I cut it out for my post
#player two move
def playerTwo():
#player settings
chipColor = black
#display player one is up
pg.draw.rect(display, chipColor, (230, 5, 20, 20))
#Colum selection
mousePressed = pg.mouse.get_pressed()
mousePos = pg.mouse.get_pos()
for event in pg.event.get():
if event.type == pg.QUIT: #Exit Out
pg.quit()
exit()
if columOne.collidepoint(mousePos) and mousePressed[0]: # if colum one was selected
if 'b' in boardRowSix[0]: # if nothing is in row 6 colum one
del(boardRowSix[0])
boardRowSix.insert(0, 'B')
pg.draw.rect(display, chipColor, (110, 320, 20, 20))
elif 'b' in boardRowFive[0]: # if nothing is in row 5 colum one
del(boardRowFive[0])
boardRowFive.insert(0, 'B')
pg.draw.rect(display, chipColor, (110, 280, 20, 20))
#The above elif is repated for every other colum and row, but I cut it out for my post
#game loop
while True:
#Draw the colum selection
columOne = pg.Rect(105, 70, 30, 30)
columTwo = pg.Rect(150, 70, 30, 30)
columThree = pg.Rect(190, 70, 30, 30)
columFour = pg.Rect(235, 70, 30, 30)
columFive = pg.Rect(280, 70, 30, 30)
columSix = pg.Rect(320, 70, 30, 30)
columSeven = pg.Rect(365, 70, 30, 30)
pg.draw.rect(display, blue, (105, 70, 30, 30))
pg.draw.rect(display, blue, (150, 70, 30, 30))
pg.draw.rect(display, blue, (190, 70, 30, 30))
pg.draw.rect(display, blue, (235, 70, 30, 30))
pg.draw.rect(display, blue, (280, 70, 30, 30))
pg.draw.rect(display, blue, (320, 70, 30, 30))
pg.draw.rect(display, blue, (365, 70, 30, 30))
if playerOneOver == False: # only run if player ones move is over
playerOne()
if playerOneOver == True: # run when player ones move is over
playerTwo()
pg.display.flip() # updates the drawings
#print the board list
print(boardRowOne)
print(boardRowTwo)
print(boardRowThree)
print(boardRowFour)
print(boardRowFive)
print(boardRowSix)
答案 0 :(得分:3)
尝试在while循环中打印playerOneOver
。您会发现它总是False
。
这是一个范围问题。您正在尝试更新playerOneOver
函数内的playerOne()
变量,但由于范围实际发生的是您的函数中正在创建新的playerOneOver
,它不会更新你在开始时定义的那个。
这是理想的行为。您可以阅读很多关于范围的内容,例如here。
更新变量的更pythonic方法是存储函数的返回值。请考虑以下代码来解决您的问题。
a = 'woo'
def update_bad():
a = 'wee'
def update_good():
return 'wee'
print 'Before bad update:',a
update_bad()
print 'After bad update:',a
print 'Before good update:',a
a = update_good()
print 'After good update:',a