Commit 1a75697c by BellCodeEditor

save project

parent eb57d78b
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
import random
import math
from pygame import locals
# 初始化pygame,为使用pygame做准备
pygame.init()
# 创建一个窗口
screen=pygame.display.set_mode((780,600))
FPSCLOCK=pygame.time.Clock()
backgroung=pygame.image.load('bg.png')
right=pygame.image.load('right.png')
left=pygame.image.load('left.png')
up=pygame.image.load('up.png')
down=pygame.image.load('down.png')
food=pygame.image.load('apple.png')
body=pygame.image.load('body.png')
x,y=90,0
xa,ya=300,420
applenum=0
position=[(0,0),(30,0),(60,0),(x,y)]
setheading="right"
snake_head=right
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
if x>=780:
exit()
if y>=600:
exit()
if y<0:
exit()
if x<0:
exit()
if setheading=="right":
x+=30
elif setheading=="left":
x-=30
elif setheading=="up":
y-=30
else :
y+=30
screen.blit(backgroung,(0,0))
screen.blit(food,(xa,ya))
if xa==x and ya==y:
screen.blit(food,(900,900))
position.append((x,y))
applenum+=1
print("苹果数量:"+str(applenum))
xa=math.floor(random.randint(60,720)/30)*30
ya=math.floor(random.randint(60,540)/30)*30
else:
position.pop(0)
position.append((x,y))
screen.blit(snake_head,position[-1])
for i in range(len(position)-1):
screen.blit(body,position[i])
pygame.display.update()
FPSCLOCK.tick(3)
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