Commit fd6bd2dd by BellCodeEditor

auto save

parent c6188a1e
Showing with 188 additions and 2 deletions
import pygame
from pygame import locals
# 初始化pygame,为使用pygame做准备
pygame.init()
screen=pygame.display.set_mode((660,480)) #创建窗口
bg=pygame.image.load("bg.png")
body=pygame.image.load("body.png")
right=pygame.image.load("right.png")
apple=pygame.image.load("apple.png")
while True:
for event in pygame.event.get(): #遍历pygame中所有的事件
if event.type==locals.QUIT: #如果发生的事件是退出事件的话
exit()#退出
screen.blit(bg,(0,0))
screen.blit(right,(240,120))
screen.blit(body,(210,120))
screen.blit(body,(180,120))
screen.blit(body,(180,90))
screen.blit(apple,(360,300))
pygame.display.update()
\ No newline at end of file
import pygame import pygame
from pygame import locals
from random import *
# 初始化pygame,为使用pygame做准备 # 初始化pygame,为使用pygame做准备
pygame.init() pygame.init()
bg=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")
snake_head=right #初始化蛇头方向
head="right" #初始化行走方向
# 创建一个窗口 # 创建一个窗口
?? screen=pygame.display.set_mode((660,480))
\ No newline at end of file FPS=pygame.time.Clock()#创建一个计时器
x,y=270,150
position=[(210,120),(210,150),(240,150),(x,y)]
apple_x=360 #苹果x坐标
apple_y=300 #苹果y坐标
font=pygame.font.Font("neuropol.ttf",18)
score=0
speed=2 #设置初始速度
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 head != "down":
head='up'
snake_head=up
if event.key==locals.K_s and head != "up":
head='down'
snake_head=down
if event.key==locals.K_a and head != "right":
head='left'
snake_head=left
if event.key==locals.K_d and head != "left":
head='right'
snake_head=right
if head=="right": #如果蛇的行走方向是朝向右的就像x+30向右走
x+=30
elif head=="left": #如果蛇的行走方向是朝向左的就像x-30向左走
x-=30
elif head=="up": #如果蛇的行走方向是朝向上的就像y-30向上走
y-=30
else: #否则就向下走y+30
y+=30
if (x,y) in position:
exit()
position.append((x,y))
if x==apple_x and y==apple_y:
apple_x=randint(1,22)*30-30
apple_y=randint(1,16)*30-30
score+=1
if score%3==0:
speed+=1
else:
position.pop(0)
#撞墙就死
# if x<0 or x>630 or y<0 or y>450:
# exit()
#穿墙模式
if x<0:
x=660
elif x>630:
x=-30
if y<0:
y=480
elif y>480:
y=-30
#将背景图画上去
screen.blit(bg,(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))
#分数
text=font.render("Score:"+str(score),True,(238,130,238))
screen.blit(text,(540,10))
text2=font.render("speed:"+str(speed),True,(238,230,8))
screen.blit(text2,(540,30))
#刷新画面
pygame.display.update()
FPS.tick(speed)
# screen.blit(bg,(0,0)) # 将图像显示出来
# pygame.display.update()# #刷新画面
\ No newline at end of file
import pygame
import pygame
from pygame import locals
# 初始化pygame,为使用硬件做准备
pygame.init()
# 创建一个窗口
screen = pygame.display.set_mode((660, 480))
fps=pygame.time.Clock()
x,y=240,120
pos=[(180,90),(180,120),(210,120),(x,y)]
# 背景
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')
setheading="right"
snake_head=right
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'
snake_head = right
if event.key == locals.K_LEFT and setheading != "right":
setheading = 'left'
snake_head = left
if event.key == locals.K_UP and setheading != "down":
setheading = 'up'
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
pos.append((x,y))
pos.pop(0)
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇画上去
screen.blit(snake_head, (x, y))
# 将贪吃蛇的身体画上去
for i in range(len(pos)-1):
screen.blit(body,pos[i])
# screen.blit(body, (210, 120))
# screen.blit(body, (180, 120))
# screen.blit(body, (180, 90))
# 将果实画上去
screen.blit(food, (360, 300))
# 刷新画面
pygame.display.update()
fps.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