python實(shí)現(xiàn)貪吃蛇游戲源碼
本文實(shí)例為大家分享了python實(shí)現(xiàn)貪吃蛇的具體代碼,供大家參考,具體內(nèi)容如下
import pygame import sys import random SCREEN_X=600 SCREEN_Y=600 class Snake(object): def __init__(self): self.direction=pygame.K_RIGHT self.body = [] for i in range(5): self.addnode() def addnode(self): left, top = (0, 0) if self.body: left, top = (self.body[0].left,self.body[0].top) node = pygame.Rect(left,top,25,25) if self.direction == pygame.K_RIGHT: node.left += 25 elif self.direction == pygame.K_LEFT: node.left -= 25 elif self.direction == pygame.K_UP: node.top -= 25 elif self.direction == pygame.K_DOWN: node.top += 25 self.body.insert(0,node) def delnode(self): self.body.pop() def isdead(self): if self.body[0].x not in range(SCREEN_X): return True if self.body[0].y not in range(SCREEN_Y): return True if self.body[0] in self.body[1:]: print('in body') return True return False def move(self): self.addnode() self.delnode() def changeddirection(self,curkey): LR = [pygame.K_LEFT,pygame.K_RIGHT] UD = [pygame.K_DOWN,pygame.K_UP] if curkey in LR+UD: if (curkey in LR) and (self.direction in LR): return if (curkey in UD) and (self.direction in UD): return self.direction = curkey class Food(object): def __init__(self): self.rect = pygame.Rect(-25, 0, 25, 25) def remove(self): self.rect.x = -25 def set(self): if self.rect.x == -25: allpos = [] for pos in range(25,SCREEN_X-25,25): allpos.append(pos) self.rect.left = random.choice(allpos) self.rect.top = random.choice(allpos) print(self.rect) def show_text(screen,pos,text,color,font_bold = False,font_size = 60,font_italic = False): cur_font=pygame.font.SysFont('宋體',font_size) cur_font.set_bold(font_bold) cur_font.set_italic(font_italic) text_fmt = cur_font.render(text,1,color) screen.blit(text_fmt,pos) def main(): pygame.init() screen_size = (SCREEN_X,SCREEN_Y) screen = pygame.display.set_mode(screen_size) pygame.display.set_caption('Snake') clock=pygame.time.Clock() scores=0 isdead=False snake=Snake() food=Food() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: #print(event) #pressed_keys = pygame.key.get_pressed() #if pressed_keys.count(1)>1: # continue print(event.key) snake.changeddirection(event.key) if event.key == pygame.K_SPACE and isdead: return main() screen.fill((255,255,255)) if not isdead: scores +=1 snake.move() for rect in snake.body: pygame.draw.rect(screen,(20,220,39),rect,0) isdead=snake.isdead() if isdead: show_text(screen,(100,200),'you dead!',(227,29,18),False,100) show_text(screen,(150,260),'press space to try again...',(0,0,22),False,30) if food.rect == snake.body[0]: scores +=50 food.remove() snake.addnode() food.set() pygame.draw.rect(screen,(136,0,21),food.rect,0) show_text(screen,(50,500),'Scores:'+str(scores),(223,223,223)) pygame.display.update() clock.tick(5) if __name__ == "__main__": main()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
對(duì)Pytorch神經(jīng)網(wǎng)絡(luò)初始化kaiming分布詳解
今天小編就為大家分享一篇對(duì)Pytorch神經(jīng)網(wǎng)絡(luò)初始化kaiming分布詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08關(guān)于Python字典(Dictionary)操作詳解
這篇文章主要介紹了關(guān)于Python字典(Dictionary)操作詳解,Python字典是另一種可變?nèi)萜髂P停铱纱鎯?chǔ)任意類型對(duì)象,如字符串、數(shù)字、元組等其他容器模型,需要的朋友可以參考下2023-04-04使用Python實(shí)現(xiàn)為PDF文件添加圖章
在日常工作中,我們經(jīng)常需要給PDF文檔添加一些標(biāo)識(shí),比如公司的圖章或水印圖章,所以本文就來(lái)為大家詳細(xì)介紹一下如何使用Python實(shí)現(xiàn)為PDF文件添加圖章,需要的可以參考下2023-11-1110個(gè)有用的Python字符串函數(shù)小結(jié)
本文主要介紹了10個(gè)有用的Python字符串函數(shù)小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08用python寫個(gè)顏值評(píng)分器篩選最美主播
這篇文章主要介紹了我如何用python寫顏值評(píng)分器,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08如何利用Python+OpenCV實(shí)現(xiàn)簡(jiǎn)易圖像邊緣輪廓檢測(cè)(零基礎(chǔ))
輪廓是形狀分析和物體檢測(cè)和識(shí)別的有用工具,下面這篇文章主要給大家介紹了關(guān)于如何利用Python+OpenCV實(shí)現(xiàn)簡(jiǎn)易圖像邊緣輪廓檢測(cè)(零基礎(chǔ))的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05python模塊hashlib(加密服務(wù))知識(shí)點(diǎn)講解
在本篇文章里小編給大家分享的是關(guān)于python模塊hashlib(加密服務(wù))知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2019-11-11Python實(shí)現(xiàn)層次分析法及自調(diào)節(jié)層次分析法的示例
這篇文章主要介紹了Python實(shí)現(xiàn)層次分析法及自調(diào)節(jié)層次分析法的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Python使用pyppeteer進(jìn)行網(wǎng)頁(yè)截圖并發(fā)送機(jī)器人實(shí)例
這篇文章主要介紹了Python使用pyppeteer進(jìn)行網(wǎng)頁(yè)截圖并發(fā)送機(jī)器人實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04