ISN correction du snake

This commit is contained in:
Bertrand Benjamin 2018-03-21 09:10:42 +03:00
parent 1263972519
commit 3746998fff
4 changed files with 479 additions and 10 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

View File

@ -1,22 +1,180 @@
import pygame
"""
Snake pour l'ISN
"""
import pygame
from random import randint
def draw_grid():
""" Dessine la grille """
for i in range(COLUMN+1):
pygame.draw.line(screen, WHITE, (i*CELLSIZE, 0), (i*CELLSIZE, ROW*CELLSIZE))
for i in range(ROW+1):
pygame.draw.line(screen, WHITE, (0, i*CELLSIZE), (COLUMN*CELLSIZE, i*CELLSIZE))
def draw_snake(snake):
""" Dessine le serpent """
for i, j in snake:
top_left = (i * CELLSIZE, j * CELLSIZE)
pygame.draw.rect(screen, GREEN, (*top_left, CELLSIZE, CELLSIZE))
def draw_cherry(cherry):
""" Dessine les cherry (bonus) """
center = (int((cherry[0] + 0.5) * CELLSIZE), int((cherry[1] + 0.5) * CELLSIZE))
radius = int(0.5*CELLSIZE)
pygame.draw.circle(screen, RED, center, radius)
def draw_right(score):
""" Dessine la partie droite de la fenête """
width_right_bar = s_width - COLUMN*CELLSIZE
top_left_right_bar = (COLUMN*CELLSIZE, 0)
title = TITLEFONT.render("SNAKE", True, WHITE)
title_rect = title.get_rect()
title_rect.centerx = int(top_left_right_bar[0] + width_right_bar/2)
title_rect.centery = 50
screen.blit(title, title_rect)
score_text = "Score " + str(score)
score = TEXTFONT.render(score_text, True, WHITE)
score_rect = score.get_rect()
score_rect.centerx = int(top_left_right_bar[0] + width_right_bar/2)
score_rect.centery = 100
screen.blit(score, score_rect)
def random_place(row, col):
""" Retourne un lieu aléatoire sur la grille """
return [randint(0, row-1), randint(0, col-1)]
def move_snake(snake, snake_direction, growing):
""" Retourne la nouvelle position du snake
Si growing est True, on ne supprime par le dernier élément de la queue pour que le serpent grandisse
L'idée est de trouver les coordonnées de la nouvelle tête (new_head) puis de coller le corp du serpent (en supprimant la dernière partie si l'on veut que le serpent de grandisse pas.
"""
new_head = [snake[0][0] + snake_direction[0], snake[0][1] + snake_direction[1]]
if growing:
return [new_head] + snake
else:
# [:-1] permet de dire que l'on veut la liste sauf le dernier élément.
return [new_head] + snake[:-1]
def is_out(snake):
""" Retourne True si le snake est sorti de la grille """
# le snake[0] est la tête du serpent.
return snake[0][0] > COLUMN or \
snake[0][0] < 0 or \
snake[0][1] > ROW or\
snake[0][1] < 0
def is_eating_queue(snake):
""" Retourne True si le serpent se mort la queue """
# On se demande si la tête du serpent est dans (in) la queue dans ce cas il se mort la queue
return snake[0] in snake[1:]
def is_eating(snake, cherry):
""" Retroune True si le snake mange la cherry"""
return snake[0] == cherry
# Call this function so the Pygame library can initialize itself
pygame.init()
largeur = 600
hauteur = 400
windowSurface = pygame.display.set_mode((largeur, hauteur), 0,32)
# --- Globals ---
# Colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
# font
TITLEFONT = pygame.font.SysFont(None, 48)
TEXTFONT = pygame.font.SysFont(None, 30)
# THE GRID
ROW = 20
COLUMN = 20
# CELL
CELLSIZE = 20
# Create an 800x600 sized screen
s_width = COLUMN * CELLSIZE + 200
s_height = ROW * CELLSIZE + 1
screen = pygame.display.set_mode([s_width, s_height])
# Set the title of the window
pygame.display.set_caption('Snake')
clock = pygame.time.Clock()
# The snake
snake = [[5, 5], [4, 5], [3, 5]]
snake_direction = [1, 0]
# The cherry
cherry = random_place(ROW, COLUMN)
# The score
score = 0
growing = False
running = True
while running:
pygame.draw.line(windowSurface, WHITE, (0,10), (600,10))
pygame.display.update()
# Event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
# On empèche que le snake fasse demi tour
if event.key == pygame.K_UP:
if snake_direction != [0, 1]:
snake_direction = [0, -1]
elif event.key == pygame.K_DOWN:
if snake_direction != [0, -1]:
snake_direction = [0, 1]
elif event.key == pygame.K_LEFT:
if snake_direction != [1, 0]:
snake_direction = [-1, 0]
elif event.key == pygame.K_RIGHT:
if snake_direction != [-1, 0]:
snake_direction = [1, 0]
# Move the snake
if growing:
snake = move_snake(snake, snake_direction, True)
growing = False
else:
snake = move_snake(snake, snake_direction, False)
# Draw elements
screen.fill(BLACK)
draw_grid()
draw_right(score)
draw_snake(snake)
draw_cherry(cherry)
# Flip screen
pygame.display.flip()
if is_eating(snake, cherry):
score = score + 1
growing = True
cherry = random_place(ROW, COLUMN)
# loosing!
if is_out(snake) or is_eating_queue(snake):
screen.fill(BLACK)
text = TITLEFONT.render('You loose!!', True, BLACK,WHITE)
textRect = text.get_rect()
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
screen.blit(text, textRect)
pygame.display.flip()
pygame.time.wait(1000)
running = False
# Pause
clock.tick(2)
pygame.quit()

BIN
ISN/pygame/snake_expl.pdf Normal file

Binary file not shown.

311
ISN/pygame/snake_expl.svg Normal file
View File

@ -0,0 +1,311 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
viewBox="0 0 801.33331 570.66669"
height="570.66669"
width="801.33331"
id="svg869"
version="1.1"
sodipodi:docname="snake_expl.svg"
inkscape:version="0.92.2 2405546, 2018-03-11">
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="1920"
inkscape:window-height="1052"
id="namedview48"
showgrid="false"
inkscape:zoom="1.1697"
inkscape:cx="323.7925"
inkscape:cy="206.6537"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1"
inkscape:current-layer="svg869" />
<metadata
id="metadata875">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title />
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs873">
<marker
inkscape:stockid="Arrow1Mend"
orient="auto"
refY="0.0"
refX="0.0"
id="Arrow1Mend"
style="overflow:visible;"
inkscape:isstock="true">
<path
id="path986"
d="M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z "
style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1"
transform="scale(0.4) rotate(180) translate(10,0)" />
</marker>
<marker
orient="auto"
refY="0.0"
refX="0.0"
id="marker8051"
style="overflow:visible;">
<path
id="path8049"
d="M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z "
style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1"
transform="scale(0.8) rotate(180) translate(12.5,0)" />
</marker>
<marker
orient="auto"
refY="0.0"
refX="0.0"
id="marker7709"
style="overflow:visible">
<path
id="path7707"
d="M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z "
style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1"
transform="scale(0.8) translate(12.5,0)" />
</marker>
<marker
orient="auto"
refY="0.0"
refX="0.0"
id="marker7395"
style="overflow:visible">
<path
id="path7393"
d="M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z "
style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1"
transform="scale(0.8) translate(12.5,0)" />
</marker>
<marker
orient="auto"
refY="0.0"
refX="0.0"
id="marker7379"
style="overflow:visible;">
<path
id="path7377"
d="M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z "
style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1"
transform="scale(0.8) rotate(180) translate(12.5,0)" />
</marker>
<marker
orient="auto"
refY="0.0"
refX="0.0"
id="marker7119"
style="overflow:visible;">
<path
id="path7117"
d="M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z "
style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1"
transform="scale(0.8) rotate(180) translate(12.5,0)" />
</marker>
<marker
style="overflow:visible;"
id="marker1407"
refX="0.0"
refY="0.0"
orient="auto">
<path
transform="scale(0.8) rotate(180) translate(12.5,0)"
style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1"
d="M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z "
id="path1405" />
</marker>
<marker
style="overflow:visible"
id="marker1237"
refX="0.0"
refY="0.0"
orient="auto">
<path
transform="scale(0.8) translate(12.5,0)"
style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1"
d="M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z "
id="path1235" />
</marker>
<marker
style="overflow:visible;"
id="Arrow1Lend"
refX="0.0"
refY="0.0"
orient="auto">
<path
transform="scale(0.8) rotate(180) translate(12.5,0)"
style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1"
d="M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z "
id="path928" />
</marker>
<marker
style="overflow:visible"
id="Arrow1Lstart"
refX="0.0"
refY="0.0"
orient="auto">
<path
transform="scale(0.8) translate(12.5,0)"
style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1"
d="M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z "
id="path925" />
</marker>
<marker
style="overflow:visible"
id="Arrow1Mstart"
refX="0.0"
refY="0.0"
orient="auto">
<path
transform="scale(0.4) translate(10,0)"
style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1;fill:#000000;fill-opacity:1"
d="M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z "
id="path931" />
</marker>
</defs>
<image
sodipodi:absref="/home/lafrite/Cours/Prof/Enseignements/2017-2018/ISN/pygame/fig/snake_corr.png"
xlink:href="fig/snake_corr.png"
y="109.27795"
x="144.4212"
id="image877"
preserveAspectRatio="none"
height="352.11078"
width="494.43591" />
<path
id="path879"
d="M 145.96232,87.695672 H 637.31601"
style="fill:none;stroke:#000000;stroke-width:1.00157475;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-start:url(#Arrow1Lstart);marker-end:url(#Arrow1Lend)" />
<path
id="path881"
d="M 648.04522,135.73483 V 462.01432"
style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;marker-start:url(#marker1237);marker-end:url(#marker1407)" />
<g
id="g917">
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:16px;line-height:1;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;-inkscape-font-specification:'Consolas, Liberation Mono, Menlo, Courier, monospace';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"
x="132.58679"
y="143.47269"
id="text885"><tspan
x="132.58679"
y="143.47269"
id="tspan887"
style="font-size:13.33333302px;line-height:1">0</tspan><tspan
x="132.58679"
y="159.47269"
style="font-size:13.33333302px;line-height:1"
id="tspan893">1</tspan><tspan
x="132.58679"
y="175.47269"
id="tspan889"
style="font-size:13.33333302px;line-height:1">2</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:13.33333302px;line-height:1.25;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;-inkscape-font-specification:'Consolas, Liberation Mono, Menlo, Courier, monospace';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"
x="115.35721"
y="456.22137"
id="text897"><tspan
id="tspan895"
x="115.35721"
y="456.22137">ROW</tspan></text>
</g>
<text
id="text901"
y="479.93118"
x="152.74573"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:16px;line-height:1.25;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;-inkscape-font-specification:'Consolas, Liberation Mono, Menlo, Courier, monospace';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"
xml:space="preserve"><tspan
y="491.57919"
x="152.74573"
id="tspan899" /></text>
<g
id="g923">
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:16px;line-height:1.25;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;-inkscape-font-specification:'Consolas, Liberation Mono, Menlo, Courier, monospace';letter-spacing:-1.29999995px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"
x="148.18616"
y="474.45969"
id="text905"><tspan
id="tspan903"
x="148.18616"
y="474.45969">0 1 2</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:16px;line-height:1.25;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;-inkscape-font-specification:'Consolas, Liberation Mono, Menlo, Courier, monospace';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"
x="439.54291"
y="474.12369"
id="text909"><tspan
id="tspan907"
x="439.54291"
y="474.12369">COLUMN</tspan></text>
</g>
<text
id="text1743"
y="77.378532"
x="357.61792"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:16px;line-height:1.25;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;-inkscape-font-specification:'Consolas, Liberation Mono, Menlo, Courier, monospace';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"
xml:space="preserve"><tspan
y="77.378532"
x="357.61792"
id="tspan1741">s_width</tspan></text>
<text
id="text1747"
y="288.85159"
x="671.25879"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:16px;line-height:1.25;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;-inkscape-font-specification:'Consolas, Liberation Mono, Menlo, Courier, monospace';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"
xml:space="preserve"><tspan
y="288.85159"
x="671.25879"
id="tspan1745">s_height</tspan></text>
<text
id="text7577"
y="274.75037"
x="64.289993"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:13.33333302px;line-height:1.25;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;-inkscape-font-specification:'Consolas, Liberation Mono, Menlo, Courier, monospace';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"
xml:space="preserve"><tspan
y="274.75037"
x="64.289993"
id="tspan7575">CELLSIZE</tspan></text>
<path
style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;marker-start:url(#Arrow1Mstart);marker-end:url(#Arrow1Mend)"
d="m 132.51261,263.9641 v 14.31991"
id="path915"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;marker-start:url(#marker1237);marker-end:url(#marker7119)"
d="M 479.61017,497.14356 H 636.06055"
id="path1319"
inkscape:connector-curvature="0" />
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:13.33333302px;line-height:1.25;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;-inkscape-font-specification:'Consolas, Liberation Mono, Menlo, Courier, monospace';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"
x="496.70856"
y="519.37146"
id="text1323"><tspan
sodipodi:role="line"
id="tspan1321"
x="496.70856"
y="519.37146">width_right_bar</tspan></text>
</svg>

After

Width:  |  Height:  |  Size: 13 KiB