pygame椭圆出错了

时间:2018-06-27 18:32:14

标签: python pygame

我正在尝试制作一个简单的pygame井字游戏。以下代码尚未完成。它应该做的只是绘制一个3 * 3的网格,其中每个矩形都用红色的X和绿色的椭圆填充。

import pygame
import random
from pygame.locals import *


table = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

pygame.init()

# Set the width and height of the screen [width, height]
window_size = [500, 500]
screen = pygame.display.set_mode(window_size, HWSURFACE | DOUBLEBUF | 
RESIZABLE)

pygame.display.set_caption("My Game")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# -------- Main Program Loop -----------
while not done:
    # --- Main event loop
    mouse_pos = [0, 0]
    mouse_click = 0
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.MOUSEBUTTONUP:
            mouse_pos = event.pos
            mouse_click = event.button
        elif event.type == VIDEORESIZE:
            window_size = event.dict['size']
            screen = pygame.display.set_mode(event.dict['size'], HWSURFACE | 
DOUBLEBUF | RESIZABLE)

    # --- Game logic should go here
    if mouse_click == 1:
        for i in xrange(0, 2, 1):
            for a in xrange(0, 2, 1):
                if a+window_size[0]/3 > mouse_pos[1] > a and 
i+window_size[1]/3 > mouse_pos[0] > i:
                    table[i][a] = 1
    # --- Screen-clearing code goes here

    # Here, we clear the screen to white. Don't put other drawing commands
    # above this, or they will be erased with this command.

    # If you want a background image, replace this clear with blit'ing the
    # background image.
    screen.fill(WHITE)

    # --- Drawing code should go here
    for i in xrange(0, window_size[1], window_size[1]/3):
        for a in xrange(0, window_size[0], window_size[0]/3):
            pygame.draw.rect(screen, BLACK, [a, i, a+window_size[0]/3, 
i+window_size[1]/3], 5)

            pygame.draw.line(screen, RED, [a, i], [a+window_size[0]/3, 
i+window_size[1]/3], 5)
            pygame.draw.line(screen, RED, [a+window_size[0]/3, i], [a, 
i+window_size[1]/3], 5)

            pygame.draw.ellipse(screen, GREEN, [a, i, a+window_size[0]/3, 
i+window_size[1]/3], 5)
    # --- Go ahead and update the screen with what we've drawn.
        pygame.display.flip()

    # --- Limit to 60 frames per second
    clock.tick(60)

# Close the window and quit.
pygame.quit()

出于某些奇怪的原因,我只是没有正确地选择椭圆,也不知道为什么。

2 个答案:

答案 0 :(得分:1)

pygame.draw.ellipse(screen, GREEN, [a, i, a+window_size[0]/3, i+window_size[1]/3], 5)

需要是:

pygame.draw.ellipse(screen, GREEN, [a, i, window_size[0]/3, window_size[1]/3], 5)

Rect左,上,宽,高。

答案 1 :(得分:0)

查看documentation。您已经为除左上角以外的所有区域扩展了矩形; a+i+修饰符应该出现,因为您仅指单个正方形。

        pygame.draw.ellipse(
            screen, GREEN,
            [a, i, 
                 window_size[0]/3, 
                 window_size[1]/3], 
            5)