Commit 7e944061 by BellCodeEditor

save project

parent 52f26819
apple.jpg

47.5 KB

apple.png

1.57 KB

asd.png

14.1 KB

bg.png

6.04 KB

body.png

161 Bytes

down.png

162 Bytes

fg.png

130 KB

left.png

162 Bytes

File added
right.png

162 Bytes

import pygame
from pygame import locals
from random import randint
# 初始化pygame,为使用硬件做准备
pygame.init()
# 创建一个窗口
screen = pygame.display.set_mode((660, 480))
fps = 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')
fg = pygame.image.load('fg.png')
head = "right"
x,y = 240,120
xy=0
ax,ay = randint(1,21)*30,randint(1,15)*30
liebiao=[(180,90),(180,120),(210,120),(x,y)]
font = pygame.font.Font("neuropol.ttf",50)
score=0
r,g,b=0,0,0
while True:
r,g,b=randint(0,255),randint(0,255),randint(0,255)
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
print(xy)
xy+=1
if event.key == locals.K_UP and head != "down":
head = "up"
if event.key == locals.K_DOWN and head != "up":
head = "down"
if event.key == locals.K_LEFT and head != "right":
head = "left"
if event.key == locals.K_RIGHT and head != "left":
head = "right"
if head == "right":
x+= 30
elif head == "left":
x-= 30
elif head == "up":
y-= 30
else:
y+= 30
liebiao.append((x,y))
if x < 0:
x=660
elif x > 630:
x=-30
if y < 0:
y=480
elif y>450:
y=-30
if ax == x and ay == y:
score+=100
ax,ay = randint(1,21)*30,randint(1,15)*30
screen.blit(food, (ax, ay))
else:
liebiao.pop(0)
# 将背景图画上去
if r%2 == 1:
screen.blit(background, (0, 0))
else:
screen.blit(fg,(r+b-200,g+b-200))
screen.blit(food, (ax, ay))
text = font.render("Score:"+str(score),True,(r,g,b))
# 将贪吃蛇画上去
for i in range(len(liebiao)-1):
screen.blit(body,(liebiao[i]))
screen.blit(right, (x, y))
for i in range(3):
screen.blit(text,(randint(0,7),randint(0,7)))
# 将贪吃蛇的身体画上去
#screen.blit(body, (210, 120))
#screen.blit(body, (180, 120))
#screen.blit(body, (180, 90))
# 将果实画上去
# 刷新画面
pygame.display.update()
fps.tick(20)
\ No newline at end of file
up.png

162 Bytes

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