請問用python3寫貪吃蛇時,怎麼只用左右兩個鍵控制蛇上下左右四個方向轉彎?

具體就是,如果蛇現在向右移動,那麼按left鍵應該向上轉彎,按right鍵向下轉彎,其他三個方向同理。都是根據蛇現在的移動方向來確定轉彎方向。


幫你找到一段代碼,自己去研究吧。

import random
import pygame
import sys
from pygame.locals import *

Snakespeed = 17
Window_Width = 800
Window_Height = 500
Cell_Size = 20 # Width and height of the cells
# Ensuring that the cells fit perfectly in the window. eg if cell size was
# 10 and window width or windowheight were 15 only 1.5 cells would
# fit.
assert Window_Width % Cell_Size == 0, "Window width must be a multiple of cell size."
# Ensuring that only whole integer number of cells fit perfectly in the window.
assert Window_Height % Cell_Size == 0, "Window height must be a multiple of cell size."
Cell_W = int(Window_Width / Cell_Size) # Cell Width
Cell_H = int(Window_Height / Cell_Size) # Cellc Height

White = (255, 255, 255)
Black = (0, 0, 0)
Red = (255, 0, 0) # Defining element colors for the program.
Green = (0, 255, 0)
DARKGreen = (0, 155, 0)
DARKGRAY = (40, 40, 40)
YELLOW = (255, 255, 0)
Red_DARK = (150, 0, 0)
BLUE = (0, 0, 255)
BLUE_DARK = (0, 0, 150)

BGCOLOR = Black # Background color

UP = "up"
DOWN = "down" # Defining keyboard keys.
LEFT = "left"
RIGHT = "right"

HEAD = 0 # Syntactic sugar: index of the snake"s head

def main():
global SnakespeedCLOCK, DISPLAYSURF, BASICFONT

pygame.init()
SnakespeedCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((Window_Width, Window_Height))
BASICFONT = pygame.font.Font("freesansbold.ttf", 18)
pygame.display.set_caption("Snake")

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

def runGame():
# Set a random start point.
startx = random.randint(5, Cell_W - 6)
starty = random.randint(5, Cell_H - 6)
wormCoords = [{"x": startx, "y": starty},
{"x": startx - 1, "y": starty},
{"x": startx - 2, "y": starty}]
direction = RIGHT

# Start the apple in a random place.
apple = getRandomLocation()

while True: # main game loop
for event in pygame.event.get(): # event handling loop
if event.type == QUIT:
terminate()
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
elif event.key == K_ESCAPE:
terminate()

# check if the Snake has hit itself or the edge
if wormCoords[HEAD]["x"] == -1 or wormCoords[HEAD]["x"] == Cell_W or wormCoords[HEAD]["y"] == -1 or wormCoords[HEAD]["y"] == Cell_H:
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 Snake has eaten an apply
if wormCoords[HEAD]["x"] == apple["x"] and wormCoords[HEAD]["y"] == apple["y"]:
# don"t remove worm"s tail segment
apple = getRandomLocation() # set a new apple somewhere
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)
DISPLAYSURF.fill(BGCOLOR)
drawGrid()
drawWorm(wormCoords)
drawApple(apple)
drawScore(len(wormCoords) - 3)
pygame.display.update()
SnakespeedCLOCK.tick(Snakespeed)

def drawPressKeyMsg():
pressKeySurf = BASICFONT.render("Press a key to play.", True, White)
pressKeyRect = pressKeySurf.get_rect()
pressKeyRect.topleft = (Window_Width - 200, Window_Height - 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("Snake!", True, White, DARKGreen)
degrees1 = 0
degrees2 = 0
while True:
DISPLAYSURF.fill(BGCOLOR)
rotatedSurf1 = pygame.transform.rotate(titleSurf1, degrees1)
rotatedRect1 = rotatedSurf1.get_rect()
rotatedRect1.center = (Window_Width / 2, Window_Height / 2)
DISPLAYSURF.blit(rotatedSurf1, rotatedRect1)

drawPressKeyMsg()

if checkForKeyPress():
pygame.event.get() # clear event queue
return
pygame.display.update()
SnakespeedCLOCK.tick(Snakespeed)
degrees1 += 3 # rotate by 3 degrees each frame
degrees2 += 7 # rotate by 7 degrees each frame

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

def getRandomLocation():
return {"x": random.randint(0, Cell_W - 1), "y": random.randint(0, Cell_H - 1)}

def showGameOverScreen():
gameOverFont = pygame.font.Font("freesansbold.ttf", 100)
gameSurf = gameOverFont.render("Game", True, White)
overSurf = gameOverFont.render("Over", True, White)
gameRect = gameSurf.get_rect()
overRect = overSurf.get_rect()
gameRect.midtop = (Window_Width / 2, 10)
overRect.midtop = (Window_Width / 2, gameRect.height + 10 + 25)

DISPLAYSURF.blit(gameSurf, gameRect)
DISPLAYSURF.blit(overSurf, overRect)
drawPressKeyMsg()
pygame.display.update()
pygame.time.wait(500)
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 = (Window_Width - 120, 10)
DISPLAYSURF.blit(scoreSurf, scoreRect)

def drawWorm(wormCoords):
for coord in wormCoords:
x = coord["x"] * Cell_Size
y = coord["y"] * Cell_Size
wormSegmentRect = pygame.Rect(x, y, Cell_Size, Cell_Size)
pygame.draw.rect(DISPLAYSURF, DARKGreen, wormSegmentRect)
wormInnerSegmentRect = pygame.Rect(
x + 4, y + 4, Cell_Size - 8, Cell_Size - 8)
pygame.draw.rect(DISPLAYSURF, Green, wormInnerSegmentRect)

def drawApple(coord):
x = coord["x"] * Cell_Size
y = coord["y"] * Cell_Size
appleRect = pygame.Rect(x, y, Cell_Size, Cell_Size)
pygame.draw.rect(DISPLAYSURF, Red, appleRect)

def drawGrid():
for x in range(0, Window_Width, Cell_Size): # draw vertical lines
pygame.draw.line(DISPLAYSURF, DARKGRAY, (x, 0), (x, Window_Height))
for y in range(0, Window_Height, Cell_Size): # draw horizontal lines
pygame.draw.line(DISPLAYSURF, DARKGRAY, (0, y), (Window_Width, y))

if __name__ == "__main__":
try:
main()
except SystemExit:
pass


如樓上所說:

directions = [0, 1, 2, 3] # 比如0123分別是上左下右
if left:
direction = (direction + 1) % 4
else:
direction = (direction - 1) % 4


分別用0到3的數字表示運動方向

按下left就+1,按下right就-1

處理一下邊界就可以了


要是想實現這種類似調整角度而改變方向的操作方法, 而不是直接通過疊加上下左右四個方向的向量來改變方向, 我們就應該想到這種數學模型是基於角度的. 就是一個像"極坐標系"的模型.

所以, 可以在策劃遊戲的底層邏輯之前, 先想好要實現的每一個功能應該用的模型. 實現什麼功能, 分成多少各類, 給什麼類什麼方法? 這些都是越早去想越好, 到時候寫代碼的時候就不用反覆修改, 因為面向對象的處理非常講究結構, 如果結構的先導設計做得好, 代碼和邏輯都會非常清晰, 寫起來十分舒暢.

換言之, 當我們有一個貪吃蛇的結點的類時, 例如, 我們把貪食蛇的移動設成一個類的方法, 實現這個方法有兩種思路, 一種思路是向量分解, 將v_x , v_y分別存作類的屬性, 移動時只需要坐標直接加上v_x, v_y, 這種思路是通過正交坐標系處理的; 同樣, 還有另外一種實現的方法, 就是設置一個速度和它的前進角度. v_rau,v_arg, 那麼, 顯而易見, 這種處理的方式就是用極坐標. 那麼每一幀, 蛇的移動的(x,y)都可以用三角函數算出來.

當你要實現左右兩個鍵變化蛇的方向, 只需要使v_arg變化就可以了, 反之, 如果上下左右直接操作的話, 就用正交坐標v_x, v_y設計.

要是沒有做好前期的策劃, 而是一邊寫一邊想的話, 最後儘管功能實現了, 也會看起來非常的不美觀, 各種元素過冗雜, 同樣的意思用多重系統去表達, 而且寫作的時候, 思路也會比較混亂.

----------來自一個不是科班出生的只是自學的人的口胡.

剛翻譯了一篇西方的教材, 寫出來的中文都一股從句味


按左鍵:蛇身逆時針旋轉90度。

按右鍵:蛇身順時針旋轉90度。


推薦閱讀:

python 使用 threading.thread(target="")指明函數入口,如果函數有返回值,如何得到這個返回值?
Python 3.3 中 input() 返回值類型是什麼?
#Python3# Beautifulsoup Mac安裝解決辦法?[自問自答已解決]
linux上現在裝著python2.6,我想把python替換為python3.4,我應該怎麼做?
推薦幾本Python3相關書籍?最好分一下基礎、進階、高級

TAG:Python | Python3x | Python開發 | Python編程 |