Commit ae8f8a5d by BellCodeEditor

save project

parent 6e7111d1
apple.png

2.05 KB

bg.png

22.5 KB

body.png

1.4 KB

down.png

2.01 KB

left.png

2.07 KB

File added
right.png

2.05 KB

from pygame import *;
from time import sleep;
import random;
import sys;
setHeading = "right";
wasPause = True;
score = 0;
appleX = 240;
appleY = 150;
playerX = 240;
playerY = 120;
position = [(210, 120), (210, 150), (playerX, playerY)];
# 初始化pygame,为使用pygame做准备
init();
background = image.load('bg.png');
right = image.load('right.png');
left = image.load('left.png');
up = image.load('up.png');
down = image.load('down.png');
body = image.load('body.png');
apple = image.load('apple.png');
myFont = font.Font('阿朱泡泡体.ttf', 30);
snakeHead = right;
# 创建一个窗口
screen = display.set_mode((660, 480));
while 1:
if wasPause:
for events in event.get():
if events.type == QUIT:
sys.exit();
if events.type == KEYDOWN:
if events.key == K_w and setHeading != "down":
setHeading = "up";
snakeHead = up;
if events.key == K_s and setHeading != "up":
setHeading = "down";
snakeHead = down;
if events.key == K_a and setHeading != "right":
setHeading = "left";
snakeHead = left;
if events.key == K_d and setHeading != "left":
setHeading = "right";
snakeHead = right;
if setHeading == "up":
playerY -= 30;
if playerX == appleX and playerY == appleY:
score += 30;
appleX = random.randint(1, 21) * 30;
appleY = random.randint(1, 15) * 30;
elif setHeading == "down":
playerY += 30;
if playerX == appleX and playerY == appleY:
score += 30;
appleX = random.randint(1, 21) * 30;
appleY = random.randint(1, 15) * 30;
elif setHeading == "left":
playerX -= 30;
if playerX == appleX and playerY == appleY:
score += 30;
appleX = random.randint(1, 21) * 30;
appleY = random.randint(1, 15) * 30;
else:
playerX += 30;
if playerX == appleX and playerY == appleY:
score += 30;
appleX = random.randint(1, 21) * 30;
appleY = random.randint(1, 15) * 30;
content = "分数:" + str(score);
scoreText = myFont.render(content, True, (50, 50, 50));
position.append((playerX, playerY));
position.pop(0);
screen.blit(background, (0, 0));
screen.blit(snakeHead, position[-1]);
screen.blit(apple, (appleX, appleY));
for i in range(len(position) - 1):
screen.blit(body, position[i]);
screen.blit(scoreText, (15, 15))
display.update();
sleep(0.1);
for things in event.get():
if things.type == KEYDOWN:
if things.key == K_SPACE:
wasPause = not wasPause;
\ No newline at end of file
up.png

2.05 KB

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