Commit 655f19e2 by BellCodeEditor

auto save

parent c6188a1e
File added
File added
import pygame
from pygame import locals
import random
# 初始化pygame,为使用pygame做准备
pygame.init()
# 创建一个窗口
??
\ No newline at end of file
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock() # 设置时钟
# 载入图片
bg = pygame.image.load('bg.png')
up = pygame.image.load('up.png')
down = pygame.image.load('down.png')
left = pygame.image.load('left.png')
right = pygame.image.load('right.png')
body = pygame.image.load('body.png')
apple = pygame.image.load('apple.png')
x, y = 240, 330
position = [(240, 420), (240, 390), (240, 360), (x, y)] # 存储三个身体和一个蛇头位置
setheading = 'right' #设置朝向
head = right
apple_x = 360
apple_y = 300
score = 0
font = pygame.font.Font('STXINGKA.TTF',20)
speed = 5
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_UP and setheading != 'down' :
setheading = 'up'
head = up
if event.key == locals.K_DOWN and setheading != 'up' :
setheading = 'down'
head = down
if event.key == locals.K_LEFT and setheading != 'right' :
setheading = 'left'
head = left
if event.key == locals.K_RIGHT and setheading != 'left' :
setheading = 'right'
head = right
if setheading == 'up':
y -= 30
elif setheading == 'down':
y += 30
elif setheading == 'left':
x -= 30
else:
x += 30
if (x,y) in position: #判断舌头是否碰到身体
exit()
position.append((x, y)) # 每次把元组 x,y添加到列表的末尾
if x == apple_x and y == apple_y:
apple_x = (random.randint(1,26))*30-30
apple_y = (random.randint(1,20))*30-30
score+=10
speed += 5
else:
position.pop(0) # 删除列表的第一项
if x < 0 or x > 800 or y <0 or y > 570: #判断碰到墙壁
exit()
# 图片渲染
screen.blit(bg, (0, 0))
screen.blit(head, position[-1])
screen.blit(apple, (apple_x, apple_y))
for i in range(len(position)-1): # 便利列表 取出三个身体位置
screen.blit(body, position[i])
text = font.render('分数:'+str(score),True,(0,0,0))
screen.blit(text,(20,20))
pygame.display.update() # 刷新画面 一次刷新就可以
clock.tick(speed) # 帧数控制速度
import pygame
import pygame
from pygame import locals
pygame.init()
scrren = pygame.display.set_mode((800, 600))
#加载图片
bg = pygame.image.load('bg.png')
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
exit()
#渲染
scrren.blit(bg,(0,0))
#刷新画面
pygame.display.update()
\ 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