Wild SNEK appeared!
This commit is contained in:
parent
5df7c2b02e
commit
e357f5cfc8
1 changed files with 26 additions and 10 deletions
36
snek.py
36
snek.py
|
@ -5,9 +5,10 @@ class Snek():
|
||||||
self.length = 1
|
self.length = 1
|
||||||
self.positions = [((SCREEN_WIDTH / 2), (SCREEN_HEIGHT / 2))]
|
self.positions = [((SCREEN_WIDTH / 2), (SCREEN_HEIGHT / 2))]
|
||||||
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
|
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
|
||||||
self.bodyColor = (50, 243, 255)
|
self.bodyColor = (50, 243, 250)
|
||||||
self.borderColor = (50, 243, 255)
|
self.borderColor = (55, 247, 255)
|
||||||
self.score = 0
|
self.score = 0
|
||||||
|
self.highScore = 0
|
||||||
|
|
||||||
def get_head_position(self):
|
def get_head_position(self):
|
||||||
return self.positions[0]
|
return self.positions[0]
|
||||||
|
@ -29,10 +30,12 @@ class Snek():
|
||||||
if len(self.positions) > self.length:
|
if len(self.positions) > self.length:
|
||||||
self.positions.pop()
|
self.positions.pop()
|
||||||
|
|
||||||
def reset(self):
|
def reset(self): # snek die
|
||||||
self.length = 1
|
self.length = 1
|
||||||
self.positions = [((SCREEN_WIDTH / 2), (SCREEN_HEIGHT / 2))]
|
self.positions = [((SCREEN_WIDTH / 2), (SCREEN_HEIGHT / 2))]
|
||||||
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
|
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
|
||||||
|
if self.score > self.highScore:
|
||||||
|
self.highScore = self.score
|
||||||
self.score = 0
|
self.score = 0
|
||||||
|
|
||||||
def draw(self, surface):
|
def draw(self, surface):
|
||||||
|
@ -59,8 +62,8 @@ class Snek():
|
||||||
class Food():
|
class Food():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.position = (0, 0)
|
self.position = (0, 0)
|
||||||
self.bodyColor = (230, 38, 255)
|
self.bodyColor = (230, 38, 250)
|
||||||
self.borderColor = (255, 77, 255)
|
self.borderColor = (235, 43, 255)
|
||||||
self.randomize_position()
|
self.randomize_position()
|
||||||
|
|
||||||
def randomize_position(self):
|
def randomize_position(self):
|
||||||
|
@ -77,9 +80,11 @@ def drawGrid(surface):
|
||||||
if (x + y) % 2 == 0:
|
if (x + y) % 2 == 0:
|
||||||
r = pygame.Rect((x*GRIDSIZE, y*GRIDSIZE), (GRIDSIZE, GRIDSIZE))
|
r = pygame.Rect((x*GRIDSIZE, y*GRIDSIZE), (GRIDSIZE, GRIDSIZE))
|
||||||
pygame.draw.rect(surface, (10, 10, 10), r)
|
pygame.draw.rect(surface, (10, 10, 10), r)
|
||||||
|
pygame.draw.rect(surface, (15, 15, 15), r, 1)
|
||||||
else:
|
else:
|
||||||
rr = pygame.Rect((x*GRIDSIZE, y*GRIDSIZE), (GRIDSIZE, GRIDSIZE))
|
rr = pygame.Rect((x*GRIDSIZE, y*GRIDSIZE), (GRIDSIZE, GRIDSIZE))
|
||||||
pygame.draw.rect(surface, (10, 10, 10), rr)
|
pygame.draw.rect(surface, (10, 10, 10), rr)
|
||||||
|
pygame.draw.rect(surface, (15, 15, 15), rr, 1)
|
||||||
|
|
||||||
SCREEN_WIDTH = 768
|
SCREEN_WIDTH = 768
|
||||||
SCREEN_HEIGHT = 768
|
SCREEN_HEIGHT = 768
|
||||||
|
@ -106,9 +111,14 @@ def main():
|
||||||
|
|
||||||
snek = Snek()
|
snek = Snek()
|
||||||
food = Food()
|
food = Food()
|
||||||
score = snek.score
|
|
||||||
|
|
||||||
myfont = pygame.font.SysFont("monospace",16)
|
fontSize = 18
|
||||||
|
scoreFont = pygame.font.SysFont("monospace", fontSize)
|
||||||
|
scoreColor = (255, 255, 255)
|
||||||
|
scoreMarginLeft = 5
|
||||||
|
highScoreMarginTop = 10
|
||||||
|
scoreMarginTop = fontSize + (highScoreMarginTop * 2)
|
||||||
|
|
||||||
while (True):
|
while (True):
|
||||||
clock.tick(10)
|
clock.tick(10)
|
||||||
snek.handle_keys()
|
snek.handle_keys()
|
||||||
|
@ -116,13 +126,19 @@ def main():
|
||||||
snek.move()
|
snek.move()
|
||||||
if snek.get_head_position() == food.position:
|
if snek.get_head_position() == food.position:
|
||||||
snek.length += 1
|
snek.length += 1
|
||||||
score += 1
|
snek.score += 1
|
||||||
food.randomize_position()
|
food.randomize_position()
|
||||||
snek.draw(surface)
|
snek.draw(surface)
|
||||||
food.draw(surface)
|
food.draw(surface)
|
||||||
screen.blit(surface, (0, 0))
|
screen.blit(surface, (0, 0))
|
||||||
text = myfont.render("Score {0}".format(score), 1, (255, 255, 255))
|
|
||||||
screen.blit(text, (5, 10))
|
# scoreboard
|
||||||
|
score = scoreFont.render("Score {0}".format(snek.score), 1, scoreColor)
|
||||||
|
highScore = scoreFont.render("High Score {0}".format(snek.highScore), 1, scoreColor)
|
||||||
|
screen.blit(highScore, (scoreMarginLeft, highScoreMarginTop))
|
||||||
|
screen.blit(score, (scoreMarginLeft, scoreMarginTop))
|
||||||
|
|
||||||
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
main()
|
main()
|
Loading…
Add table
Reference in a new issue