Commit 29cb1251 by BellCodeEditor

save project

parent c04f6d11
Showing with 32 additions and 24 deletions
import pygame cimport pygame
from pygame import locals from pygame import locals
pygame.init() pygame.init()
screen = pygame.display.set_mode((660, 480)) screen = pygame.display.set_mode((660, 480))
uxsd=pygame.time.Clock() FPSCLOCK=pygame.time.Clock()
background = pygame.image.load('bg.png') background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') right = pygame.image.load('right.png')
food = pygame.image.load('apple.png') food = pygame.image.load('apple.png')
...@@ -12,35 +12,43 @@ left = pygame.image.load('left.png') ...@@ -12,35 +12,43 @@ left = pygame.image.load('left.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 snake_head=right
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
exit() exit()
if event.type == locals.k_RIGHT and setheading !="lfet": elif event.type==locals.KEYDOWN:
setheading='right' if event.type == locals.k_RIGHT and setheading !="lfet":
snake_head=right setheading='right'
snake_head=right
if event.type == locals.k_UP and setheading !="down": if event.type == locals.k_UP and setheading !="down":
setheading='up' setheading='up'
snake_head=up snake_head=up
y-=30 if event.type == locals.k_LEFT and setheading !="right":
if event.type == locals.k_LEFT and setheading !="right": setheading='left'
setheading='left' snake_head=left
snake_head=left if event.type == locals.k_DOWN and setheading !="up":
setheading='down'
snake_head=down
if setheading=='right':
x+=30
elif setheading=='left':
x-=30 x-=30
if event.type == locals.k_DOWN and setheading !="up": elif setheading=='up':
setheading='down' y-=30
snake_head=down else:
y+=30 y+=30
if x==apple_x and y==apple_y:
screen.blit(background, (0, 0))
x+=30
position.append((x,y)) position.append((x,y))
position.pop(0) position.pop(0)
screen.blit(right, (x,y)) screen.blit(background, (0,0))
screen.blit(snake_head,position[-1])
for i in range(len(position)-1): for i in range(len(position)-1):
screen.blit(body,position[i]) screen.blit(body,position[i])
screen.blit(food, (apple_x, apple_y))
pygame.display.update() pygame.display.update()
uxsd.t FPSCLOCK.tick(3)
\ No newline at end of file \ 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