Created
July 23, 2018 23:44
-
-
Save raymondberg/72d685d3038f7749f9e9ee37d9d12f4d to your computer and use it in GitHub Desktop.
Tucker's Snake Game
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, sys | |
from pygame.locals import * | |
pygame.init() | |
board_color = (168, 173, 181) | |
line_color = (100, 100, 100) | |
DISPLAYSURF = pygame.display.set_mode((1000, 500)) | |
pygame.display.set_caption('G R A P H G A M E !') | |
pygame.font.init() # you have to call this at the start, | |
# if you want to use this module. | |
myfont = pygame.font.SysFont('Comic Sans MS', 14) | |
class Board: | |
def __init__(self): | |
self.x_start = 10 | |
self.y_start = 10 | |
self.width = 480 | |
self.height = 480 | |
self.grid_width = 16 | |
def draw_lines(self): | |
offset = self.x_start + self.grid_width | |
while offset < 480: | |
# Draw vertical lines | |
pygame.draw.line(DISPLAYSURF, line_color, (offset,10), (offset,490), 1) | |
# Draw horizontal lines | |
pygame.draw.line(DISPLAYSURF, line_color, (10,offset), (490,offset), 1) | |
offset = offset + self.grid_width | |
def draw(self): | |
rect = pygame.Rect(self.x_start, self.y_start, self.width, self.height) | |
pygame.draw.rect(DISPLAYSURF, board_color, rect) | |
self.draw_lines() | |
def get_coordinates_from_location(self, game_board_x, game_board_y): | |
""" Returns a tuple with the actual x and y coordinate (tuple) for a given | |
game board position""" | |
## input(1, 1) --> output ( 10 + 16, 490 - 16 ) -> output( 26, 474) | |
return (game_board_x * self.grid_width + self.x_start, | |
(self.y_start + self.height) - game_board_y * self.grid_width) | |
class DirectionPicker: | |
def __init__(self): | |
self.x_start = 600 | |
self.y_start = 250 | |
self.width = 240 | |
self.height = 240 | |
self.grid_width = 80 | |
def draw_lines(self): | |
#fix this area, need new variables for x and y. | |
# Draw vertical lines for direction picker | |
pygame.draw.line(DISPLAYSURF, line_color, (680,250), (680,490), 1) | |
pygame.draw.line(DISPLAYSURF, line_color, (760,250), (760,490), 1) | |
# Draw horizontal lines for direction picker | |
pygame.draw.line(DISPLAYSURF, line_color, (840,330), (600,330), 1) | |
pygame.draw.line(DISPLAYSURF, line_color, (840,410), (600,410), 1) | |
def draw(self): | |
rect = pygame.Rect(self.x_start, self.y_start, self.width, self.height) | |
pygame.draw.rect(DISPLAYSURF, board_color, rect) | |
self.draw_lines() | |
def which_square_is_position_in(self, mouse_position): | |
x,y = mouse_position | |
direction = [ | |
['up_left', 'up', 'up_right'], | |
['left', None, 'right'], | |
['down_left', 'down', 'down_right'], | |
] | |
col = row = -1 | |
col = (x - self.x_start) // self.grid_width | |
row = (y - self.y_start) // self.grid_width | |
if col in [0,1,2] and row in [0,1,2]: | |
return direction[row][col] | |
class Snake: | |
def __init__(self): | |
self.color = (242, 0, 0) | |
self.x_location = 1 | |
self.y_location = 1 | |
self.previous_positions = [] | |
self.save_position() | |
def draw(self, board): | |
pygame.draw.circle( | |
DISPLAYSURF, | |
self.color, | |
board.get_coordinates_from_location(self.x_location, self.y_location), | |
4, | |
) | |
pygame.draw.line( | |
DISPLAYSURF, | |
self.color, | |
board.get_coordinates_from_location(self.previous_x, self.previous_y), | |
board.get_coordinates_from_location(self.x_location, self.y_location), | |
1, | |
) | |
def save_position(self): | |
self.previous_x = self.x_location | |
self.previous_y = self.y_location | |
## self.previous_positions.append((self.x_location, self.y_location)) | |
## hint what is self.previous_positions[-1]? | |
def move(self, direction): | |
self.save_position() | |
if direction == 'up': | |
self.y_location = self.y_location + 1 | |
if direction == 'down': | |
self.y_location = self.y_location - 1 | |
if direction == 'right': | |
self.x_location = self.x_location + 1 | |
if direction == 'left': | |
self.x_location = self.x_location - 1 | |
if direction == 'up_left': | |
self.x_location = self.x_location - 1 | |
self.y_location = self.y_location + 1 | |
if direction == 'up_right': | |
self.x_location = self.x_location + 1 | |
self.y_location = self.y_location + 1 | |
if direction == 'down_left': | |
self.x_location = self.x_location - 1 | |
self.y_location = self.y_location - 1 | |
if direction == 'down_right': | |
self.x_location = self.x_location + 1 | |
self.y_location = self.y_location - 1 | |
## Board -> board | |
## Snake -> snake | |
## board ----> snake.draw() | |
## snake (self.x, self.y) -----> board.get() | |
board = Board() | |
direction_picker = DirectionPicker() | |
snake = Snake() | |
last_message = "" | |
while True: # main game loop | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
pygame.quit() | |
sys.exit() | |
elif event.type == MOUSEBUTTONDOWN: | |
last_message = f'{event.pos}' | |
textsurface = myfont.render(last_message, False, (200, 203, 250)) | |
DISPLAYSURF.blit(textsurface,(500,0)) | |
direction = direction_picker.which_square_is_position_in(event.pos) | |
if direction: | |
snake.move(direction) | |
elif event.type == MOUSEBUTTONUP: | |
textsurface = myfont.render(last_message, False, (0, 0, 0)) | |
DISPLAYSURF.blit(textsurface,(500,0)) | |
board.draw() | |
direction_picker.draw() | |
snake.draw(board) | |
## always goes last | |
pygame.display.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment