Commit c9d77a2f by BellCodeEditor

save project

parent a1dcb30d
Showing with 9 additions and 4 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() # pygame时钟,控制游戏速度(帧数) FPSCLOCK=pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
...@@ -14,8 +15,8 @@ x, y=240, 120 ...@@ -14,8 +15,8 @@ x, y=240, 120
position=[(180, 90), (180, 120), (210, 120), (x, y)] position=[(180, 90), (180, 120), (210, 120), (x, y)]
setheading="right" setheading="right"
snake_head=right snake_head=right
food_x=360 apple_x=360
food_y=300 apple_y=300
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:
...@@ -30,7 +31,7 @@ while True: ...@@ -30,7 +31,7 @@ while True:
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.ke ==locals.K_DOWN and setheading!="up": if event.key ==locals.K_DOWN and setheading!="up":
setheading= 'down' setheading= 'down'
snake_hea =down snake_hea =down
if setheading=="right": if setheading=="right":
...@@ -43,10 +44,13 @@ while True: ...@@ -43,10 +44,13 @@ while True:
y+=30 y+=30
position.append((x, y)) position.append((x, y))
position.pop(0) position.pop(0)
if x==apple_x and y==apple_y:
apple_x=random.randint(0,630)
apple_y=random.randint(0,450)
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 i in range(len(position)-1):
screen.blit(body, position[i]) screen.blit(body, position[i])
screen.blit(food, (food_x, food_y)) screen.blit(food, (apple_x, apple_y))
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