失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python实现诺基亚双人贪吃蛇小游戏

python实现诺基亚双人贪吃蛇小游戏

时间:2022-05-05 03:53:29

相关推荐

python实现诺基亚双人贪吃蛇小游戏

完成目标效果:

主程序代码:

import mathimport randomimport pygamefrom pygame.locals import *running = Falseplaying = Falsescreen = Nonetimer = Nonesnk1 = Nonesnk2 = Nonefoods = NoneremainFoods = 7radiusFood = 8'''链表节点'''class Node:def __init__(self, data, prev = None, next = None):self.data = dataself.prev = prevself.next = nextdef insert_front(self, node):if self.prev:node.prev = self.prevself.prev.next = nodeself.prev = nodenode.next = selfelse:self.prev = nodenode.next = selfreturn nodedef insert_back(self, node):if self.next:node.next = self.nextself.next.prev = nodeself.next = nodenode.prev = selfelse:self.next = nodenode.prev = selfreturn nodedef remove(self):if self.next:self.next.prev = self.previf self.prev:self.prev.next = self.next'''蛇'''class Snack:def __init__(self, surface, color, start_pos, end_pos, face):self.color = colorself.surface = surfaceself.head = Node(start_pos)self.tail = Node(end_pos)self.head.insert_back(self.tail)self.length = self.distanceBetween(start_pos, end_pos)self.face = faceself.speed = 120self.eat = 0self.grow = 0self.mapAngle = [[0, math.pi * 3 / 2, math.pi / 2],[0, math.pi * 7 / 4, math.pi / 4],[math.pi, math.pi * 5 / 4, math.pi * 3 / 4]]'''坐标取整'''def intPos(self, pos):return (int(pos[0]), int(pos[1]))'''坐标转角度'''def pos2Angle(self, pos):return self.mapAngle[pos[0]][pos[1]]'''极坐标位移'''def polarPos(self, pos, angle, dis):xx = pos[0] + dis * math.cos(angle)yy = pos[1] + dis * math.sin(angle)return (xx, yy)'''计算两点间距离'''def distanceBetween(self, pos1, pos2):dx = pos2[0] - pos1[0]dy = pos2[1] - pos1[1]return math.sqrt(dx*dx + dy*dy)'''计算两点间角度'''def angleBetween(self, pos1, pos2):dx = pos2[0] - pos1[0]dy = pos2[1] - pos1[1]return math.atan2(dy, dx)'''改变面向'''def changeFace(self, newFace):if newFace[0] == 0 and newFace[1] == 0:returnif newFace == self.face:returnxx = self.face[0] + newFace[0]yy = self.face[1] + newFace[1]if xx == 0 and yy == 0:returnself.face = newFaceself.head = self.head.insert_front(Node(self.head.data))'''吃到食物'''def eatFood(self, grow):self.grow = growself.eat += 1'''绘制蛇身'''def draw(self):node = self.headpygame.draw.circle(self.surface, self.color, self.intPos(node.data), 6, 6)while node:n2 = node.next if not n2:breakpygame.draw.line(self.surface, self.color, self.intPos(node.data), self.intPos(n2.data), 6)node = node.next'''每帧移动'''def walk(self, delta):dis = self.speed * delta / 1000self.head.data = self.polarPos(self.head.data, self.pos2Angle(self.face), dis)if self.grow >= dis:self.grow -= diselse:dis -= self.growself.grow = 0self.cutTail(dis)'''收缩尾巴'''def cutTail(self, length):node = self.tailwhile length > 0:n2 = node.prevdis = self.distanceBetween(n2.data, node.data)angle = self.angleBetween(node.data, n2.data)if dis > length:node.data = self.polarPos(node.data, angle, length)length = 0else:self.tail = node.prevnode.remove()length -= disnode = node.prev'''屏幕指定位置绘制文字'''def printText(surface, str, pos, size = 24, color = (255, 255, 255)):global screenfont = pygame.font.SysFont("microsoftyaheimicrosoftyaheiui", size)text = font.render(str, True, color)w = text.get_width()h = text.get_height()surface.blit(text, (pos[0] - w / 2, pos[1] - h / 2))'''添加食物'''def addFood():global screen, snk1, snk2, foods, remainFoodsif remainFoods <= 0:returnw = screen.get_width()h = screen.get_height()while True:posX = random.randint(5, w - 5)posY = random.randint(5, h - 5)color = tuple(screen.get_at((posX, posY)))if color != snk1.color and color != snk2.color:breakremainFoods -= 1if not foods:foods = Node((posX, posY))else:foods = foods.insert_front(Node((posX, posY)))'''删除食物'''def removeFood(node):global foodsif node == foods:foods = foods.nextelse:node.remove()'''检测吃到食物'''def checkEatFood():global foods, radiusFood, snk1, snk2node = foodswhile node:if snk1.distanceBetween(snk1.head.data, node.data) < (radiusFood + 4):snk1.eatFood(50)removeFood(node)addFood()breakelif snk2.distanceBetween(snk2.head.data, node.data) < (radiusFood + 4):snk2.eatFood(50)removeFood(node)addFood()breakelse:node = node.next'''游戏初始界面'''def logo():global screen, remainFoodsw = screen.get_width()h = screen.get_height()printText(screen, "Snack V1.0", (w / 2, h / 3), 48)printText(screen, "任意键继续", (w / 2, h / 2), 24, (55, 255, 55))printText(screen, str(remainFoods) + "个食物,抢完即止", (w / 2, h * 2 / 3), 32)def quit():pygame.font.quit()'''检测游戏结束'''def checkGameOver():global remainFoods, snk1, snk2, foods, playing, screenif remainFoods == 0 and foods == None:playing = Falsescreen.fill((0,0,0))w = screen.get_width()h = screen.get_height()if snk1.eat > snk2.eat:printText(screen, "玩家1 胜利", (w / 2, h / 2), 48)elif snk1.eat < snk2.eat:printText(screen, "玩家2 胜利", (w / 2, h / 2), 48)else:printText(screen, "平局", (w / 2, h / 2), 48)'''键盘按键转换成面向角度'''def cmd():global snk1, snk2keys = pygame.key.get_pressed()x1 = x2 = y1 = y2 = 0if keys[pygame.K_RIGHT]:x2+=1if keys[pygame.K_LEFT]:x2-=1if keys[pygame.K_UP]:y2+=1if keys[pygame.K_DOWN]:y2-=1if keys[pygame.K_d]:x1+=1if keys[pygame.K_a]:x1-=1if keys[pygame.K_w]:y1+=1if keys[pygame.K_s]:y1-=1snk1.changeFace((x1, y1))snk2.changeFace((x2, y2))'''游戏每帧更新'''def play(delta):global playing, snk1, snk2if not playing:returncmd()snk1.walk(delta)snk2.walk(delta)checkEatFood()checkGameOver()'''绘制'''def draw():global snk1, snk2, playing, screen, radiusFood, remainFoodsif not playing:returnscreen.fill((0,0,0))snk1.draw()snk2.draw()node = foodswhile node:color = (255, 255, 255)if remainFoods == 0:color = (255, 0, 0)pygame.draw.circle(screen, color, node.data, radiusFood, radiusFood // 2 + 1)node = node.nextdef start(width = 800, height = 600, fps = 60):global running, screen, timer, playing, snk1, snk2pygame.init()pygame.font.init()font = pygame.font.SysFont("microsoftyaheimicrosoftyaheiui", 20)pygame.display.set_caption("Snack V1.0")screen = pygame.display.set_mode((width, height))logo()snk1 = Snack(screen, (0, 150, 200), (100, 100), (0, 100), (1, 0))snk2 = Snack(screen, (255, 100, 0), (width * 5 // 6, height // 2), (width * 5 // 6 + 100, height // 2), (-1, 0))for i in range(3):addFood()timer = pygame.time.Clock()running = Truewhile running:delta = timer.tick(fps)play(delta)draw()for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.KEYDOWN:if event.key == pygame.K_SPACE and playing == False:screen.fill((0,0,0))playing = Truepygame.display.flip()if __name__ == "__main__":start()quit()

原链接:链接:/s/1vGn8Ci9tNoPflDrCRMwa1A

提取码:7z8a

如果觉得《python实现诺基亚双人贪吃蛇小游戏》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。