Commit 0b147c2d by BellCodeEditor

save project

parent 2b884c94
Showing with 67 additions and 11 deletions
......@@ -3,21 +3,29 @@ from pygame import *;
import random;
import sys;
setHeadingString = "right"
playerX = 0
playerY = 0
position = [(playerX, playerY)]
setHeading = "right";
fps = time.Clock();
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');
snake = image.load('right.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');
setHeading = snake
myFont = font.Font('阿朱泡泡体.ttf', 30)
snakeHead = right;
# 创建一个窗口
screen = display.set_mode((660, 480));
......@@ -27,12 +35,59 @@ while 1:
if events.type == QUIT:
sys.exit();
if events.type == KEYDOWN:
if events.key == K_w:
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, 17) * 30;
elif setHeading == "down":
playerY += 30;
if playerX == appleX and playerY == appleY:
score += 30;
appleX = random.randint(1, 21) * 30;
appleY = random.randint(1, 17) * 30;
elif setHeading == "left":
playerX -= 30;
if playerX == appleX and playerY == appleY:
score += 30;
appleX = random.randint(1, 21) * 30;
appleY = random.randint(1, 17) * 30;
else:
playerX += 30;
if playerX == appleX and playerY == appleY:
score += 30;
appleX = random.randint(1, 21) * 30;
appleY = random.randint(1, 17) * 30;
position.append((playerX, playerY));
position.pop(0);
screen.blit(background, (0, 0));
screen.blit(snake, (240, 120));
screen.blit(body, (210, 120));
screen.blit(apple, (240, 150));
screen.blit(snakeHead, position[-1]);
screen.blit(apple, (appleX, appleY));
for i in range(len(position) - 1):
screen.blit(body, position[i]);
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