Commit 5f4f6e1a by BellCodeEditor

auto save

parent 27a602bc
Showing with 171 additions and 13 deletions
import pygame
from pygame import locals
import os
import sys
import random
# 初始化pygame,为使用硬件做准备
pygame.init()
# 创建一个窗口
screen = pygame.display.set_mode((660, 480))
clock = pygame.time.Clock() # 创建一个计时器
# 背景
background = pygame.image.load('bg.png')
right = pygame.image.load('right.png')
food = pygame.image.load('apple.png')
body = pygame.image.load('body.png')
x, y = 360, 300
position = [(330, 300), (300, 300), (270, 300), (x, y)]
setheading = 'right'
food_x , food_y = 180 , 300 #初始化苹果坐标
speed = 3
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_RIGHT and setheading != 'left':
setheading = 'right'
if event.key == locals.K_LEFT and setheading != 'right':
setheading = 'left'
if event.key == locals.K_UP and setheading != 'down':
setheading = 'up'
if event.key == locals.K_DOWN and setheading != 'up':
setheading = 'down'
if setheading == 'right':
x += 30
elif setheading == 'left':
x -= 30
elif setheading == 'up':
y -= 30
else:
y += 30
if (x,y) in position: #碰到身体就结束
exit()
position.append((x, y))
if x == food_x and y == food_y:
num1 = random.randint(0,21)
num2 = random.randint(0,15)
food_x = num1 * 30
food_y = num2 * 30
speed += 0.5
else:
position.pop(0)
if x < 0 or x > 630 or y < 0 or y > 450:
exit() #结束程序
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇画上去
screen.blit(right, (x, y))
for i in range(len(position)-1):
screen.blit(body, position[i])
# 将果实画上去
screen.blit(food, (food_x, food_y))
# 刷新画面
pygame.display.update()
clock.tick(speed) # 设置帧数
import pygame
from pygame import locals
import os
import sys
import random
# 初始化pygame,为使用硬件做准备
pygame.init()
# 创建一个窗口
screen = pygame.display.set_mode((660, 480))
# 背景
background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') # 右
left = pygame.image.load('left.png') # 左
up = pygame.image.load('up.png') # 上
down = pygame.image.load('down.png') # 下
food = pygame.image.load('apple.png')
body = pygame.image.load('body.png')
clock = pygame.time.Clock() # 创建时钟
x, y = 360, 300 # 蛇头初始坐标
# 把身体和蛇头初始坐标存进列表中
position = [(270, 300), (300, 300), (330, 300), (x, y)]
setheading = 'right' # 初始朝向
snake_head = right # 设置初始蛇头图片
apple_x, apple_y = 180, 300
speed = 3
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_w and setheading != 'down':
setheading = 'up'
snake_head = up
if event.key == locals.K_d and setheading != 'left':
setheading = 'right'
snake_head = right
if event.key == locals.K_a and setheading != 'right':
setheading = 'left'
snake_head = left
if event.key == locals.K_s and setheading != 'up':
setheading = 'down'
snake_head = down
if setheading == 'right':
x += 30
elif setheading == 'left':
x -= 30
elif setheading == 'up':
y -= 30
elif setheading == 'down':
y += 30
position.append((x, y))
if x == apple_x and y == apple_y:
num1 = random.randint(0,21) #横 格子数
num2 = random.randint(0, 15) #竖 格子数
apple_x = num1 * 30
apple_y = num2 * 30
speed += 0.5
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, (x, y))
# 把身体渲染出来
for i in range(len(position)-1):
screen.blit(body, position[i])
# 将果实画上去
screen.blit(food, (apple_x, apple_y))
# 刷新画面
pygame.display.update()
clock.tick(speed) # 帧数
...@@ -8,7 +8,7 @@ pygame.init() ...@@ -8,7 +8,7 @@ pygame.init()
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((660, 480)) screen = pygame.display.set_mode((660, 480))
FRSCLOCK=pygame.time.Clock() # 帧数
# 背景 # 背景
background = pygame.image.load('bg.png') background = pygame.image.load('bg.png')
...@@ -16,28 +16,26 @@ right = pygame.image.load('right.png') ...@@ -16,28 +16,26 @@ 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')
x , y = 240 ,120 #蛇头的初始位置
poistion = [(210, 120),(180, 120), (180, 90),(x , y)]
setheading = '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()
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# 将贪吃蛇画上去 # 将贪吃蛇画上去
screen.blit(right, 360,300) screen.blit(right, (360 , 300 ))
screen.blit(body , (330,300))
screen.blit(body , (300,300))
screen.blit(body , poistion[i]) screen.blit(body , (270,300))
# 将果实画上去 # 将果实画上去
screen.blit(food, (360, 300)) screen.blit(food, (180, 300))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FRSCLOCK.tick(3) #帧数3
\ 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