python 贪吃蛇
我们的文件结构:
---snake
--run.py
run.py:
import pygame as pg
import sys
import random as rd
from pygame import *
pg.init()
SC_W = 600
SC_H = 600
SC = pg.display.set_mode((SC_W, SC_H))
Timer = pg.time.Clock()
START = True
FPS = 5
SnakeDirection = 'R'
pg.display.set_caption("贪吃蛇(Snake)")
snakeBodyWidth = 20
snakeBodyHeight = 20
snakeBodyColor = (255, 255, 0)
radomStartX = rd.randint(6, 20)
radomStartY = rd.randint(6, 20)
bodyMagin = 5
snakeInnerBodyColor = (255, 255, 255)
snakeStartPos = [
[radomStartX*snakeBodyWidth, radomStartX*snakeBodyHeight],
[(radomStartX-1)*snakeBodyWidth, radomStartX*snakeBodyHeight],
[(radomStartX-2)*snakeBodyWidth, radomStartX*snakeBodyHeight]
]
foodWidth = 20
foodHeight = 20
footStartX = rd.randint(0, 29)
footStartY = rd.randint(0, 29)
footStartPos = [
[footStartX*foodWidth, footStartY*foodHeight],
[rd.randint(0, 29)*foodWidth, rd.randint(0, 29)*foodHeight]
]
Score = 0
font = pygame.font.SysFont("arial", 16)
while START:
foodColor = (rd.randint(0, 255), rd.randint(0, 255), rd.randint(0, 255))
for event in pg.event.get():
if event.type == pg.QUIT:
START = False
break
elif event.type == KEYDOWN:
if event.key == K_a or event.key == K_LEFT:
if SnakeDirection != 'R':
SnakeDirection = 'L'
elif event.key == K_d or event.key == K_RIGHT:
if SnakeDirection != 'L':
SnakeDirection = 'R'
elif event.key == K_w or event.key == K_UP:
if SnakeDirection != 'D':
SnakeDirection = 'U'
elif event.key == K_s or event.key == K_DOWN:
if SnakeDirection != 'U':
SnakeDirection = 'D'
SC.fill((255, 250, 240))
userScore = font.render(str(Score), True, (255, 0, 0))
SC.blit(userScore, (0, 0))
if snakeStartPos[0][0] < 0 or snakeStartPos[0][0] >= 600 or snakeStartPos[0][1] < 0 or snakeStartPos[0][1] >= 600:
pg.display.set_caption("Game over")
userScore = font.render("Game Over", True, (255, 0, 0))
SC.fill((255, 250, 240))
SC.blit(userScore, (200, 250))
footStartPos.pop(0)
footStartPos.pop(-1)
pg.display.update()
Timer.tick(FPS+5)
if footStartPos[0] in snakeStartPos:
footStartPos[0] = [rd.randint(
0, 29)*foodWidth, rd.randint(0, 29)*foodHeight]
Score += 1
snakeStartPos.append([-20, -20])
elif footStartPos[1] in snakeStartPos:
footStartPos[1] = [(rd.randint(0, 29)-1)*foodWidth,
(rd.randint(0, 29))*foodHeight]
Score += 1
snakeStartPos.append([-20, -20])
foodRect = pg.Rect(footStartPos[0][0],
footStartPos[0][1], foodWidth, foodHeight)
foodDrawRect = pg.draw.rect(SC, foodColor, foodRect)
foodRect = pg.Rect(footStartPos[1][0],
footStartPos[1][1], foodWidth, foodHeight)
foodDrawRect = pg.draw.rect(SC, foodColor, foodRect)
for snackPos in snakeStartPos:
snakeRect = pg.Rect(snackPos[0], snackPos[1],
snakeBodyWidth, snakeBodyHeight)
snakeDrawRect = pg.draw.rect(SC, snakeBodyColor, snakeRect)
snackInnerRect = pg.Rect(
snackPos[0]+4, snackPos[1]+4, snakeBodyWidth-8, snakeBodyHeight-8)
snakeInnerDraw = pg.draw.rect(SC, snakeInnerBodyColor, snackInnerRect)
if SnakeDirection == 'R':
NEWHEAD = [snakeStartPos[0][0]+20, snakeStartPos[0][1]]
elif SnakeDirection == 'L':
NEWHEAD = [snakeStartPos[0][0]-20, snakeStartPos[0][1]]
elif SnakeDirection == 'U':
NEWHEAD = [snakeStartPos[0][0], snakeStartPos[0][1]-20]
elif SnakeDirection == 'D':
NEWHEAD = [snakeStartPos[0][0], snakeStartPos[0][1]+20]
snakeStartPos.insert(0, NEWHEAD)
snakeStartPos.pop(-1)
pg.display.update()
Timer.tick(FPS)
成品(暂时没有windows版)
|