想在 X 和 O 之间交替
Want to alternate between X's and O's
我想在 pygame 中交替使用 X 和 O。我做了一个 while 循环,如果图像被 blitted,它应该重新启动。只是它一直返回相同的图像,而不是交替返回 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()
问题是 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
虽然交流发电机是处理这种翻转值的一种非常有趣的方式,但我喜欢它。
我想在 pygame 中交替使用 X 和 O。我做了一个 while 循环,如果图像被 blitted,它应该重新启动。只是它一直返回相同的图像,而不是交替返回 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()
问题是 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
虽然交流发电机是处理这种翻转值的一种非常有趣的方式,但我喜欢它。