用户:wawj查看:4 回复:7 评论:4 创建时间:2019-01-16T14:04:48
谁会做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)
pygame.quit()
exit()
else:
snake.pop()
pygame.display.update()
CLOCK.tick(FPS)点赞1
评论
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)
点赞0
评论
很短,只有40多行,但很好用 from turtle import * from random import randrange from freegames import square, vector food = vector(0, 0) snake = [vector(10, 0)] aim = vector(0, 10) def change(x, y): "Change snake direction." aim.x = x aim.y = y def inside(head): "Return True if head inside boundaries." return-200 < head.x < 190and-200 < head.y < 200 def move(): "Move snake forward one segment." head = snake[-1].copy() head.move(aim) ifnot inside(head)or head in snake: square(head.x, head.y, 9, "red") update() return snake.append(head) if head == food: print("Snake:",len(snake)) food.x = randrange(-15, 15)*10 food.y = randrange(-15, 15)*10 else: snake.pop(0) clear() for body in snake: square(body.x, body.y, 9, "black") square(food.x, food.y, 9, "green") update() ontimer(move, 300) def main(): setup(420, 420, 370, 0) hideturtle() tracer(False) listen() onkey(lambda: change(10, 0), "Right") onkey(lambda: change(-10, 0), "Left") onkey(lambda: change(0, 10), "Up") onkey(lambda: change(0, -10), "Down") move() done() main()
点赞0
评论