Commit 14b6d92b by BellCodeEditor

save project

parent c714b5db
apple.png

2.05 KB

bg.png

22.5 KB

body.png

1.4 KB

down.png

2.01 KB

left.png

2.07 KB

File added
right.png

2.05 KB

import pygame
from pygame import locals
from random import *
pygame.init()
screen = pygame.display.set_mode((660, 480))
FPSCLOCK = pygame.time.Clock()
background = pygame.image.load('bg.png')
right = pygame.image.load('right.png')
food = pygame.image.load('apple.png')
body = pygame.image.load('body.png')
left = pygame.image.load('left.png')
up = pygame.image.load('up.png')
down = pygame.image.load('down.png')
my_font = pygame.font.Font("neuropol.ttf",18)
x, y = 240, 120
position = [(300,420),(270,420),(240,420),(210,420),(180,420),(150,420),(120,420),(90,420),(60,420),(30,420),(0,420),(0,390),(0,360),(0,330),(0,300),(0,270),(0,240),(0,210),(0,180),(0,150),(0,120),(0,90),(0,60),(0,30),(0,0),(30,0),(60,0),(90,0),(120,0),(150,0),(180,0),(180,30),(180,60),(180,90),(180,120),(210,120),(x, y)]
setheading = "right"
snake_head = right
apple_x = randint(0,630)
apple_y = randint(0,430)
changdu=4
score=0
while apple_x%30!=0 or apple_y%30!=0:
apple_x = randint(0,630)
apple_y = randint(0,430)
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_RIGHT and setheading != "left":
setheading = 'right'
snake_head = right
if event.key == locals.K_LEFT and setheading != "right":
setheading = 'left'
snake_head = left
if event.key == locals.K_UP and setheading != "down":
setheading = 'up'
snake_head = up
if event.key == locals.K_DOWN and setheading != "up":
setheading = 'down'
snake_head = down
info = "Score:"+str(score)
text = my_font.render(info,True,(0,0,0))
if setheading == "right":
x += 30
elif setheading == "left":
x -= 30
elif setheading == "up":
y -= 30
else:
y += 30
if x<0 or x>630 or y<0 or y>460:
exit()
if x==apple_x and y==apple_y:
changdu+=1
score+=1
apple_x = randint(0,630)
apple_y = randint(0,430)
while apple_x%30!=0 or apple_y%30!=0:
apple_x = randint(0,630)
apple_y = randint(0,430)
position.append((x, y))
if not changdu>=len(position):
position.pop(0)
screen.blit(background, (0, 0))
screen.blit(snake_head, position[-1])
for i in range(len(position)-1):
screen.blit(body, position[i])
screen.blit(food, (apple_x, apple_y))
pygame.display.update()
FPSCLOCK.tick(6)
\ No newline at end of file
up.png

2.05 KB

Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment