初始点击事件发生后,Pygame冻结

时间:2019-04-03 05:03:51

标签: python pygame

我想在井字游戏中在X和O之间切换。唯一的问题是,在放置O的初始单击之后,它会冻结。它不会继续下去。我期望while循环从if语句开始“继续”,但是好像它不再注册任何事件。另外,我使用的交流发电机似乎还不错,我是在重新启动事件排序规则,但应该将其注册??

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)

问题在于结构

if next(alternator) == 1:
      # [...]
elif next(alternator) == 0:
      # [...]

alternator第一次在if语句中求值。如果不是yield 1而是yield 0,则不满足条件,并评估elif语句。但是在elif语句中alternator被第二次求值,现在无论如何yield 1。 这导致elif条件从不满足。该应用程序似乎已挂起,因为在这种状态下,if条件和elif条件都不会在任何时候得到满足。

alternate-if语句之前对elif进行一次评估,以解决此问题:

a = next(alternator)
if a == 1:
    screen.blit(oimg,[result[0], result[1]])
    result = ()
elif a == 0:   
    screen.blit(ximg,[result[0], result[1]])
    result = ()

顺便说一下,事件循环中的continue语句和pygame.display.flip()调用是多余的。当消息队列为空并且主循环末尾的单个pygame.display.flip()调用已足够时,事件循环将终止。