Commit fa695f7c by BellCodeEditor

save project

parent abf830f5
Showing with 12 additions and 6 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random
pygame.init() pygame.init()
screen=pygame.display.set_mode((660,480)) screen=pygame.display.set_mode((660,480))
FPSCLOCK=pygame.time.Clock() FPSCLOCK=pygame.time.Clock()
...@@ -12,6 +13,8 @@ up=pygame.image.load("up.png") ...@@ -12,6 +13,8 @@ up=pygame.image.load("up.png")
down=pygame.image.load("down.png") down=pygame.image.load("down.png")
x,y=240,120 x,y=240,120
position=[(180,90),(180,120),(210,120),(x,y)] position=[(180,90),(180,120),(210,120),(x,y)]
apple_x=360
apple_y=300
setheading="right" setheading="right"
snake_head=right snake_head=right
while True: while True:
...@@ -26,10 +29,10 @@ while True: ...@@ -26,10 +29,10 @@ while True:
setheading="left" setheading="left"
snake_head=left snake_head=left
if event.key==locals.K_UP and setheading!="down": if event.key==locals.K_UP and setheading!="down":
setheading=="up" setheading="up"
snake_head=up snake_head=up
if event.key==locals.K_DOWN and setheading!="up": if event.key==locals.K_DOWN and setheading!="up":
setheading=="down" setheading="down"
snake_head=down snake_head=down
if setheading=="right": if setheading=="right":
x+=30 x+=30
...@@ -40,12 +43,14 @@ while True: ...@@ -40,12 +43,14 @@ while True:
else: else:
y+=30 y+=30
position.append((x,y)) position.append((x,y))
if x==apple_x and y==apple_y:
apple_x=random.randint(0,660)
apple_y=random.randint(0,480)
position.pop(0) position.pop(0)
screen.blit(background,(0,0)) screen.blit(background,(0,0))
screen.blit(snake_head,position[-1]) screen.blit(snake_head,position[-1])
for i in range(len(position)-1):
for t in range(len(position)-1): screen.blit(body,position[i])
screen.blit(body,position[t]) screen.blit(food,(apple_x,apple_y))
screen.blit(food,(360,300))
pygame.display.update() pygame.display.update()
FPSCLOCK.tick(3) FPSCLOCK.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