Created
December 17, 2019 11:44
-
-
Save robivirt/58b2ef0576f8ceecfcbda162aadf3bd8 to your computer and use it in GitHub Desktop.
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 | |
class Board: | |
def __init__(self, width, height): | |
self.width = width | |
self.height = height | |
self.board = [[0 for _ in range(width)] for _ in range(height)] | |
self.gray = [] | |
self.player = None | |
self.left = 0 | |
self.top = 0 | |
self.cell_size = 10 | |
self.speed = 50 | |
def set_view(self, left, top, cell_size): | |
self.left = left | |
self.top = top | |
self.cell_size = cell_size | |
def render(self): | |
for i, j in self.gray: | |
pygame.draw.rect(screen, [128, 128, 128], [i, j, 50, 10]) | |
if self.player is not None: | |
pygame.draw.rect(screen, [0, 0, 255], [self.player[0], self.player[1], | |
20, 20]) | |
x, y = self.player | |
self.speed = 50 | |
for i, j in self.gray: | |
left = max(x, i) | |
top = min(y + 20, j + 10) | |
right = min(x + 20, i + 50) | |
bottom = max(y, j) | |
widt = right - left | |
heigh = top - bottom | |
if widt > 0 and heigh > 0: | |
self.speed = 0 | |
def get_click1(self, mouse_pos): | |
cell = self.get_cell1(mouse_pos) | |
self.on_click1(cell) | |
def get_cell1(self, mouse_pos): | |
x, y = mouse_pos | |
if x >= 500 or y >= 500: | |
return None | |
return x, y | |
def on_click1(self, kor): | |
if kor is not None: | |
x, y = kor | |
self.gray.append([x, y]) | |
def get_click2(self, mouse_pos): | |
cell = self.get_cell2(mouse_pos) | |
self.on_click2(cell) | |
def get_cell2(self, mouse_pos): | |
x, y = mouse_pos | |
if x >= 500 or y >= 500: | |
return None | |
return x, y | |
def on_click2(self, kor): | |
if kor is not None: | |
x, y = kor | |
self.player = [x, y] | |
self.speed = 50 | |
def move_down(self): | |
if self.player is not None: | |
self.player[1] += self.speed // 50 | |
def move_right(self): | |
if self.player is not None: | |
self.player[0] += 10 | |
def move_left(self): | |
if self.player is not None: | |
self.player[0] -= 10 | |
pygame.init() | |
size = width, height = (500, 500) | |
screen = pygame.display.set_mode(size) | |
screen.fill((0, 0, 0)) | |
n, m = 500, 500 | |
board = Board(n, m) | |
running = True | |
is_next = False | |
speed = 50 | |
fps = 0 | |
font = pygame.font.Font(None, 25) | |
text = font.render("My text",True, pygame.Color("black")) | |
screen.blit(text, [250, 250]) | |
timer = pygame.time.Clock() | |
while running: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: | |
board.get_click1(event.pos) | |
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 3: | |
board.get_click2(event.pos) | |
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT: | |
board.move_right() | |
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT: | |
board.move_left() | |
fps += timer.tick() | |
if fps / 20 >= 1: | |
board.move_down() | |
fps %= 20 | |
fps %= 20 | |
screen.fill((0, 0, 0)) | |
board.render() | |
pygame.display.flip() | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
aaaw