Commit b17e1c4c by BellCodeEditor

save project

parent cc3b049a
Showing with 31 additions and 5 deletions

46.1 KB | W: | H:

22.5 KB | W: | H:

bg.png
bg.png
bg.png
bg.png
  • 2-up
  • Swipe
  • Onion skin
import pygame import pygame
from pygame import locals from pygame import locals
import random
# 初始化pygame,为使用硬件做准备 # 初始化pygame,为使用硬件做准备
pygame.init() pygame.init()
...@@ -12,9 +12,16 @@ background = pygame.image.load('bg.png') ...@@ -12,9 +12,16 @@ background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') right = pygame.image.load('right.png')
food = pygame.image.load('apple.png') food = pygame.image.load('apple.png')
body = pygame.image.load('body.png') body = pygame.image.load('body.png')
left = pygame.image.load('left.png')
down = pygame.image.load('down.png')
up = pygame.image.load('up.png')
my_font=pygame.font.Font('neuropol.ttf',18)
Score=0
x,y=240,120 x,y=240,120
apple_x=360
apple_y=300
setheading="right" setheading="right"
snake_head=right
position = [(180,90),(180,120),(210,120),(x,y)] position = [(180,90),(180,120),(210,120),(x,y)]
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
...@@ -24,12 +31,16 @@ while True: ...@@ -24,12 +31,16 @@ while True:
if event.type==locals.KEYDOWN: if event.type==locals.KEYDOWN:
if event.key==locals.K_d and setheading !="left": if event.key==locals.K_d and setheading !="left":
setheading = "right" setheading = "right"
snake_head = right
if event.key==locals.K_a and setheading !="right": if event.key==locals.K_a and setheading !="right":
setheading = "left" setheading = "left"
snake_head = left
if event.key==locals.K_w and setheading !="down": if event.key==locals.K_w and setheading !="down":
setheading = "up" setheading = "up"
snake_head = up
if event.key==locals.K_s and setheading !="up": if event.key==locals.K_s and setheading !="up":
setheading = "down" setheading = "down"
snake_head = down
if setheading == "right": if setheading == "right":
x+=30 x+=30
elif setheading == "left": elif setheading == "left":
...@@ -39,16 +50,30 @@ while True: ...@@ -39,16 +50,30 @@ while True:
else: else:
y+=30 y+=30
position.append((x,y)) position.append((x,y))
position.pop(0) if x==apple_x and y==apple_y:
num1=random.randint(1,22)
num2=random.randint(1,16)
apple_x = num1 * 30 - 30
apple_Y = num2 * 30 - 30
Score +=10
else:
position.pop(0)
if x<0 or x>660 or y<0 or y>480:
exit()
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# 将贪吃蛇画上去 # 将贪吃蛇画上去
screen.blit(right, position[-1]) screen.blit(snake_head, position[-1])
# 将贪吃蛇的身体画上去 # 将贪吃蛇的身体画上去
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(food, (360, 300)) screen.blit(food, (apple_x, apple_y))
info="Score:"+str(Score)
text=my_font.render(info,True,(0,0,0))
screen.blit(text,(540,10))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPSCLOCK.tick(3) FPSCLOCK.tick(3)
\ No newline at end of file
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