сделал загатовку, откуда сделаем старт микро-игры
This commit is contained in:
@@ -0,0 +1,10 @@
|
|||||||
|
from modules.Game import Game
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
game = Game()
|
||||||
|
game.start()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
|
class Board:
|
||||||
|
def __init__(self, width, height, screen):
|
||||||
|
self.width = width
|
||||||
|
self.height = height
|
||||||
|
self.board = [[1] * width for _ in range(height)]
|
||||||
|
self.left = 10
|
||||||
|
self.top = 10
|
||||||
|
self.cell_size = 30
|
||||||
|
self.screen = screen
|
||||||
|
|
||||||
|
def render(self):
|
||||||
|
for y in range(self.height):
|
||||||
|
for x in range(self.width):
|
||||||
|
pygame.draw.rect(self.screen, pygame.Color(255, 255, 255), (
|
||||||
|
x * self.cell_size + self.left, y * self.cell_size + self.top, self.cell_size, self.cell_size),
|
||||||
|
self.board[y][x])
|
||||||
|
|
||||||
|
def set_view(self, left, top, cell_size):
|
||||||
|
self.left = left
|
||||||
|
self.top = top
|
||||||
|
self.cell_size = cell_size
|
||||||
|
|
||||||
|
def on_click(self, cell_coords):
|
||||||
|
for x in range(self.height):
|
||||||
|
self.board[x][cell_coords[1]] = (self.board[x][cell_coords[1]] + 1) % 2
|
||||||
|
for y in range(self.width):
|
||||||
|
self.board[cell_coords[0]][y] = (self.board[cell_coords[0]][y] + 1) % 2
|
||||||
|
self.board[cell_coords[0]][cell_coords[1]] = (self.board[cell_coords[0]][cell_coords[1]] + 1) % 2
|
||||||
|
|
||||||
|
def get_cell(self, mouse_pos):
|
||||||
|
if self.left <= mouse_pos[1] < self.left + self.height * self.cell_size and \
|
||||||
|
self.top <= mouse_pos[0] < self.top + self.width * self.cell_size:
|
||||||
|
return int((mouse_pos[1] - self.left) / self.cell_size), int((mouse_pos[0] - self.top) / self.cell_size)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_click(self, mouse_pos):
|
||||||
|
cell = self.get_cell(mouse_pos)
|
||||||
|
if cell is not None:
|
||||||
|
self.on_click(cell)
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
from pymongo import MongoClient
|
||||||
|
|
||||||
|
|
||||||
|
class UseDB:
|
||||||
|
def __init__(self, collection_name):
|
||||||
|
self.series_collection = None
|
||||||
|
self.client = MongoClient('localhost', 27017)
|
||||||
|
self.db = self.client['stepik']
|
||||||
|
self.series_collection = self.db[collection_name]
|
||||||
|
|
||||||
|
def find_document(self, elements):
|
||||||
|
return [x for x in self.series_collection.find(elements)]
|
||||||
|
|
||||||
|
def insert_document(self, data):
|
||||||
|
return self.series_collection.insert_one(data).inserted_id
|
||||||
|
|
||||||
|
def update_document(self, query_elements, new_values):
|
||||||
|
self.series_collection.update_one(query_elements, {'$set': new_values})
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import pygame
|
||||||
|
from modules.Board import Board
|
||||||
|
|
||||||
|
|
||||||
|
class Game:
|
||||||
|
def __init__(self):
|
||||||
|
pygame.init()
|
||||||
|
size = 500, 500
|
||||||
|
self.screen = pygame.display.set_mode(size)
|
||||||
|
pygame.display.set_caption('Чёрное в белое и наоборот')
|
||||||
|
self.board = Board(5, 7, self.screen)
|
||||||
|
self.board.set_view(100, 100, 50)
|
||||||
|
self.running = True
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
while self.running:
|
||||||
|
self.event()
|
||||||
|
self.render()
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
def event(self):
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
self.running = False
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
self.board.get_click(event.pos)
|
||||||
|
|
||||||
|
def render(self):
|
||||||
|
self.screen.fill((0, 0, 0))
|
||||||
|
self.board.render()
|
||||||
|
pygame.display.flip()
|
||||||
Reference in New Issue
Block a user