想要在X和O之间切换

时间:2019-04-03 03:33:27

标签: python pygame next

我想在pygame中在X和O之间切换。我做了一个while循环,如果图像变白了,应该重新启动。只是它不断返回相同的图像,而不是交替显示X或O。我似乎找不到任何问题。我涉及到使用next(alternator)在X和O之间拆分决策。

import matplotlib.pyplot as plt
import pygame
import sys
import pygame
import os
from PIL import Image

pygame.font.init()

size = 320, 240
black = 0, 0, 0
white = 255,255,255
red = 255, 0, 0

x1y1 = [(100, 0), (100, 300)]
x2y2 = [(200, 0), (200, 300)]
x3y3 = [(0, 100), (300, 100)]
x4y4 = [(0, 200), (300, 200)]

ser = []
for a in range(0,3): 
        for b in range(0,3):
            ser.append((a,b))             

def centroid(coord1, coord2): 
    xx = 50
    yy = 50
    coords = []
    for a in range(0,3): 
        for b in range(0,3):
            if a  == int(coord1) and b  == int(coord2):
                coords += tuple([xx + a*100, yy + b*100])
                return tuple(coords)

def fourCorners(a,b,length,width): 
    center = (a, b)
    corner3 = (int(a + length/2), int(b + width/2))
    corner2 = (int(a + length/2), int(b - width/2))
    corner4 = (int(a - length/2), int(b + width/2))
    corner1 = (int(a - length/2), int(b - width/2))
    return [corner1 ,corner2 ,corner3 ,corner4]

def withinRect(a,b,corners):
    if len(corners) != 4: 
        print('Pass a list parameter of length 4.')
    elif int(corners[0][0]) >= int(a) >= int(corners[1][0]) and int(corners[0][1]) >= int(b) >= int(corners[1][1]): 
        return True    


screen = pygame.display.set_mode((300,300))

screen.fill(white)
pygame.draw.line(screen, (0, 0, 128), x1y1[0], x1y1[1], 3)
pygame.draw.line(screen, (0, 0, 128), x2y2[0], x2y2[1], 3)
pygame.draw.line(screen, (0, 0, 128), x3y3[0], x3y3[1], 3)
pygame.draw.line(screen, (0, 0, 128), x4y4[0], x4y4[1], 3)

os.chdir('C:\\Users\\DELL\\Documents\\E-books\\Coding\\Projects')
os.path.abspath("X.png")
ximg = pygame.image.load("X.png")
ximg = pygame.transform.scale(ximg, (80,80))

os.path.abspath("O.png")
oimg = pygame.image.load("O.png")
oimg = pygame.transform.scale(oimg, (80,80))


def insert_x():
    global result 
    result = ()

    def alternate(): 
        while True:
            yield 0 
            yield 1


    alternator = alternate()    
    next(alternator)
    button = pygame.Rect(0,0,300,300)
    while True:
        ev = pygame.event.get()
        for event in ev:
            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()      
                x, y = event.pos
                evb = pygame.Rect(x,y,10,10)
                for val in ser:
                    va = tuple([100*x + 10 for x in val])
                    if (va[0] + 100 >= x >= va[0] and va[1] + 100 >= y >= va[1]):
                        result += va
                if (button.colliderect(evb)):
                    if next(alternator) == 1:
                        screen.blit(oimg,[result[0], result[1]])
                        next(alternator)
                        pygame.display.flip()
                        result = ()
                        continue
                    elif next(alternator) == 0:   
                        screen.blit(ximg,[result[0], result[1]])
                        next(alternator)
                        pygame.display.flip()
                        result = ()
                        continue
                    pygame.display.update()
        pygame.display.flip()
        continue

insert_x()

1 个答案:

答案 0 :(得分:0)

问题是next(alternator)的同步调用。 如果永久产生0/1/0/1 / ...,则两次调用它会返回相同的值-也就是说,它会导致它跳过第二个值。

考虑代码:

def alternate():
    while True:
        yield 0
        yield 1

alternator = alternate()
next(alternator)

for i in range( 10 ):
    if ( next(alternator) == 1 ):
        print("O ", end='')
        next(alternator)
    elif ( next(alternator) == 0 ):
        print("X ", end='')
        next(alternator)
print("")

哪个输出:

OOOOOOOOOO

然后考虑这种替代方法:

for i in range( 10 ):
    alt = next(alternator)
    if ( alt == 1 ):
        print("o", end='')
    elif ( alt == 0 ):
        print("x", end='')
print("")

哪个输出:

oxoxoxoxox

我喜欢,交流发电机是处理此触发器值的一种非常有趣的方式。