pygame.event和pygame.draw的问题

时间:2015-01-08 06:27:09

标签: pygame

我正在尝试制作两个8 * 8的屏幕。在一个我想要pygame绘制圆圈和在另一个三个矩形。

import pygame,sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1063,501))

pygame.draw.line(screen,(255,255,255),(0,0),(0,500))
pygame.draw.line(screen, (255,255,255), (0,0),(500,0))
pygame.draw.line(screen, (255,255,255), (500,0),(500,500))
pygame.draw.line(screen, (255,255,255), (500,500),(0,500))
for i in range(8):
    pygame.draw.line(screen, (255,255,255), (0,i*62.5+62.5),(500,i*62.5+62.5))
for i in range(8):
    pygame.draw.line(screen, (255,255,255), (i*62.5+62.5,0),(i*62.5+62.5,500))


pygame.draw.line(screen,(255,255,255),(562,0),(562,500))
pygame.draw.line(screen, (255,255,255), (562,0),(1062,0))
pygame.draw.line(screen, (255,255,255), (1062,0),(1062,500))
pygame.draw.line(screen, (255,255,255), (1062,500),(500,500))

for i in range(8):
    pygame.draw.line(screen, (255,255,255), (562,i*62.5+62.5),(1062,i*62.5+62.5))
for i in range(8):
    pygame.draw.line(screen, (255,255,255), (562 + i*62.5+62.5,0),(562+i*62.5+62.5,500))

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            x  = int(pos[0])
            y  = int(pos[1]) 
            x1 = int(pos[0]//62.5)
            y1 = int(pos[1]//62.5)
            x2 = int(x1*62.5+62.5/2)
            y2 = int(y1*62.5+62.5/2)
            print(x2,' ',y2)
            if x >563:
                pygame.draw.circle(screen, (255,0,0), (x2, y2), 10, 0)
                print(x1-8,' ',y1+1)

while True:
    if event.type == KEYDOWN :
        if x < 531 and event. key == pygame.K_1:
            pygame.draw.rect(screen, (0,255,0), (x2-27, y2-27, 55, 55))
            pygame.display.update()
        if x < 531 and event. key == pygame.K_2:
            pygame.draw.rect(screen, (0,255,0), (x2-27, y2-27, 55, 55))
            pygame.draw.rect(screen, (0,255,0), (x2+35, y2-27, 55, 55))
            pygame.display.update()
        if x < 531 and event. key == pygame.K_3:
            pygame.draw.rect(screen, (0,255,0), (x2-27, y2-27, 55, 55))
            pygame.draw.rect(screen, (0,255,0), (x2+35, y2-27, 55, 55))
            pygame.draw.rect(screen, (0,255,0), (x2-(27+62.5), y2-27, 55, 55))
            pygame.display.update()

    pygame.display.update()

我不知道这个问题我试图制作两个8 * 8的屏幕。在一个我想要pygame绘制圆圈和在另一个三个矩形。

1 个答案:

答案 0 :(得分:1)

你有2个while循环,将其更改为有效:

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            x  = int(pos[0])
            y  = int(pos[1])
            x1 = int(pos[0]//62.5)
            y1 = int(pos[1]//62.5)
            x2 = int(x1*62.5+62.5/2)
            y2 = int(y1*62.5+62.5/2)
            print(x2,' ',y2)
            if x >563:
                pygame.draw.circle(screen, (255,0,0), (x2, y2), 10, 0)
            print(x1-8,' ',y1+1)


        if event.type == KEYDOWN :
            if x < 531 and event. key == pygame.K_1:
                pygame.draw.rect(screen, (0,255,0), (x2-27, y2-27, 55, 55))

            if x < 531 and event. key == pygame.K_2:
                pygame.draw.rect(screen, (0,255,0), (x2-27, y2-27, 55, 55))
                pygame.draw.rect(screen, (0,255,0), (x2+35, y2-27, 55, 55))

            if x < 531 and event. key == pygame.K_3:
                pygame.draw.rect(screen, (0,255,0), (x2-27, y2-27, 55, 55))
                pygame.draw.rect(screen, (0,255,0), (x2+35, y2-27, 55, 55))
                pygame.draw.rect(screen, (0,255,0), (x2-(27+62.5), y2-27, 55, 55))

    pygame.display.update()

但是,除非您在按1,2或3之前单击窗口中的某个位置,否则会在x事件中定义MOUSEBUTTONDOWN时收到错误。要修复此问题并在鼠标指针下方绘制1,2或3个方块而不在窗口内单击,请将pos声明移出事件循环:

while True:

    pos = pygame.mouse.get_pos()
    x  = int(pos[0])
    y  = int(pos[1])
    x1 = int(pos[0]//62.5)
    y1 = int(pos[1]//62.5)
    x2 = int(x1*62.5+62.5/2)
    y2 = int(y1*62.5+62.5/2)

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == MOUSEBUTTONDOWN:
            print(x2,' ',y2)
            if x >563:
                pygame.draw.circle(screen, (255,0,0), (x2, y2), 10, 0)
                print(x1-8,' ',y1+1)


        if event.type == KEYDOWN :
            if x < 531 and event. key == pygame.K_1:
                pygame.draw.rect(screen, (0,255,0), (x2-27, y2-27, 55, 55))

            if x < 531 and event. key == pygame.K_2:
                pygame.draw.rect(screen, (0,255,0), (x2-27, y2-27, 55, 55))
                pygame.draw.rect(screen, (0,255,0), (x2+35, y2-27, 55, 55))

            if x < 531 and event. key == pygame.K_3:
                pygame.draw.rect(screen, (0,255,0), (x2-27, y2-27, 55, 55))
                pygame.draw.rect(screen, (0,255,0), (x2+35, y2-27, 55, 55))
                pygame.draw.rect(screen, (0,255,0), (x2-(27+62.5), y2-27, 55, 55))

    pygame.display.update()
相关问题