Lv.1
努力像我昵称一样名商人。帝国崛起、技术大爆炸、努力做生意
签名:做作品和做生意 联系方式:QQ:430460845
在 谁会做Python贪吃蛇游戏。 中回复
import pygame, random
from pygame.locals import *
pygame.init()
UNITSIZE = 50
WINWUNITS = 8
WINHUNITS = 8
WINW = UNITSIZE * WINWUNITS
WINH = UNITSIZE * WINHUNITS
FPS = 2
CLOCK = pygame.time.Clock()
snake = [(3, 3), (2, 3) , (1, 3)]
direction = "right"
score = 0
BGCOLOR = GOLD = (216, 234, 196)
HEADC = BLUE = ( 0, 0, 255)
SNAKEC = RED = (255, 0, 0)
STARC = YELLOW = (255, 255, 0)
SCREEN = pygame.display.set_mode((WINW, WINH))
pygame.display.set_caption("贪吃的小蛇 - 得分:0")
def updateStar():
global star
validPos = []
for x in range(WINWUNITS):
for y in range(WINHUNITS):
validPos.append((x, y))
for invalid in snake:
validPos.remove(invalid)
star = random.choice(validPos)
def unit2rect(units):
return pygame.Rect(units[0] * UNITSIZE, units[1] * UNITSIZE, UNITSIZE, UNITSIZE)
def drawSnake(screen, headc, bodyc, poseslist, radius):
for pos in snake:
if pos == snake[0]:
color = headc
else:
color = bodyc
unitr = unit2rect(pos)
pygame.draw.circle(screen, color, unitr.center, radius)
def drawStar(screen, color, rect):
l = rect.left
w = rect.width
t = rect.top
h = rect.height
pygame.draw.polygon(screen, color, ((l + 0.5 * w, t ),
(l + 0.6 * w, t + 0.4 * h),
(l + w, t + 0.4 * h),
(l + 0.7 * w, t + 0.6 * h),
(l + 0.8 * w, t + h),
(l + 0.5 * w, t + 0.8 * h),
(l + 0.2 * w, t + h),
(l + 0.3 * w, t + 0.6 * h),
(l , t + 0.4 * h),
(l + 0.4 * w, t + 0.4 * h)))
updateStar()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
elif event.type == KEYDOWN:
if event.key == K_LEFT and direction != "right":
direction = "left"
elif event.key == K_RIGHT and direction != "left":
direction = "right"
elif event.key == K_UP and direction != "down":
direction = "up"
elif event.key == K_DOWN and direction != "up":
direction = "down"
SCREEN.fill(BGCOLOR)
if direction == "left":
snake.insert(0, ((snake[0][0]-1) % WINWUNITS, snake[0][1]))
elif direction == "right":
snake.insert(0, ((snake[0][0]+1) % WINWUNITS, snake[0][1]))
elif direction == "up":
snake.insert(0, (snake[0][0], (snake[0][1]-1) % WINHUNITS))
elif direction == "down":
snake.insert(0, (snake[0][0], (snake[0][1]+1) % WINHUNITS))
drawSnake(SCREEN, HEADC, SNAKEC, snake, UNITSIZE//2)
drawStar(SCREEN, STARC, pygame.Rect(unit2rect(star)))
if snake[0] == star:
score += 1
updateStar()
pygame.display.set_caption("贪吃的小蛇 - 得分:" + str(score))
elif snake[0] in snake[1:]:
pygame.display.update()
pygame.time.wait(3000)
print('最终得分',score,'分')
pygame.quit()
exit()
else:
snake.pop()
pygame.display.update()
CLOCK.tick(FPS)
2019-02-16T14:26:08 点赞:0
在 【修图店】【封面店】 中回复
封面格式:
必填:
大字:
【开源】做生意(跑商)
小字:快来玩吧
非必填:
底图:
风格:富有
要求:很像做生意的风格
2019-02-18T12:18:29 点赞:0
在 【修图店】【封面店】 中回复
2019-02-18T12:22:29 点赞:0
在 【小教程】如何限制网站访问 中回复
print('奶茶是专攻C++的?)
answer=input('能看懂吗?')
if answer=='能':
print('1')
else:
print('1')2019-02-18T20:23:37 点赞:0
在 【往事回顾】编程猫历史:你见过几个? 中回复
以前好像还开过工作室,为什么以前取消了?为什么银行没了?(希望可以喵)为什么战队没了?
这些东西都挺好的
2019-02-19T13:54:44 点赞:0