Error in Python application - python

I have made a snakes game in Python which was referred from someplace, but am getting error.
The program snakes.py is given below:
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)
I am getting the following error on w.addch(food[0], food[1], curses.ACS_PI):
TypeError: integer argument expected, got float.
I am also usng python 3.6 version, so how to fix this code.

In Python 3 the slash operator produces a float, even if both its arguments were ints. So your line food = [sh/2, sw/2] produces a list of two floats.
You should use the double-slash operator which produces an int:
food = [sh//2, sw//2]

food = [sh/2, sw/2]
In Python 3, the division / operator always produces floating-point values.
You should consider using the integer division operator, //.

Related

How do I add my current code fit into a main menu screen with Python Curses?

I'm working on a snake game for a school assignment in Python Curses but the code I have written doesn't have a menu option and I am looking for one that will work for it. I really just need a play button to enter the game and an exit button to quit out of it. I've tried lots of methods but some either don't show up or I can't put the code in the selected button.
I have tried setting them in rows as I have seen two tutorials do. One required me to change my game code with a dif code to work but it didn't make any menu show up. (Code gotten from here https://www.youtube.com/watch?v=RXEOIAxgldw) The other had a simple menu which was very close to working as it did show up but didn't have a tutorial on how to connect it to the snake game as the same user had made a snake game as well. It's the one I had the most luck with but I can't figure out how to set it up so that when I chose "Play" it will play the game and when I chose "Exit" it will clear the game and stop running as when I select Exit the code doesn't stop and it goes back to a broken version of the snake game. (Code gotten from here https://www.youtube.com/watch?v=zwMsmBsC1GM)
Here is what my snake code looks like. All I need is a Play and Exit button and was wanting to know what would be the best way for that to work without altering my snake code too much. I'm a beginner to coding in curses so any help would be much appreciated.
`
import curses
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)
snake = [(4, 10), (4, 9), (4, 8)]
food = (10, 20)
score = 0
win.addch(food[0], food[1], '©')
ESC = 27
key = curses.KEY_RIGHT
while key != ESC:
win.addstr(0, 2, 'Score ' + str(score) + ' ')
win.timeout(150 - (len(snake)) // 5 + (len(snake) // 10 % 120))
event = win.getch()
prev_key = key
key = event if event != -1 else prev_key
if key not in [
curses.KEY_LEFT, curses.KEY_RIGHT, curses.KEY_UP, curses.KEY_DOWN,
ESC
]:
key = prev_key
y = snake[0][0]
x = snake[0][1]
if key == curses.KEY_DOWN:
y += 1
if key == curses.KEY_UP:
y -= 1
if key == curses.KEY_LEFT:
x -= 1
if key == curses.KEY_RIGHT:
x += 1
snake.insert(0, (y, x))
if y == 0: break
if y == 19: break
if x == 0: break
if x == 59: break
if snake[0] in snake[1:]: break
if snake[0] == food:
score += 1
food = ()
while food == ():
food = (randint(1, 18), randint(1, 58))
if food in snake:
food = ()
win.addch(food[0], food[1], '©')
else:
last = snake.pop()
win.addch(last[0], last[1], ' ')
win.addch(snake[0][0], snake[0][1], '※')
curses.endwin()
print(f"Game over! Final score= {score}")
`

how do i disappear food from inside snake with python?

I want to make the food disappear from the snake when it eats the food but the string stays visible inside snake making the snake look weird with the food in the snake... now what I been trying is this line of code:
Error ( 'tuple' object has no attribute 'replace' )
for food in snake:
food = food.replace(food, ' ')
I just started writting code not too long ago... i only know the basics of python but still confusing and I'm trying to learn as i work in this project to make the game better...
This is the python Script:
import curses
from mimetypes import init
from random import randint
WINDOW_WIDTH = 60
WINDOW_HEIGHT = 20
# Set Up
curses.initscr()
win = curses.newwin(WINDOW_HEIGHT, WINDOW_WIDTH, 0, 0) # y, x
win.keypad(1)
curses.noecho()
curses.curs_set(0)
win.border(0)
win.nodelay(1)
# Snake And Food
snake = [(4, 10), (4, 9), (4, 8)]
food = (10, 10)
win.addch(food[0], food[1], '⊠')
# Game Logic
score = 0
ESC = 27
key = curses.KEY_RIGHT
while key != ESC:
win.addstr(0, 2, 'score ' + str(score) + ' ')
win.timeout(150 - (len(snake)) // 5 + len(snake)//10 % 120)
prev_key = key
event = win.getch()
key = event if event != -1 else prev_key
if key not in [curses.KEY_LEFT, curses.KEY_RIGHT, curses.KEY_UP, curses.KEY_DOWN, ESC]:
prev_key
# Calculating Coordinates
y = snake[0][0]
x = snake[0][1]
if key == curses.KEY_DOWN:
y += 1
if key == curses.KEY_UP:
y -= 1
if key == curses.KEY_LEFT:
x -= 1
if key == curses.KEY_RIGHT:
x += 1
snake.insert(0, (y, x))
# Hit Border Reboot
if y == 0: break
if y == WINDOW_HEIGHT-1: break
if x == 0: break
if x == WINDOW_WIDTH-1: break
# dead.Self Snake
if snake[0] in snake[1:]: break
if snake[0] == food:
# Eat The Food
score += 1
food = ()
while food == ():
food = (randint(1,WINDOW_HEIGHT-2), randint(1,WINDOW_WIDTH-2))
if food in snake:
food = ()
win.addch(food[0], food[1], '⊠')
else:
# Move Snake
last = snake.pop()
win.addch(last[0], last[1], ' ')
win.addch(snake[0][0], snake[0][1], '▇')
# Working in Making the food Disappear inside snake
curses.endwin()
print(f"Final score = {score}")
I'm trying to figure out if it goes at the bottom of the code or somewhere else... I try a few things ex: While, If, and different variables...
P.s. ((I just wrote the code but is not mines I did'nt copy & paste neither...))
Please Help!!....

How do I prevent this Integer expectation error?

I'm making a simple little snake game as coding practice.
While trying to add the tail character to the window, I get this error. I know that I am not giving it integers, but I do not know what to do differently in order to.
Here is the error:
line 56, in <module>
w.addch(tail[0], tail[1], ' ')
TypeError: integer argument expected, got float
Here is the part generating error:
else:
tail = snake.pop()
w.addch(tail[0], tail[1], ' ')
Heres the full code:
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_RIGHT:
new_head[1] += 1
if key == curses.KEY_LEFT:
new_head[1] -= 1
snake.insert(0, new_head)
if snake[0] == food:
food = None
while food is None:
nf = [
random.randinit(1, sh-1),
random.randinit(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)
I cant think of a way to be more direct so this is me filling the text requirements.
reference for code?:
s = screen, snk_x/y = Snake x/y, sw/sh = screen width/height, w = window.
If I can be anymore clear let me know.
addch expects int. But division of integers in python 3 returns float.
You can:
convert the numbers to int w.addch(int(food[0]), int(food[1]), curses.ACS_PI)
or you can use // instead of / to do integer division
You can try just casting the values to integers using int() as follows:
w.addch(int(tail[0]), int(tail[1]), ' ')

type error :integer argument expected

I am making a snake game in Python by the help of google , but I found some errors which i dont understand why its so happening...my code is
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)
While running this code I got two errors, i.e.,
Traceback (most recent call last):
File "snakegame.py", line 20, in <module>
w.addch(food[0], food[1], curses.ACS_PI)
TypeError: integer argument expected, got float
In Python 3 / results in floating point results. To get integer results, use integer division //.
food = [sh // 2, sw // 2]
In addition, your snake is initialized with floating point numbers. Again, in order to make sure that snake contains only integers, do this:
snk_x = sw // 4
snk_y = sh // 2
After that, you will see a different crash due to new_head exceeding sw or sh (if you leave the snake move by itself without pressing any keys) but this is unrelated to the original issue.
Modified a bit, no error, but food not coming second time.
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(int(food[0]), int(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(int(food[0]), int(food[1]), curses.ACS_PI)
else:
tail = snake.pop()
w.addch(int(tail[0]), int(tail[1]), ' ')
w.addch(int(snake[0][0]), int(snake[0][1]), curses.ACS_CKBOARD)
enter image description here
This is a bit modified and works fine for me.
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)
This should work fine
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 = int(sw/4)
snk_y = int(sh/2)
snake = [
[snk_y, snk_x],
[snk_y, snk_x-1],
[snk_y, snk_x-2]
]
food = [sh/2, sw/2]
w.addch(int(food[0]), int(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(int(tail[0]), int(tail[1]), ' ')
w.addch(int(snake[0][0]), int(snake[0][1]), curses.ACS_CKBOARD)
Other solutions are over-using int() in more places than needed. You only need to use // integer division in the places I've shown.
Also a bit more fun. Diamond back snake.
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 # use integer division here
snk_y = sh//2 # use integer division here
snake = [
[snk_y, snk_x],
[snk_y, snk_x-1],
[snk_y, snk_x-2]
]
food = [sh//2, sw//2] # use integer division here
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
w.addch(snake[0][0], snake[0][1], curses.ACS_DIAMOND)
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)
use // not / because interpret see your parameter in flote values while you want to insert integer values
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]

'break' outside loop in snake game

I'm currently making a snake game in python (IDLE 3.6.0) and it keeps coming up with an error called 'break' outside loop. What does that mean? What am I doing wrong. This is the first time I've ever come accross this error.
Here's my code:
# 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.cur_set(0)
win.border(0)
win.nodelay(1)
key = KEY_RIGHT # Initalizing Values
score = 0
snake = [[4,10], [4,9], [4,8]] # Initial snake co-ordinates
food = [10,20] # First food co-ordinates
win.addc(food[0], food[1], '*') # Prints the food
while key != 27:
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)
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:
food = []
score += 1
while food == []:
food = [randint(1, 18), randint(1, 58)]
if food in snake: food = []
win.addch(food[0], food[1], '*')
else:
last = snake.pop()
win.addch(last[0], last[1], ' ')
win.addch(snake[0][0], snake[0][1], '#')
curses.erdwin()
print("\nScore - " + str(score))
print("http://bitemelater.in\n")
I'd be glad if you could help! Thanks!
The line that says if snake[0]in snake[1:]: break is where your error is stemming from. You probably want to use some kind of game ending function to display points and high scores and such:
def end_game():
#do your game ending things here
Then you would call that function here:
if snake[0]in snake[1:]: end_game()
EDIT:
I found this in relation to your curses error: Error no module named curses. It seems as if curses doesn't support Windows machines. Try installing UniCurses or this binary.

Categories

Resources