Last active
December 14, 2015 17:49
-
-
Save itsjef/5124883 to your computer and use it in GitHub Desktop.
tạo ra 1 cái bảng click click
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
#colors | |
black = (0 ,0 ,0 ) | |
white = (255,255,255) | |
blue = (0 ,0 ,255) | |
green = (0 ,255,0 ) | |
red = (255,0 ,0 ) | |
#width&height | |
width = 20 | |
height = 20 | |
#margin between cells | |
margin = 5 | |
#create a 2d array | |
grid = [] | |
for row in range(10): | |
grid.append([]) | |
for column in range(10): | |
grid[row].append(0) | |
grid[1][5] = 1 | |
#begin | |
pygame.init() | |
size = [255,255] | |
screen = pygame.display.set_mode(size) | |
pygame.display.set_caption('My Game') | |
done = False | |
clock = pygame.time.Clock() | |
# ---- Main program ---- | |
while done == False: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
done = True | |
if event.type == pygame.MOUSEBUTTONDOWN: | |
pos = pygame.mouse.get_pos() | |
column = pos[0]//(width+margin) #x value of Oxy | |
row = pos[1]//(height+margin) #y value of Oxy | |
if grid[row][column] == 1: | |
grid[row][column] = 0 | |
else: | |
grid[row][column] = 1 | |
print('Click',pos,'Grid coordinates: ',column, row) | |
#background | |
screen.fill(black) | |
for row in range(10): | |
for column in range(10): | |
color = white | |
if grid[row][column] == 1: | |
color = green | |
pygame.draw.rect(screen,color,[(margin+width)*column+margin,(margin+height)*row+margin,width,height]) | |
#pygame.draw.rect(Surface,color,[position,width,height]) | |
clock.tick(20) | |
pygame.display.flip() | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment