python實(shí)現(xiàn)雙人貪吃蛇小游戲
小編今天要給大家分享的是雙人貪吃蛇,大家可以和自己的兄弟,姐妹,爸爸,媽媽等一起玩喲!我先介紹一下游戲:
運(yùn)行游戲,進(jìn)入初始界面,按下空格鍵。
玩家(1):w,a,s,d
玩家(2):↑,←,↓,→
玩家要爭(zhēng)奪7個(gè)實(shí)物,直到吃完為止
游戲結(jié)束。
下面是小編寫(xiě)的代碼:
import math import random import pygame from pygame.locals import * running = False playing = False screen = None timer = None snk1 = None snk2 = None foods = None remainFoods = 7 radiusFood = 8 ''' 鏈表節(jié)點(diǎn) ''' class Node: def __init__(self, data, prev = None, next = None): self.data = data self.prev = prev self.next = next def insert_front(self, node): if self.prev: node.prev = self.prev self.prev.next = node self.prev = node node.next = self else: self.prev = node node.next = self return node def insert_back(self, node): if self.next: node.next = self.next self.next.prev = node self.next = node node.prev = self else: self.next = node node.prev = self return node def remove(self): if self.next: self.next.prev = self.prev if self.prev: self.prev.next = self.next ''' 蛇 ''' class Snack: def __init__(self, surface, color, start_pos, end_pos, face): self.color = color self.surface = surface self.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 = face self.speed = 120 self.eat = 0 self.grow = 0 self.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] ] '''坐標(biāo)取整''' def intPos(self, pos): return (int(pos[0]), int(pos[1])) '''坐標(biāo)轉(zhuǎn)角度''' def pos2Angle(self, pos): return self.mapAngle[pos[0]][pos[1]] '''極坐標(biāo)位移''' def polarPos(self, pos, angle, dis): xx = pos[0] + dis * math.cos(angle) yy = pos[1] + dis * math.sin(angle) return (xx, yy) '''計(jì)算兩點(diǎn)間距離''' def distanceBetween(self, pos1, pos2): dx = pos2[0] - pos1[0] dy = pos2[1] - pos1[1] return math.sqrt(dx*dx + dy*dy) '''計(jì)算兩點(diǎn)間角度''' 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: return if newFace == self.face: return xx = self.face[0] + newFace[0] yy = self.face[1] + newFace[1] if xx == 0 and yy == 0: return self.face = newFace self.head = self.head.insert_front(Node(self.head.data)) '''吃到食物''' def eatFood(self, grow): self.grow = grow self.eat += 1 '''繪制蛇身''' def draw(self): node = self.head pygame.draw.circle(self.surface, self.color, self.intPos(node.data), 6, 6) while node: n2 = node.next if not n2: break pygame.draw.line(self.surface, self.color, self.intPos(node.data), self.intPos(n2.data), 6) node = node.next '''每幀移動(dòng)''' def walk(self, delta): dis = self.speed * delta / 1000 self.head.data = self.polarPos(self.head.data, self.pos2Angle(self.face), dis) if self.grow >= dis: self.grow -= dis else: dis -= self.grow self.grow = 0 self.cutTail(dis) '''收縮尾巴''' def cutTail(self, length): node = self.tail while length > 0: n2 = node.prev dis = 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 = 0 else: self.tail = node.prev node.remove() length -= dis node = node.prev '''屏幕指定位置繪制文字''' def printText(surface, str, pos, size = 24, color = (255, 255, 255)): global screen font = 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, remainFoods if remainFoods <= 0: return w = 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: break remainFoods -= 1 if not foods: foods = Node((posX, posY)) else: foods = foods.insert_front(Node((posX, posY))) '''刪除食物''' def removeFood(node): global foods if node == foods: foods = foods.next else: node.remove() '''檢測(cè)吃到食物''' def checkEatFood(): global foods, radiusFood, snk1, snk2 node = foods while node: if snk1.distanceBetween(snk1.head.data, node.data) < (radiusFood + 4): snk1.eatFood(50) removeFood(node) addFood() break elif snk2.distanceBetween(snk2.head.data, node.data) < (radiusFood + 4): snk2.eatFood(50) removeFood(node) addFood() break else: node = node.next '''游戲初始界面''' def logo(): global screen, remainFoods w = screen.get_width() h = screen.get_height() printText(screen, "Snack V1.0", (w / 2, h / 3), 48) printText(screen, "任意鍵繼續(xù)", (w / 2, h / 2), 24, (55, 255, 55)) printText(screen, str(remainFoods) + "個(gè)食物,搶完即止", (w / 2, h * 2 / 3), 32) def quit(): pygame.font.quit() '''檢測(cè)游戲結(jié)束''' def checkGameOver(): global remainFoods, snk1, snk2, foods, playing, screen if remainFoods == 0 and foods == None: playing = False screen.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) '''鍵盤(pán)按鍵轉(zhuǎn)換成面向角度''' def cmd(): global snk1, snk2 keys = pygame.key.get_pressed() x1 = x2 = y1 = y2 = 0 if keys[pygame.K_RIGHT]: x2+=1 if keys[pygame.K_LEFT]: x2-=1 if keys[pygame.K_UP]: y2+=1 if keys[pygame.K_DOWN]: y2-=1 if keys[pygame.K_d]: x1+=1 if keys[pygame.K_a]: x1-=1 if keys[pygame.K_w]: y1+=1 if keys[pygame.K_s]: y1-=1 snk1.changeFace((x1, y1)) snk2.changeFace((x2, y2)) '''游戲每幀更新''' def play(delta): global playing, snk1, snk2 if not playing: return cmd() snk1.walk(delta) snk2.walk(delta) checkEatFood() checkGameOver() '''繪制''' def draw(): global snk1, snk2, playing, screen, radiusFood, remainFoods if not playing: return screen.fill((0,0,0)) snk1.draw() snk2.draw() node = foods while 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.next def start(width = 800, height = 600, fps = 60): global running, screen, timer, playing, snk1, snk2 pygame.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 = True while running: delta = timer.tick(fps) play(delta) draw() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE and playing == False: screen.fill((0,0,0)) playing = True pygame.display.flip() if __name__ == "__main__": start() quit()
以上就是雙人貪吃蛇的代碼啦!
教大家pygame的安裝方式
在終端輸入
pip install pyame,然后回車(chē)鍵進(jìn)行安裝
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)代碼統(tǒng)計(jì)器
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)代碼統(tǒng)計(jì)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09瀏覽器常用基本操作之python3+selenium4自動(dòng)化測(cè)試(基礎(chǔ)篇3)
瀏覽器常用基本操作有很多種,今天給大家介紹python3+selenium4自動(dòng)化測(cè)試的操作方法,是最最基礎(chǔ)的一篇,對(duì)python3 selenium4自動(dòng)化測(cè)試相關(guān)知識(shí)感興趣的朋友一起看看吧2021-05-05用smtplib和email封裝python發(fā)送郵件模塊類(lèi)分享
本文針對(duì)發(fā)郵件相關(guān)的操作進(jìn)行了封裝,包括發(fā)送文本、HTML、帶附件的郵件,使用Python發(fā)郵件,主要用到smtplib和email兩個(gè)模塊,需要的朋友可以參考下2014-02-02python實(shí)現(xiàn)將m3u8視頻轉(zhuǎn)換成mp4的操作步驟
m3u8 是一種基于文本的媒體播放列表文件格式,通常用于指定流媒體播放器播放在線媒體流,MP4是一種基于MPEG-4 Part 12(2015)和MPEG-4 Part 14標(biāo)準(zhǔn)的數(shù)字多媒體容器格式,本文將給大家介紹python實(shí)現(xiàn)將m3u8視頻轉(zhuǎn)換成mp4的操作步驟,需要的朋友可以參考下2024-05-05Python爬蟲(chóng)實(shí)現(xiàn)爬取百度百科詞條功能實(shí)例
這篇文章主要介紹了Python爬蟲(chóng)實(shí)現(xiàn)爬取百度百科詞條功能,結(jié)合完整實(shí)例形式分析了Python爬蟲(chóng)的基本原理及爬取百度百科詞條的步驟、網(wǎng)頁(yè)下載、解析、數(shù)據(jù)輸出等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04Pytorch反向傳播中的細(xì)節(jié)-計(jì)算梯度時(shí)的默認(rèn)累加操作
這篇文章主要介紹了Pytorch反向傳播中的細(xì)節(jié)-計(jì)算梯度時(shí)的默認(rèn)累加操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Python List remove()實(shí)例用法詳解
在本篇內(nèi)容里小編給大家整理了一篇關(guān)于Python List remove()方法及實(shí)例,有需要的朋友們跟著學(xué)習(xí)下。2021-08-08python range()函數(shù)取反序遍歷sequence的方法
今天小編就為大家分享一篇python range()函數(shù)取反序遍歷sequence的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06