程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python production Games: 80 lines of code to achieve a simple Snake game

編輯:Python


The above is a screenshot of the implementation , Nonsense , Just start talking about the code

pos = {

'UP': (-1,0),
'DOWN':(+1,0),
'LEFT':(0,-1),
'RIGHT':(0,+1),
}
current = 'RIGHT'
snake = [[1,1],[1,2],[1,3],[1,4],[1,5]] #ex: [[1,1],[1,2],[1,3]] [y,x]
head = snake[-1]
snake_speed = 0.1
rany, ranx = -1, -1

To initialize some data , Snakes are represented by lists , The head of the snake is the last item on the list , Speed use time.sleep() Express , The lower the speed , Faster

def isover(head):
if snake.count(head) > 1: # Touch your body 
return True
if (head[0] == y-1) or (head[1] == x-1) or (head[0] == 0) or (head[1] == 0): # Hit the wall 
return True
return False

Decide if the game is over , Touch your body to end or touch the wall to end

def randomyx():
ranflag = False
while ranflag is False:
rany, ranx = random.randint(1,y-2) , random.randint(1,x-2)
if [rany,ranx] not in snake:
ranflag = True

Randomly generate snake eating points , Be careful : Generated points : It should be in the box and not on the snake

while isover(head) is False:
draw()
head = [ head[0] + pos[current][0] , head[1] + pos[current][1]]
snake.append(head)
if head[0] == rany and head[1] == ranx:
randomyx()
snake_speed = snake_speed - 0.01
else:
snake.pop(0)
time.sleep(snake_speed)

The above is the core logic code , The main idea is :

1. As long as the snake doesn't eat , Just join in , Tail drop ( Form the feeling of snake walking ); As long as the snake eats something , Just add it in , The tail doesn't go out ( Form the feeling of snake growing up ) <— Typical queues

2. Then consider current <-- The current direction of the snake , How to change

The first consideration here is an asynchronous situation ! Monitor mouse time asynchronously !

 t1 = threading.Thread(target=listening, args=(head,), name='listenkeyboard')
t1.start()
def listening(head):
while True:
c = stdscr.getch()
if c == curses.KEY_UP and current != 'DOWN':
current = 'UP'
elif c == curses.KEY_DOWN and current != 'UP':
current = 'DOWN'
elif c == curses.KEY_LEFT and current != 'RIGHT':
current = 'LEFT'
elif c == curses.KEY_RIGHT and current != 'LEFT':
current = 'RIGHT'

What needs to be noted here is : When the current direction is up , Cannot press —> Press the snake and you'll hit yourself ~

Finish writing the main logic , after Beautify the following interface ->

curses.start_color() # On color 
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_RED)# Define the color of the point 
curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLUE) # Define the color of the snake 
''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :711312441 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
for si in snake:
stdscr.addstr(si[0],si[1],'@',curses.color_pair(2))
stdscr.addstr(rany,ranx,' ',curses.color_pair(1))

such , A greedy Snake game is finished , The following is all the code of the implementation ,python3 snake.py, Play by yourself ~~

import time,curses,random,threading
pos = {

'UP': (-1,0),
'DOWN':(+1,0),
'LEFT':(0,-1),
'RIGHT':(0,+1),
}
current = 'RIGHT'
snake = [[1,1],[1,2],[1,3],[1,4],[1,5]] #ex: [[1,1],[1,2],[1,3]] [y,x]
head = snake[-1]
snake_speed = 0.1
rany, ranx = -1, -1
def isover(head):
if snake.count(head) > 1: # Touch your body 
return True
if (head[0] == y-1) or (head[1] == x-1) or (head[0] == 0) or (head[1] == 0): # Hit the wall 
return True
return False
def randomyx():
global rany,ranx
ranflag = False
while ranflag is False:
rany, ranx = random.randint(1,y-2) , random.randint(1,x-2)
if [rany,ranx] not in snake:
ranflag = True
def draw():
stdscr.erase()
stdscr.border()
for si in snake:
stdscr.addstr(si[0],si[1],'@',curses.color_pair(2))
stdscr.addstr(rany,ranx,' ',curses.color_pair(1))
stdscr.refresh()
def listening(head):
global current
while True:
c = stdscr.getch()
if c == curses.KEY_UP and current != 'DOWN':
current = 'UP'
elif c == curses.KEY_DOWN and current != 'UP':
current = 'DOWN'
elif c == curses.KEY_LEFT and current != 'RIGHT':
current = 'LEFT'
elif c == curses.KEY_RIGHT and current != 'LEFT':
current = 'RIGHT'
def loop(stdscr):
global head,snake_speed
randomyx()
t1 = threading.Thread(target=listening, args=(head,), name='listenkeyboard')
t1.start()
while isover(head) is False:
draw()
head = [ head[0] + pos[current][0] , head[1] + pos[current][1]]
snake.append(head)
if head[0] == rany and head[1] == ranx:
randomyx()
snake_speed = snake_speed - 0.01
else:
snake.pop(0)
time.sleep(snake_speed)
stdscr = curses.initscr()
curses.noecho() # No output - -
curses.cbreak() # Read now : I don't know for the moment - -
curses.start_color() # On color 
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_RED)# Define the color of the point 
curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLUE) # Define the color of the snake 
#init_pair(n,f,b) Change the color to n, Give Way f For the foreground view ,b Background color . The color is right 0 Natural black and white , It's not allowed to change .
stdscr.keypad(1) # Turn on keypad
stdscr.border()
curses.curs_set(0)
(y,x) = stdscr.getmaxyx()
curses.wrapper(loop)
curses.endwin()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved