Commit cf439404 by BellCodeEditor

save project

parent 42baf734
Showing with 56 additions and 10 deletions
import pygame import pygame
import sys import sys
import random
# 初始化pygame,为使用pygame做准备 # 初始化pygame,为使用pygame做准备
pygame.init() pygame.init()
head_x = 60
head_y = 60
position = [(0,0),(30,0),(30,30),(head_x,head_y)]
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1080,550)) screen = pygame.display.set_mode((1080,550))
background = pygame.image.load("r_c.png") background = pygame.image.load("r_c.png")
body = pygame.image.load("body.png") body = pygame.image.load("body.png")
right = pygame.image.load("right.png")
apple = pygame.image.load("apple.png") apple = pygame.image.load("apple.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")
FPSCLOCK = pygame.time.Clock()
heading = "right"
snake_head = right
apple_x = 540
apple_y = 150
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
sys.exit() sys.exit()
if event.type == pygame.KEYDOWN:
if event.key ==pygame.K_w and heading != "down":
heading = "up"
if event.key ==pygame.K_s and heading != "up":
heading = "down"
if event.key ==pygame.K_a and heading != "right":
heading = "left"
if event.key ==pygame.K_d and heading != "left":
heading = "right"
screen.blit(background,(0,0)) screen.blit(background,(0,0))
screen.blit(body,(0,0)) screen.blit(apple,(apple_x,apple_y))
screen.blit(body,(30,0))
screen.blit(body,(30,30)) if heading == "right":
screen.blit(body,(30,60)) head_x += 30
screen.blit(right,(60,60)) snake_head = right
screen.blit(apple,(525,140)) if heading == "left":
pygame.display.update() head_x -= 30
\ No newline at end of file snake_head = left
if heading == "up":
head_y -= 30
snake_head = up
if heading == "down":
head_y += 30
snake_head = down
position.append((head_x,head_y))
if head_x == apple_x and head_y == apple_y:
apple_x = random.randint(0,21)*30
apple_y = random.randint(0,15)*30
else:
position.pop(0)
screen.blit(snake_head,(head_x,head_y))
for index in range(len(position)-1):
screen.blit(body,position[index])
FPSCLOCK.tick(10)
pygame.display.update()
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