Commit 7f93663d by BellCodeEditor

save project

parent 7105f301
Showing with 18 additions and 3 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random
# 初始化pygame,为使用pygame做准备 # 初始化pygame,为使用pygame做准备
pygame.init() pygame.init()
...@@ -12,12 +12,16 @@ left=pygame.image.load("left.png") ...@@ -12,12 +12,16 @@ left=pygame.image.load("left.png")
right=pygame.image.load("right.png") right=pygame.image.load("right.png")
up=pygame.image.load("up.png") up=pygame.image.load("up.png")
FPS=pygame.time.Clock() FPS=pygame.time.Clock()
my_fond=pygame.font.Font("neuropol.ttf",18)
# 创建一个窗口 # 创建一个窗口
x,y=240,120 x,y=240,120
position=[(180,90),(180,120),(210,120),(x,y)] position=[(180,90),(180,120),(210,120),(x,y)]
apple_x=360
apple_y=300
screen=pygame.display.set_mode((800,600)) screen=pygame.display.set_mode((800,600))
setheading='right' setheading='right'
snake_head=right snake_head=right
score=0
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type==locals.QUIT: if event.type==locals.QUIT:
...@@ -44,13 +48,24 @@ while True: ...@@ -44,13 +48,24 @@ while True:
elif setheading =="down": elif setheading =="down":
y+=30 y+=30
position.append((x,y)) position.append((x,y))
if x==apple_x and y==apple_y:
a=random.randint(1,22)
d=random.randint(1,16)
apple_x=30*a-30
apple_y=30*d-30
else:
position.pop(0) position.pop(0)
if x<0 or x>630 or y<0 or y>450:
exit()
screen.blit(back,(0,0)) screen.blit(back,(0,0))
screen.blit(snake_head,(x,y)) screen.blit(snake_head,(x,y))
for i in range(len(position)-1): for i in range(len(position)-1):
screen.blit(body,position[i]) screen.blit(body,position[i])
screen.blit(frut,(apple_x,apple_y))
screen.blit(frut,(90,60)) info="score:"+str(score)
text=my_fond.render(info,True,(0,0,0))
screen.blit(text,(540,10))
# screen.blit(body,(210,120)) # screen.blit(body,(210,120))
# screen.blit(body,(180,120)) # screen.blit(body,(180,120))
# screen.blit(body,(150,120)) # screen.blit(body,(150,120))
......
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