Commit 26151405 by BellCodeEditor

save project

parent c98e148d
Showing with 29 additions and 4 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random
# 初始化pygame,为使用硬件做准备 # 初始化pygame,为使用硬件做准备
pygame.init() pygame.init()
...@@ -12,19 +12,43 @@ background = pygame.image.load('bg.png') ...@@ -12,19 +12,43 @@ 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')
head='right'
Time=pygame.time.Clock() Time=pygame.time.Clock()
x,y=(240,120) x,y=(240,120)
list=[(360, 300),(180, 120),(210, 120),(x,y)] list=[(360, 300),(180, 120),(210, 120),(x,y)]
apple_x,apple_y=300,360
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:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
if event.type==locals.KEYDOWN:
if event.key == locals.K_DOWN and head != 'up':
head='down'
if event.key == locals.K_UP and head != 'down':
head='up'
if event.key == locals.K_RIGHT and head != 'left':
head='right'
if event.key == locals.K_LEFT and head != 'right':
head='left'
screen.blit(background,(0,0)) screen.blit(background,(0,0))
screen.blit(right,(x,y)) screen.blit(right,(x,y))
x+=30 if head=='right':
x +=30
if head=='left':
x -=30
if head=='up':
y -=30
if head=='down':
y +=30
if x==apple_x and y==apple_y:
apple_x=random.randint(1,21)*30
apple_y=random.randint(1,15)*30
list.append((x,y)) list.append((x,y))
list.pop(0) list.pop(0)
# 将背景图画上去 # 将背景图画上去
...@@ -38,7 +62,7 @@ while True: ...@@ -38,7 +62,7 @@ while True:
# screen.blit(body, (180, 120)) # screen.blit(body, (180, 120))
# screen.blit(body, (180, 90)) # screen.blit(body, (180, 90))
# 将果实画上去 # 将果实画上去
screen.blit(food, (360, 300)) screen.blit(food, (apple_x, apple_y))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
Time.tick(3) Time.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