欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)智能貪吃蛇游戲的示例代碼

 更新時間:2022年07月20日 08:30:47   作者:python  
我想大家都玩過諾基亞上面的貪吃蛇吧,這篇文章將帶你一步步用python語言實(shí)現(xiàn)一個snake小游戲,文中的示例代碼講解詳細(xì),感興趣的可以了解一下

前言

我想大家都玩過諾基亞上面的貪吃蛇吧,本文將帶你一步步用python語言實(shí)現(xiàn)一個snake小游戲。

基本環(huán)境配置

版本:Python3

系統(tǒng):Windows

相關(guān)模塊:pygame

pip install pygame安裝即可

實(shí)現(xiàn)效果

實(shí)現(xiàn)代碼

import?random,?pygame,?sys
from?pygame.locals?import?*
import?time
'''
'''
FPS?=?1
##WINDOWWIDTH?=?640
#WINDOWHEIGHT?=?480
WINDOWWIDTH?=?600
WINDOWHEIGHT?=?480
CELLSIZE?=?40
assert?WINDOWWIDTH?%?CELLSIZE?==?0,?"Window?width?must?be?a?multiple?of?cell?size."
assert?WINDOWHEIGHT?%?CELLSIZE?==?0,?"Window?height?must?be?a?multiple?of?cell?size."
CELLWIDTH?=?int(WINDOWWIDTH?/?CELLSIZE)
CELLHEIGHT?=?int(WINDOWHEIGHT?/?CELLSIZE)?

#?????????????R????G????B
WHITE?????=?(255,?255,?255)
BLACK?????=?(??0,???0,???0)
RED???????=?(255,???0,???0)
GREEN?????=?(??0,?255,???0)
DARKGREEN?=?(??0,?155,???0)
DARKGRAY??=?(?40,??40,??40)
BGCOLOR?=?BLACK



UP?=?'up'
DOWN?=?'down'
LEFT?=?'left'
RIGHT?=?'right'

direction?=?UP
DIRECTION?=?[UP,DOWN,LEFT,RIGHT]

HEAD?=?0?#?syntactic?sugar:?index?of?the?worm's?head



distance?=?[]

for?y?in?range(CELLHEIGHT):
????distance.append([])
????for?x?in?range(CELLWIDTH):
????????distance[y].append(8888)

def?into_queue(grid,?queue,?visited,?worm,apple):
????x,y?=?grid
????if?(x,?y)?==?(apple['x'],apple['y']):
????????return?False
????elif?x?<?0?or?x?>=?CELLWIDTH:
????????return?False
????elif?y?<?0?or?y?>=?CELLHEIGHT:
????????return?False
????elif?(x,?y)?in?queue:
????????return?False
????elif?(x,?y)?in?visited:
????????return?False
????else:
????????return?True

def?is_snake(x,y,worm):
????for?body?in?worm:
????????if?body['x']?==?x?and?body['y']?==?y:
????????????return?True
????return?False


def?cal_distance(worm,apple):
????queue?=?[(apple['x'],apple['y'])]
????visited?=?[]
????found?=?False
????for?y?in?range(CELLHEIGHT):
????????for?x?in?range(CELLWIDTH):
????????????distance[y][x]?=?9999

????distance[apple['y']][apple['x']]?=?0

????while?len(queue)?!=?0:
????????head?=?queue[0]
????????visited.append(head)
????????up_grid?=?head[0],?head[1]?-?1
????????down_grid?=?head[0],?head[1]?+?1
????????left_grid?=?head[0]?-?1,?head[1]
????????right_grid?=?head[0]?+?1,?head[1]

????????for?grid?in?[up_grid,?down_grid,?left_grid,?right_grid]:
????????????if?into_queue(grid,?queue,?visited,worm,apple):
????????????????if?grid[0]?==?worm[HEAD]['x']?and?grid[1]?==?worm[HEAD]['y']:
????????????????????found?=?True
????????????????if?not?is_snake(grid[0],grid[1],worm):
????????????????????queue.append(grid)
????????????????????distance[grid[1]][grid[0]]?=?distance[head[1]][head[0]]?+?1
????????queue.pop(0)
????return?found

def?main():
????global?FPSCLOCK,?DISPLAYSURF,?BASICFONT

????pygame.init()
????FPSCLOCK?=?pygame.time.Clock()
????DISPLAYSURF?=?pygame.display.set_mode((WINDOWWIDTH,?WINDOWHEIGHT))
????BASICFONT?=?pygame.font.Font('freesansbold.ttf',?18)
????pygame.display.set_caption('Snaky')

????showStartScreen()
????while?True:
????????runGame()
????????showGameOverScreen()

def?can_move(grid,?worm):
????x,y?=?grid
????if?x?<?0?or?x?>=?CELLWIDTH:
????????return?False
????elif?y?<?0?or?y?>=?CELLHEIGHT:
????????return?False
????elif?is_snake(x,?y,worm):
????????return?False
????elif?(x,?y)?==?(worm[HEAD]['x'],?worm[HEAD]['y']):
????????return?False
????else:
????????return?True


def?update_dirc(now,?direc):
????loc?=?{'x':0,'y':0}
????if?direc?==?UP:
????????loc?=?{'x':now['x'],'y':now['y']-1}
????elif?direc?==?DOWN:
????????loc?=?{'x':now['x'],'y':now['y']+1}
????elif?direc?==?RIGHT:
????????loc?=?{'x':now['x']+1,'y':now['y']}
????elif?direc?==?LEFT:
????????loc?=?{'x':now['x']-1,'y':now['y']}
????return?loc

def?virtual_run(wormCoords,?apple,direction):
????wormCoords?=?list(wormCoords)
????food_eated?=?False
????while?not?food_eated:
????????cal_distance(wormCoords,apple)
????????four_dis?=?[99999,?99999,?99999,?99999]
????????if?can_move((wormCoords[HEAD]['x'],?wormCoords[HEAD]['y']?-?1),?wormCoords):
????????????four_dis[0]?=?distance[wormCoords[HEAD]['y']?-?1][wormCoords[HEAD]['x']]

????????if?can_move((wormCoords[HEAD]['x']?+?1,?wormCoords[HEAD]['y']),?wormCoords):
????????????four_dis[1]?=?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?+?1]

????????if?can_move((wormCoords[HEAD]['x'],?wormCoords[HEAD]['y']?+?1),?wormCoords):
????????????four_dis[2]?=?distance[wormCoords[HEAD]['y']?+?1][wormCoords[HEAD]['x']]

????????if?can_move((wormCoords[HEAD]['x']?-?1,?wormCoords[HEAD]['y']),?wormCoords):
????????????four_dis[3]?=?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?-?1]

????????min_num?=?min(four_dis)

????????if?four_dis[0]?<?99999?and?distance[wormCoords[HEAD]['y']?-?1][wormCoords[HEAD]['x']]?==?min_num?and?direction?!=?DOWN:
????????????direction?=?UP

????????elif?four_dis[1]?<?99999?and?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?+?1]?==?min_num?and?direction?!=?"LEFT":
????????????direction?=?RIGHT

????????elif?four_dis[2]?<?99999?and?distance[wormCoords[HEAD]['y']?+?1][wormCoords[HEAD]['x']]?==?min_num?and?direction?!=?"UP":
????????????direction?=?DOWN

????????elif?four_dis[3]?<?99999?and?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?-?1]?==?min_num?and?direction?!=?RIGHT:
????????????direction?=?LEFT
????????if?wormCoords[HEAD]['x']?==?-1?or?wormCoords[HEAD]['x']?==?CELLWIDTH?or?wormCoords[HEAD]['y']?==?-1?or?wormCoords[HEAD]['y']?==?CELLHEIGHT:
????????????return?#?game?over
????????for?wormBody?in?wormCoords[1:]:
????????????if?wormBody['x']?==?wormCoords[HEAD]['x']?and?wormBody['y']?==?wormCoords[HEAD]['y']:
????????????????return

????????#?move?the?worm?by?adding?a?segment?in?the?direction?it?is?moving
????????if?direction?==?UP:
????????????newHead?=?{'x':?wormCoords[HEAD]['x'],?'y':?wormCoords[HEAD]['y']?-?1}
????????elif?direction?==?DOWN:
????????????newHead?=?{'x':?wormCoords[HEAD]['x'],?'y':?wormCoords[HEAD]['y']?+?1}
????????elif?direction?==?LEFT:
????????????newHead?=?{'x':?wormCoords[HEAD]['x']?-?1,?'y':?wormCoords[HEAD]['y']}
????????elif?direction?==?RIGHT:
????????????newHead?=?{'x':?wormCoords[HEAD]['x']?+?1,?'y':?wormCoords[HEAD]['y']}
????????if?wormCoords[HEAD]['x']?!=?apple['x']?or?wormCoords[HEAD]['y']?!=?apple['y']:
????????????food_eated?=?True
????????????wormCoords.insert(0,?newHead)
????????else:
????????????del?wormCoords[-1]?#?remove?worm's?tail?segment
????????????wormCoords.insert(0,?newHead)
????result?=?cal_distance(wormCoords,wormCoords[-1])
????for?i?in?range(4):
????????temp?=?update_dirc(wormCoords[HEAD],DIRECTION[i])
????????if?temp['x']?==?wormCoords[-1]['x']?and?temp['y']?==?wormCoords[-1]['y']:
????????????result?=?False
????return?result

def?distance_(x,y):
????return?abs(x['x']-y['x'])?+?abs(x['y']?-?x['y'])


def?any_possible_move(worm,apple,direction):
????temp_direc?=?direction
????max_dis?=?0
????for?i?in?range(4):
????????temp?=?update_dirc(worm[0],DIRECTION[i])
????????if?can_move((temp['x'],temp['y']),worm):
????????????if?(distance_(temp,?apple)?>?max_dis)?and?(examine_direction(DIRECTION[i],?direction)):
????????????????max_dis?=?distance_(temp,?apple)
????????????????temp_direc?=?DIRECTION[i]
????return?temp_direc

def?examine_direction(temp?,?direction):
????if?direction?==?UP:
????????if?temp?==?DOWN:
????????????return?False
????elif?direction?==?RIGHT:
????????if?temp?==?LEFT:
????????????return?False
????elif?direction?==?LEFT:
????????if?temp?==?RIGHT:
????????????return?False
????elif?direction?==?DOWN:
????????if?temp?==?UP:
????????????return?False
????return?True

def?check_head(worm,direction):
????for?i?in?range(4):
????????temp?=?update_dirc(worm[HEAD],?DIRECTION[i])
????????if?can_move((temp['x'],temp['y']),worm)?and?examine_direction(DIRECTION[i],direction):
????????????if?distance[temp['y']][temp['x']]?<?9999:
????????????????return?True
????return?False


def?runGame():
????global?running_,DIRECTION
????#?Set?a?random?start?point.
????startx?=?random.randint(0,?CELLWIDTH?-1)
????starty?=?random.randint(0,?CELLHEIGHT?-1)
????wormCoords?=?[{'x':?startx,?????'y':?starty},
??????????????????{'x':?startx?-?1,?'y':?starty},
??????????????????{'x':?startx?-?2,?'y':?starty}]
????direction?=?RIGHT
????running_?=?True
????#?Start?the?apple?in?a?random?place.
????apple?=?getRandomLocation(wormCoords)
????count?=?0
????while?True:?#?main?game?loop
????????for?event?in?pygame.event.get():?#?event?handling?loop
????????????if?event.type?==?QUIT:
????????????????terminate()
????????new_direction?=?None
????????#print?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']]
????????if?cal_distance(wormCoords,apple):
????????????#print?"Test"
????????????if?virtual_run(wormCoords,?apple,?direction):
????????????????cal_distance(wormCoords,apple)
????????????????four_dis?=?[99999]?*?4
????????????????if?can_move((wormCoords[HEAD]['x'],?wormCoords[HEAD]['y']?-?1),?wormCoords):
????????????????????four_dis[0]?=?distance[wormCoords[HEAD]['y']?-?1][wormCoords[HEAD]['x']]

????????????????if?can_move((wormCoords[HEAD]['x']?+?1,?wormCoords[HEAD]['y']),?wormCoords):
????????????????????four_dis[1]?=?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?+?1]

????????????????if?can_move((wormCoords[HEAD]['x'],?wormCoords[HEAD]['y']?+?1),?wormCoords):
????????????????????four_dis[2]?=?distance[wormCoords[HEAD]['y']?+?1][wormCoords[HEAD]['x']]

????????????????if?can_move((wormCoords[HEAD]['x']?-?1,?wormCoords[HEAD]['y']),?wormCoords):
????????????????????four_dis[3]?=?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?-?1]

????????????????max_num?=?min(four_dis)

????????????????if?four_dis[0]?<?99999?and?distance[wormCoords[HEAD]['y']?-?1][wormCoords[HEAD]['x']]?==?max_num?and?direction?!=?DOWN:
????????????????????new_direction?=?UP

????????????????elif?four_dis[1]?<?99999?and?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?+?1]?==?max_num?and?direction?!=?LEFT:
????????????????????new_direction?=?RIGHT

????????????????elif?four_dis[2]?<?99999?and?distance[wormCoords[HEAD]['y']?+?1][wormCoords[HEAD]['x']]?==?max_num?and?direction?!=?UP:
????????????????????new_direction?=?DOWN

????????????????elif?four_dis[3]?<?99999?and?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?-?1]?==?max_num?and?direction?!=?RIGHT:
????????????????????new_direction?=?LEFT
????????????else:
????????????????count?+=?1
????????????????print(count)
????????????????four_dis?=?[-1]?*?4
????????????????cal_distance(wormCoords,?wormCoords[-1])
????????????????if?can_move((wormCoords[HEAD]['x'],?wormCoords[HEAD]['y']?-?1),?wormCoords):
????????????????????four_dis[0]?=?distance[wormCoords[HEAD]['y']?-?1][wormCoords[HEAD]['x']]

????????????????if?can_move((wormCoords[HEAD]['x']?+?1,?wormCoords[HEAD]['y']),?wormCoords):
????????????????????four_dis[1]?=?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?+?1]

????????????????if?can_move((wormCoords[HEAD]['x'],?wormCoords[HEAD]['y']?+?1),?wormCoords):
????????????????????four_dis[2]?=?distance[wormCoords[HEAD]['y']?+?1][wormCoords[HEAD]['x']]

????????????????if?can_move((wormCoords[HEAD]['x']?-?1,?wormCoords[HEAD]['y']),?wormCoords):
????????????????????four_dis[3]?=?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?-?1]

????????????????max_num?=?0
????????????????for?i?in?four_dis:
????????????????????if?i?!=?9999:
????????????????????????if?i?>?max_num:
????????????????????????????max_num?=?i

????????????????if?four_dis[0]?>?-1?and?distance[wormCoords[HEAD]['y']?-?1][wormCoords[HEAD]['x']]?==?max_num?and?direction?!=?DOWN:
????????????????????new_direction?=?UP

????????????????elif?four_dis[1]?>?-1?and?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?+?1]?==?max_num?and?direction?!=?LEFT:
????????????????????new_direction?=?RIGHT

????????????????elif?four_dis[2]?>?-1?and?distance[wormCoords[HEAD]['y']?+?1][wormCoords[HEAD]['x']]?==?max_num?and?direction?!=?UP:
????????????????????new_direction?=?DOWN

????????????????elif?four_dis[3]?>?-1?and?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?-?1]?==?max_num?and?direction?!=?RIGHT:
????????????????????new_direction?=?LEFT
????????????????if?count?==?10:
????????????????????new_direction?=?any_possible_move(wormCoords,?apple,?direction)
????????????????????count?=?0
????????else:
????????????four_dis?=?[-1]?*?4
????????????cal_distance(wormCoords,?wormCoords[-1])
????????????if?can_move((wormCoords[HEAD]['x'],?wormCoords[HEAD]['y']?-?1),?wormCoords):
????????????????four_dis[0]?=?distance[wormCoords[HEAD]['y']?-?1][wormCoords[HEAD]['x']]

????????????if?can_move((wormCoords[HEAD]['x']?+?1,?wormCoords[HEAD]['y']),?wormCoords):
????????????????four_dis[1]?=?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?+?1]

????????????if?can_move((wormCoords[HEAD]['x'],?wormCoords[HEAD]['y']?+?1),?wormCoords):
????????????????four_dis[2]?=?distance[wormCoords[HEAD]['y']?+?1][wormCoords[HEAD]['x']]

????????????if?can_move((wormCoords[HEAD]['x']?-?1,?wormCoords[HEAD]['y']),?wormCoords):
????????????????four_dis[3]?=?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?-?1]

????????????max_num?=?0
????????????for?i?in?four_dis:
????????????????if?i?!=?9999:
????????????????????if?i?>?max_num:
????????????????????????max_num?=?i

????????????if?four_dis[0]?>?-1?and?distance[wormCoords[HEAD]['y']?-?1][wormCoords[HEAD]['x']]?==?max_num?and?direction?!=?DOWN:
????????????????new_direction?=?UP

????????????elif?four_dis[1]?>?-1?and?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?+?1]?==?max_num?and?direction?!=?LEFT:
????????????????new_direction?=?RIGHT

????????????elif?four_dis[2]?>?-1?and?distance[wormCoords[HEAD]['y']?+?1][wormCoords[HEAD]['x']]?==?max_num?and?direction?!=?UP:
????????????????new_direction?=?DOWN

????????????elif?four_dis[3]?>?-1?and?distance[wormCoords[HEAD]['y']][wormCoords[HEAD]['x']?-?1]?==?max_num?and?direction?!=?RIGHT:
????????????????new_direction?=?LEFT
????????if?new_direction?==?None:
????????????direction?=?any_possible_move(wormCoords,?apple,?direction)
????????else:
????????????direction?=?new_direction
????????#temp_?=?update_dirc(wormCoords[HEAD],direction)
????????#while?not?can_move((temp_['x'],temp_['y']),?wormCoords):
????????????#direction?=?any_possible_move(wormCoords,?apple,?direction)
????????#?check?if?the?worm?has?hit?itself?or?the?edge
????????if?wormCoords[HEAD]['x']?==?-1?or?wormCoords[HEAD]['x']?==?CELLWIDTH?or?wormCoords[HEAD]['y']?==?-1?or?wormCoords[HEAD]['y']?==?CELLHEIGHT:
????????????return?#?game?over
????????for?wormBody?in?wormCoords[1:]:
????????????if?wormBody['x']?==?wormCoords[HEAD]['x']?and?wormBody['y']?==?wormCoords[HEAD]['y']:
????????????????return?#?game?over

????????#?check?if?worm?has?eaten?an?apply
????????if?wormCoords[HEAD]['x']?==?apple['x']?and?wormCoords[HEAD]['y']?==?apple['y']:
????????????#?don't?remove?worm's?tail?
????????????apple?=?getRandomLocation(wormCoords)
????????else:
????????????del?wormCoords[-1]?#?remove?worm's?tail?segment

????????#?move?the?worm?by?adding?a?segment?in?the?direction?it?is?moving
????????if?direction?==?UP:
????????????newHead?=?{'x':?wormCoords[HEAD]['x'],?'y':?wormCoords[HEAD]['y']?-?1}
????????elif?direction?==?DOWN:
????????????newHead?=?{'x':?wormCoords[HEAD]['x'],?'y':?wormCoords[HEAD]['y']?+?1}
????????elif?direction?==?LEFT:
????????????newHead?=?{'x':?wormCoords[HEAD]['x']?-?1,?'y':?wormCoords[HEAD]['y']}
????????elif?direction?==?RIGHT:
????????????newHead?=?{'x':?wormCoords[HEAD]['x']?+?1,?'y':?wormCoords[HEAD]['y']}
????????wormCoords.insert(0,?newHead)?#?set?a?new?apple?somewhere
????????DISPLAYSURF.fill(BGCOLOR)
????????drawGrid()
????????drawWorm(wormCoords)
????????drawApple(apple)
????????drawScore(len(wormCoords)?-?3)
????????time.sleep(0.01)
????????pygame.display.update()
????????#FPSCLOCK.tick(FPS)

def?drawPressKeyMsg():
????pressKeySurf?=?BASICFONT.render('Press?a?key?to?play.',?True,?DARKGRAY)
????pressKeyRect?=?pressKeySurf.get_rect()
????pressKeyRect.topleft?=?(WINDOWWIDTH?-?200,?WINDOWHEIGHT?-?30)
????DISPLAYSURF.blit(pressKeySurf,?pressKeyRect)


def?checkForKeyPress():
????if?len(pygame.event.get(QUIT))?>?0:
????????terminate()

????keyUpEvents?=?pygame.event.get(KEYUP)
????if?len(keyUpEvents)?==?0:
????????return?None
????if?keyUpEvents[0].key?==?K_ESCAPE:
????????terminate()
????return?keyUpEvents[0].key


def?showStartScreen():
????titleFont?=?pygame.font.Font('freesansbold.ttf',?100)
????titleSurf1?=?titleFont.render('Snaky!',?True,?WHITE,?DARKGREEN)
????titleSurf2?=?titleFont.render('Snaky!',?True,?GREEN)

????degrees1?=?0
????degrees2?=?0
????while?True:
????????DISPLAYSURF.fill(BGCOLOR)
????????rotatedSurf1?=?pygame.transform.rotate(titleSurf1,?degrees1)
????????rotatedRect1?=?rotatedSurf1.get_rect()
????????rotatedRect1.center?=?(WINDOWWIDTH?/?2,?WINDOWHEIGHT?/?2)
????????DISPLAYSURF.blit(rotatedSurf1,?rotatedRect1)

????????rotatedSurf2?=?pygame.transform.rotate(titleSurf2,?degrees2)
????????rotatedRect2?=?rotatedSurf2.get_rect()
????????rotatedRect2.center?=?(WINDOWWIDTH?/?2,?WINDOWHEIGHT?/?2)
????????DISPLAYSURF.blit(rotatedSurf2,?rotatedRect2)

????????drawPressKeyMsg()

????????if?checkForKeyPress():
????????????pygame.event.get()?#?clear?event?queue
????????????return
????????pygame.display.update()
????????FPSCLOCK.tick(FPS)
????????degrees1?+=?3?#?rotate?by?3?degrees?each?frame
????????degrees2?+=?7?#?rotate?by?7?degrees?each?frame
????????time.sleep(0.1)


def?terminate():
????pygame.quit()
????sys.exit()


def?getRandomLocation(worm):
????temp?=?{'x':?random.randint(0,?CELLWIDTH?-?1),?'y':?random.randint(0,?CELLHEIGHT?-?1)}
????while?test_not_ok(temp,?worm):
????????temp?=?{'x':?random.randint(0,?CELLWIDTH?-?1),?'y':?random.randint(0,?CELLHEIGHT?-?1)}
????return?temp

def?test_not_ok(temp,?worm):
????for?body?in?worm:
????????if?temp['x']?==?body['x']?and?temp['y']?==?body['y']:
????????????return?True
????return?False


def?showGameOverScreen():
????gameOverFont?=?pygame.font.Font('freesansbold.ttf',?150)
????gameSurf?=?gameOverFont.render('Game',?True,?WHITE)
????overSurf?=?gameOverFont.render('Over',?True,?WHITE)
????gameRect?=?gameSurf.get_rect()
????overRect?=?overSurf.get_rect()
????gameRect.midtop?=?(WINDOWWIDTH?/?2,?10)
????overRect.midtop?=?(WINDOWWIDTH?/?2,?gameRect.height?+?10?+?25)

????DISPLAYSURF.blit(gameSurf,?gameRect)
????DISPLAYSURF.blit(overSurf,?overRect)
????drawPressKeyMsg()
????pygame.display.update()
????pygame.time.wait(5)
????checkForKeyPress()?#?clear?out?any?key?presses?in?the?event?queue

????while?True:
????????if?checkForKeyPress():
????????????pygame.event.get()?#?clear?event?queue
????????????return

def?drawScore(score):
????scoreSurf?=?BASICFONT.render('Score:?%s'?%?(score),?True,?WHITE)
????scoreRect?=?scoreSurf.get_rect()
????scoreRect.topleft?=?(WINDOWWIDTH?-?120,?10)
????DISPLAYSURF.blit(scoreSurf,?scoreRect)


def?drawWorm(wormCoords):
????for?coord?in?wormCoords:
????????x?=?coord['x']?*?CELLSIZE
????????y?=?coord['y']?*?CELLSIZE
????????wormSegmentRect?=?pygame.Rect(x,?y,?CELLSIZE,?CELLSIZE)
????????pygame.draw.rect(DISPLAYSURF,?DARKGREEN,?wormSegmentRect)
????????wormInnerSegmentRect?=?pygame.Rect(x?+?4,?y?+?4,?CELLSIZE?-?8,?CELLSIZE?-?8)
????????pygame.draw.rect(DISPLAYSURF,?GREEN,?wormInnerSegmentRect)


def?drawApple(coord):
????x?=?coord['x']?*?CELLSIZE
????y?=?coord['y']?*?CELLSIZE
????appleRect?=?pygame.Rect(x,?y,?CELLSIZE,?CELLSIZE)
????pygame.draw.rect(DISPLAYSURF,?RED,?appleRect)


def?drawGrid():
????for?x?in?range(0,?WINDOWWIDTH,?CELLSIZE):?#?draw?vertical?lines
????????pygame.draw.line(DISPLAYSURF,?DARKGRAY,?(x,?0),?(x,?WINDOWHEIGHT))
????for?y?in?range(0,?WINDOWHEIGHT,?CELLSIZE):?#?draw?horizontal?lines
????????pygame.draw.line(DISPLAYSURF,?DARKGRAY,?(0,?y),?(WINDOWWIDTH,?y))

running_?=?True

if?__name__?==?'__main__':
????main()

到此這篇關(guān)于Python實(shí)現(xiàn)智能貪吃蛇游戲的示例代碼的文章就介紹到這了,更多相關(guān)Python貪吃蛇游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python通用驗(yàn)證碼識別OCR庫之ddddocr驗(yàn)證碼識別

    Python通用驗(yàn)證碼識別OCR庫之ddddocr驗(yàn)證碼識別

    dddd_ocr也是一個用于識別驗(yàn)證碼的開源庫,又名帶帶弟弟ocr,爬蟲界大佬sml2h3開發(fā),識別效果也是非常不錯,下面這篇文章主要給大家介紹了關(guān)于Python通用驗(yàn)證碼識別OCR庫之ddddocr驗(yàn)證碼識別的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • Python 實(shí)現(xiàn)打印單詞的菱形字符圖案

    Python 實(shí)現(xiàn)打印單詞的菱形字符圖案

    這篇文章主要介紹了Python 實(shí)現(xiàn)打印單詞的菱形字符圖案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • 詳解TensorFlow2實(shí)現(xiàn)前向傳播

    詳解TensorFlow2實(shí)現(xiàn)前向傳播

    這篇文章主要介紹了TensorFlow2如何實(shí)現(xiàn)前向傳播,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Python生成器定義與簡單用法實(shí)例分析

    Python生成器定義與簡單用法實(shí)例分析

    這篇文章主要介紹了Python生成器定義與簡單用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python生成器的概念、原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2018-04-04
  • Flask框架運(yùn)用Ajax實(shí)現(xiàn)數(shù)據(jù)交互的示例代碼

    Flask框架運(yùn)用Ajax實(shí)現(xiàn)數(shù)據(jù)交互的示例代碼

    使用Ajax技術(shù)網(wǎng)頁應(yīng)用能夠快速地將增量更新呈現(xiàn)在用戶界面上,而不需要重載刷新整個頁面,這使得程序能夠更快地回應(yīng)用戶的操作,本文將簡單介紹使用AJAX如何實(shí)現(xiàn)前后端數(shù)據(jù)通信
    2022-11-11
  • Python制作動態(tài)字符圖的實(shí)例

    Python制作動態(tài)字符圖的實(shí)例

    今天小編就為大家分享一篇關(guān)于Python制作動態(tài)字符圖的實(shí)例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 對numpy中向量式三目運(yùn)算符詳解

    對numpy中向量式三目運(yùn)算符詳解

    今天小編就為大家分享一篇對numpy中向量式三目運(yùn)算符詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python?selenium把歌詞評論做成詞云圖

    Python?selenium把歌詞評論做成詞云圖

    大家好,本篇文章主要講的是Python?selenium把歌詞評論做成詞云圖,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • 在Python中定義一個常量的方法

    在Python中定義一個常量的方法

    今天小編就為大家分享一篇在Python中定義一個常量的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • 保姆級python教程寫個貪吃蛇大冒險

    保姆級python教程寫個貪吃蛇大冒險

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)雙人模式的貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評論