Я пытаюсь сделать простую игру Tic Tac Toe. Следующий код не закончен. Все, что он должен сделать, это нарисовать сетку 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()
По какой-то странной причине, я просто не правильно понял эллипс и не знаю почему.