diff --git a/apple.png b/apple.png new file mode 100644 index 0000000..e58cb8f Binary files /dev/null and b/apple.png differ diff --git a/bg.png b/bg.png new file mode 100644 index 0000000..a085237 Binary files /dev/null and b/bg.png differ diff --git a/body.png b/body.png new file mode 100644 index 0000000..a254609 Binary files /dev/null and b/body.png differ diff --git a/down.png b/down.png new file mode 100644 index 0000000..41d1525 Binary files /dev/null and b/down.png differ diff --git a/left.png b/left.png new file mode 100644 index 0000000..365dffe Binary files /dev/null and b/left.png differ diff --git a/neuropol.ttf b/neuropol.ttf new file mode 100644 index 0000000..ddefb40 Binary files /dev/null and b/neuropol.ttf differ diff --git a/right.png b/right.png new file mode 100644 index 0000000..e77639a Binary files /dev/null and b/right.png differ diff --git a/snake.py b/snake.py new file mode 100644 index 0000000..31706c1 --- /dev/null +++ b/snake.py @@ -0,0 +1,105 @@ +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 diff --git a/up.png b/up.png new file mode 100644 index 0000000..44f2e28 Binary files /dev/null and b/up.png differ diff --git "a/\351\230\277\346\234\261\346\263\241\346\263\241\344\275\223.ttf" "b/\351\230\277\346\234\261\346\263\241\346\263\241\344\275\223.ttf" new file mode 100755 index 0000000..fa0e7b3 Binary files /dev/null and "b/\351\230\277\346\234\261\346\263\241\346\263\241\344\275\223.ttf" differ