Commit 70dad483 by BellCodeEditor

save project

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