马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import pygame
import sys
from pygame.locals import *
import random
pygame.init()
white=(255,255,255)
bg=(255,255,255)
black=(0,0,0)
left=50
top=50
speed = [-2, 1]
size = width,height = 640,640
screen = pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
clock=pygame.time.Clock()
rectangle=pygame.draw.rect(screen,black,(left,top,50,50),1)
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit(0)
if event.type == KEYDOWN:
if event.key == K_LEFT:
speed = [-5, 0]
if event.key == K_RIGHT:
speed = [5, 0]
if event.key == K_UP:
speed = [0, -5]
if event.key == K_DOWN:
speed = [0, 5]
if left < 0 or left > 590:
# 翻转图像
# 反方向移动
speed[0] = -speed[0]
speed[1] = -speed[1]
if top < 0 or top > 590:
speed[0] = -speed[0]
speed[1] = -speed[1]
'''screen.fill(white)
pygame.draw.rect(screen,black,(left,top,50,50),1)'''
left=left+speed[0]
top=top+speed[1]
screen.fill(bg)
# 更新图像
pygame.draw.rect(screen,black,(left,top,50,50),1)
# 更新界面
pygame.display.flip()
# 延迟10毫秒
pygame.time.delay(30)
貌似忽然间发现了贪吃蛇的雏形 |