0% found this document useful (0 votes)
24 views6 pages

Ular

Uploaded by

Tita Marlina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

Ular

Uploaded by

Tita Marlina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import random

import curses

s = curses.initscr()

curses.curs_set(0)

sh, sw = s.getmaxyx()

w = curses.newwin(sh, sw, 0, 0)

w.keypad(1)

w.timeout(100)

snk_x = sw/4

snk_y = sh/2

snake =[

[snk_y,snk_x],

[snk_y,snk_x-1],

[snk_y,snk_x-2]

food = [sh/2, sw/2]

w.addch(food[0], food[1], curses.ACS_PI)

key = curses.KEY_RIGHT

while True:

next_key = w.getch()

key = key if next_key == -1 else next_key

if snake[0][0] in [0, sh] or snake[0][1] in [0, sw] or snake[0] in snake[1:]:

curses.endwin()

quit()

new_head = [snake[0][0], snake[0][1]]


if key == curses.KEY_DOWN:

new_head[0] +=1

if key == curses.KEY_UP:

new_head[0] -=1

if key == curses.KEY_LEFT:

new_head[1] -=1

if key == curses.KEY_RIGHT:

new_head[1] +=1

snake.insert(0, new_head)

if snake[0]== food:

food = None

while food is None:

nf = [

random.randint(1, sh-1),

random.randint(1, sw-1)

food = nf if nf not in snake else None

w.addch(food[0],food[1],curses.ACS_PI)

else:

tail = snake.pop()

w.addch(tail[0], tail[1], ' ')

w.addch(snake[0][0], snake[0][1], curses.ACS_CKBOARD)


# SNAKES GAME

# Use ARROW KEYS to play, SPACE BAR for pausing/resuming and Esc Key for exiting

import curses

from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN

from random import randint

curses.initscr()

win = curses.newwin(20, 60, 0, 0)

win.keypad(1)

curses.noecho()

curses.curs_set(0)

win.border(0)

win.nodelay(1)

key = KEY_RIGHT # Initializing values

score = 0

snake = [[4,10], [4,9], [4,8]] # Initial snake co-ordinates

food = [10,20] # First food co-ordinates

win.addch(food[0], food[1], '*') # Prints the food


while key != 27: # While Esc key is not pressed

win.border(0)

win.addstr(0, 2, 'Score : ' + str(score) + ' ') # Printing 'Score' and

win.addstr(0, 27, ' SNAKE ') # 'SNAKE' strings

win.timeout(150 - (len(snake)/5 + len(snake)/10)%120) # Increases the speed of Snake as its


length increases

prevKey = key # Previous key pressed

event = win.getch()

key = key if event == -1 else event

if key == ord(' '): # If SPACE BAR is pressed, wait for another

key = -1 # one (Pause/Resume)

while key != ord(' '):

key = win.getch()

key = prevKey

continue

if key not in [KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, 27]: # If an invalid key is pressed

key = prevKey

# Calculates the new coordinates of the head of the snake. NOTE: len(snake) increases.

# This is taken care of later at [1].

snake.insert(0, [snake[0][0] + (key == KEY_DOWN and 1) + (key == KEY_UP and -1), snake[0][1] + (key
== KEY_LEFT and -1) + (key == KEY_RIGHT and 1)])
# If snake crosses the boundaries, make it enter from the other side

if snake[0][0] == 0: snake[0][0] = 18

if snake[0][1] == 0: snake[0][1] = 58

if snake[0][0] == 19: snake[0][0] = 1

if snake[0][1] == 59: snake[0][1] = 1

# Exit if snake crosses the boundaries (Uncomment to enable)

#if snake[0][0] == 0 or snake[0][0] == 19 or snake[0][1] == 0 or snake[0][1] == 59: break

# If snake runs over itself

if snake[0] in snake[1:]: break

if snake[0] == food: # When snake eats the food

food = []

score += 1

while food == []:

food = [randint(1, 18), randint(1, 58)] # Calculating next food's coordinates

if food in snake: food = []

win.addch(food[0], food[1], '*')

else:

last = snake.pop() # [1] If it does not eat the food, length decreases

win.addch(last[0], last[1], ' ')

win.addch(snake[0][0], snake[0][1], '#')


curses.endwin()

print("\nScore - " + str(score))

print("http://bitemelater.in\n")

You might also like