Commit 50f5b5cf by BellCodeEditor

auto save

parent 77f8b56e
Showing with 16 additions and 36 deletions
import pygame import pygame
import random import random
from pygame import locals from pygame import locals
# 初始化pygame,为使用硬件做准备
pygame.init() pygame.init()
# 创建一个窗口
screen = pygame.display.set_mode((661, 481)) screen = pygame.display.set_mode((661, 481))
FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) FPSCLOCK = 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')
body = pygame.image.load('body.png') # 身体 body = pygame.image.load('body.png')
left = pygame.image.load('left.png') # 头 朝左 left = pygame.image.load('left.png')
up = pygame.image.load('up.png') # 头 朝上 up = pygame.image.load('up.png')
down = pygame.image.load('down.png') # 头 朝下 down = pygame.image.load('down.png')
x2,x3,x4,x5,x6,x7,x8,x9,x10,x11=330 x2=330
y2,y3,y4,y5,y6,y7,y8,y9,y10,y11=330 y2=330
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)]
...@@ -31,7 +27,7 @@ snake_head = right ...@@ -31,7 +27,7 @@ snake_head = right
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.type == locals.KEYDOWN:
if event.key == locals.K_RIGHT and setheading != "left": if event.key == locals.K_RIGHT and setheading != "left":
...@@ -47,7 +43,6 @@ while True: ...@@ -47,7 +43,6 @@ while True:
setheading = 'down' setheading = 'down'
snake_head = down snake_head = down
# 设置贪吃蛇的头部坐标
if setheading == "right": if setheading == "right":
x += 30 x += 30
elif setheading == "left": elif setheading == "left":
...@@ -56,38 +51,23 @@ while True: ...@@ -56,38 +51,23 @@ while True:
y -= 30 y -= 30
else: else:
y += 30 y += 30
position.append((x, y)) position.append((x, y))
if x2 or x3 or x4 or x5 or x6 or x7 or x8 or x9 or x10 or x11==x and y2 or y3 or y4 or y5 or y6 or y7 or y8 or y9 or y10 or y11==y: if x2==x and y2==y:
x2=random.randint(0,21) x2=random.randint(0,21)
y2=random.randint(0,15) y2=random.randint(0,15)
x2=x2*30 x2=x2*30
y2=y2*30 y2=y2*30
else: else:
position.pop(0) position.pop(0)
# 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去
screen.blit(snake_head, 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, (x2, y2)) screen.blit(food, (x2, y2))
screen.blit(food, (x3, y3))
screen.blit(food, (x4, y4))
screen.blit(food, (x5, y5))
screen.blit(food, (x6, y6))
screen.blit(food, (x7, y7))
screen.blit(food, (x8, y8))
screen.blit(food, (x9, y9))
screen.blit(food, (x10,y10))
screen.blit(food, (x11,y11))
# 刷新画面
pygame.display.update() pygame.display.update()
FPSCLOCK.tick(4) FPSCLOCK.tick(0.01)
\ No newline at end of file \ 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