Commit 55ee3c12 by BellCodeEditor

auto save

parent e97a1482
Showing with 46 additions and 81 deletions
import pygame import turtle
from pygame import locals turtle.setup(1500,800,50,50)
import random pen1 = turtle.Pen()
# 初始化pygame,为使用硬件做准备 pen1.speed(5)
pygame.init()
pen1.penup()
# 创建一个窗口 pen1.goto(-550,0)
screen = pygame.display.set_mode((660, 480)) pen1.pendown()
FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) pen1.pensize(20)
pen1.pencolor("blue")
# 背景 pen1.circle(150)
background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') # 头 朝右
food = pygame.image.load('apple.png') # 食物 苹果 pen1.penup()
body = pygame.image.load('body.png') # 身体 pen1.goto(-210,0)
left = pygame.image.load('left.png') # 头 朝左 pen1.pendown()
up = pygame.image.load('up.png') # 头 朝上 pen1.pensize(20)
down = pygame.image.load('down.png') # 头 朝下 pen1.pencolor("red")
pen1.circle(150)
x, y = 240, 120
position = [(180, 90), (180, 120), (210, 120), (x, y)] pen1.penup()
pen1.goto(130,0)
setheading = "right" pen1.pendown()
snake_head = right pen1.pensize(20)
pen1.pencolor("black")
apple_x = 360 pen1.circle(150)
apple_y = 300
pen1.penup()
pen1.goto(-400,-150)
while True: pen1.pendown()
for event in pygame.event.get(): pen1.pensize(20)
if event.type == locals.QUIT: pen1.pencolor("green")
# 接收到退出事件后退出程序 pen1.circle(150)
exit()
if event.type == locals.KEYDOWN: pen1.penup()
if event.key == locals.K_RIGHT and setheading != "left": pen1.goto(-20,-150)
setheading = 'right' pen1.pendown()
snake_head = right pen1.pensize(20)
if event.key == locals.K_LEFT and setheading != "right": pen1.pencolor("yellow")
setheading = 'left' pen1.circle(150)
snake_head = left
if event.key == locals.K_UP and setheading != "down":
setheading = 'up' turtle.done()
snake_head = up
if event.key == locals.K_DOWN and setheading != "up":
setheading = 'down'
snake_head = down
# 设置贪吃蛇的头部坐标
if setheading == "right":
x += 30
elif setheading == "left":
x -= 30
elif setheading == "up":
y -= 30
else:
y += 30
position.append((x, y))
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
else:
position.pop(0)
if x<0 or x>630 or y<0 or y>450:
exit()
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去
screen.blit(snake_head, position[-1])
# 将贪吃蛇的身体画上去
for i in range(len(position)-1):
screen.blit(body, position[i])
# 将果实画上去
screen.blit(food, (apple_x,apple_y))
# 刷新画面
pygame.display.update()
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