Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matt end game screen #1

Merged
merged 6 commits into from
Mar 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .models.py.swo
Binary file not shown.
12 changes: 7 additions & 5 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
from models import *
import time

square_width = 10 #pixels
square_width = 10 # pixels
grid_width = 51
pixels_wide = square_width * grid_width
ms_per_block = 6 # screen refreshes per move
score_font_size = 14

if __name__ == '__main__':
pygame.init()
size = (pixels_wide, pixels_wide)
size = (pixels_wide, int(pixels_wide*1.08) )# + score_font_size + 14)
screen = pygame.display.set_mode(size)

model = GameModel(grid_width)
Expand All @@ -20,13 +21,14 @@
count = 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
controller.handle_event(event)
# if event.type == pygame.QUIT:
# running = False
running = controller.handle_event(event)
count += 1
if count == ms_per_block:
model.update_snake()
model.check_collision()
count = 0
view.draw()
time.sleep(.001)
pygame.QUIT
112 changes: 89 additions & 23 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,27 @@ def map_origin():
row.append(cube[x][y][z])
grid.append(row)
return grid
#xy = (1, 1, 0)
#yz = (0, 1, 1)
#xz = (1, 0, 1)
#if tuple(np.abs(np.add(up, right))) == xy:
# z = self.model.plane.depth
# newgrid = [[y[z] for y in x] for x in cube]
#if tuple(np.abs(np.add(up, right))) == xz:
# y = self.model.plane.depth
# newgrid = [[z for z in x[y]] for x in cube]
#if tuple(np.abs(np.add(up, right))) == yz:
# x = self.model.plane.depth
# newgrid = [[z for z in y] for y in cube[x]]
#return newgrid


def draw(self):
plane = self.get_slice()
self.screen.fill(pygame.Color('black'))

# Place the appropriate rectangle for every element of the grid (None, SnakeBodyPart, Wall or Food)
for i in range(len(plane)):
for j in range(len(plane[i])):
get_rect = pygame.Rect(*self.coord_to_pixels((i, j)))
try: # try to get color attribute, if
color = plane[i][j].color
color = plane[i][j].color if not self.model.snake.dead else plane[i][j].dead_color
except:
color = 'black'
pygame.draw.rect(self.screen, pygame.Color(color), get_rect)
color = pygame.Color('black')
pygame.draw.rect(self.screen, color, get_rect)

self.print_score()

# Blit the death screen if the snake is dead
if self.model.snake.dead:
self.print_death_text('Wasted', 64)

pygame.display.update()

def coord_to_pixels(self, coords):
Expand All @@ -67,14 +62,73 @@ def coord_to_pixels(self, coords):
width, height = (self.square_size, self.square_size)
return (left, top, width, height)

def print_death_text(self, death_message, font_size):


dead_font = pygame.font.Font(None, font_size)
options_font = pygame.font.Font(None, font_size/2)
options_color = (181, 180, 103)

background = pygame.Surface(self.screen.get_size())
background = background.convert()

# Wasted
text = dead_font.render(death_message, 1, (200, 0, 0, 1))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
textpos.centery = background.get_rect().centerx # centerx such that the text is at the center of the square field
self.screen.blit(text, textpos)

# Play Again
replay = options_font.render("R: Play Again", 1, options_color)
replay_pos = replay.get_rect()
replay_pos.centerx = background.get_rect().centerx
replay_pos.centery = background.get_rect().centerx + font_size*3/4 # centerx such that the text is at the center of the square field
self.screen.blit(replay, replay_pos)

# Play Again
quit = options_font.render("Q: Quit", 1, options_color)
quit_pos = quit.get_rect()
quit_pos.centerx = background.get_rect().centerx
quit_pos.centery = background.get_rect().centerx + font_size*3/4 + font_size/2 # centerx such that the text is at the center of the square field
self.screen.blit(quit, quit_pos)


def print_score(self):
score_str = 'Score: ' + str(self.model.score2)

screen_size = self.screen.get_size()
font = pygame.font.Font(None, int(0.06*screen_size[0]))#20) # How to pass font size?
background = pygame.Surface( ( screen_size[0], screen_size[1]-screen_size[0] ) )
background = background.convert()

text = font.render(score_str, 1, (255, 255, 255, 1))
textpos = text.get_rect()
textpos.x = int(0.03*screen_size[0])
textpos.centery = sum(screen_size)/2
self.screen.blit(text, textpos)



class GameController(object):
def __init__(self, model):
self.model = model

def handle_event(self, event):
if event.type == pygame.QUIT:
return False

if event.type != pygame.KEYDOWN:
return
return True

if self.model.snake.dead:
if event.key == pygame.K_q:
# Quit
return False
elif event.key == pygame.K_r:
# Restart the game.
self.model.__init__()
return True

directions = ['up', 'left', 'down', 'right']

Expand All @@ -86,8 +140,11 @@ def handle_event(self, event):

if event.key in direction_keys:
self.model.snake.change_direction(control_dict[event.key])
self.model.score2 -= 1
if event.key in orientation_keys:
self.model.change_orientation(orient_dict[event.key])
self.model.score2 -= 1
return True


class GameModel(object):
Expand All @@ -101,6 +158,8 @@ def __init__(self, dimensions=50):
self.plane = Plane()
#self.make_first_walls()
self.make_food()
self.score = 0
self.score2 = 0

def make_first_walls(self):
for i in range(len(self.grid.grid)):
Expand Down Expand Up @@ -189,6 +248,8 @@ def check_collision(self):
self.snake.grow()
del self.foods[i]
self.make_food()
self.score += 10 # Have the score based off of len(snake)?
self.score2 += 10

# Check for snake collisions
for part in self.snake.get_list()[1:]: # Don't check against the head
Expand Down Expand Up @@ -235,10 +296,12 @@ def turn_left(self):


class SnakeBodyPart(object):
color = 'green'
def __init__(self, x, y, z, index=0):

color = pygame.Color('green')
dead_color = pygame.Color(78 ,78 ,78 ,255)

def __init__(self, x, y, z):
self.x, self.y, self.z = (x, y, z)
self.index = index


class Snake(LinkedList):
Expand Down Expand Up @@ -275,7 +338,8 @@ def die(self):


class Food(object):
color = 'yellow'
color = pygame.Color('yellow')
dead_color = pygame.Color(210, 210, 210, 255)

def __init__(self, x, y, z):
self.x, self.y, self.z = (x, y, z)
Expand All @@ -285,7 +349,9 @@ def __repr__(self):


class Wall(object):
color = 'red'

color = pygame.Color('red')
dead_color = pygame.Color(128, 128, 128, 255)

def __init__(self, x, y, z):
self.x, self.y, self.z = (x, y, z)
Expand Down