Commit 70dad483 by BellCodeEditor

save project

parent 9ae46e77
Showing with 18 additions and 19 deletions
import pygame
from pygame import locals
# 初始化pygame,为使用硬件做准备
import random
pygame.init()
# 创建一个窗口
screen = pygame.display.set_mode((660, 480))
weee=pygame.time.Clock()
# 背景
background = pygame.image.load('bg.png')
right = pygame.image.load('right.png')
food = pygame.image.load('apple.png')
......@@ -19,40 +15,42 @@ x,y=240,120
postion=[(180,90),(180,120),(210,120),(x,y)]
sb="right"
dsb=right
apple_x=360
apple_y=300
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type==locals.KEYDOWN:
elif event.type==locals.KEYDOWN:
if event.key==locals.K_RIGHT and sb!="left":
sb="right"
dsb=right
x+=30
if event.key==locals.K_LEFT and sb!="right":
sb="left"
dsb=left
x-=30
if event.key==locals.K_UP and sb!="down":
sb="up"
dsb=up
y-=30
if event.key==locals.K_DOWN and sb!="up":
sb="down"
dsb=down
y+=30
if sb=="right":
x+=30
elif sb=="left":
x-=30
elif sb=="up":
y-=30
else:
y+=30
if x==apple_x and y==apple_y:
apple_x=random.randint(0,660)
apple_y=random.randint(480,0)
postion.append((x,y))
postion.pop(0)
screen.blit(background, (0, 0))
# 将贪吃蛇画上去
screen.blit(right, (x,y))
screen.blit(dsb, postion[-1])
for i in range(len(postion)-1):
screen.blit(body,(postion[i]))
# screen.blit(body, (210, 120))
# screen.blit(body, (180, 120))
# screen.blit(body, (180, 90))
# screen.blit(food, (360, 300))
x=x+30
# 刷新画面
screen.blit(food,(apple_x,apple_y))
pygame.display.update()
weee.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