Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flake8 alerts resolved #26

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
*.pyc
*.pyc
.idea/
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/python-console-snake.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 14 additions & 12 deletions menu.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@

import subprocess
import os

running = True
state = 0


def printHeader():
def print_header():
os.system('clear')
print 'S N A K E\n'


def tutorial():
global state

if state == 0:
printHeader()
print_header()
print '\nHi there! Welcome to Snake.'
print 'Type snake to launch the game'
success = False
Expand All @@ -36,7 +36,7 @@ def tutorial():
print 'Type snake to launch the game'

elif state == 1:
printHeader()
print_header()
print 'Great game. Now let\'s see what options are available.'
print 'Type snake --help'
success = False
Expand All @@ -57,19 +57,20 @@ def tutorial():
success = False
while not success:
command = raw_input()
cmdArray = command.split(' ')
if len(cmdArray) == 3 and \
cmdArray[0] == 'snake --theme' and \
cmdArray[1] == '--theme' or cmdArray[1] == '--t' and \
cmdArray[2] == 'classic' or cmdArray[2] == 'minimal':
command = "python . --theme " + cmdArray[2]
cmd_array = command.split(' ')
if len(cmd_array) == 3 and \
cmd_array[0] == 'snake --theme' and \
cmd_array[1] == '--theme' or cmd_array[1] == '--t' and \
cmd_array[2] == 'classic' or cmd_array[2] == 'minimal':
command = "python . --theme " + cmd_array[2]
subprocess.call(command, shell=True)
success = True
state += 1
elif command == 'snake --help' or command == 'snake -h':
success = False
else:
print 'Try typying snake --theme minimal'
print 'Try typing snake --theme minimal'


def run():
try:
Expand All @@ -78,4 +79,5 @@ def run():
except KeyboardInterrupt:
exit()

run()

run()
2 changes: 1 addition & 1 deletion snake/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import graphics
import theme
import gameloop
Expand All @@ -23,4 +22,5 @@ def run():
except KeyboardInterrupt:
exit()


run()
19 changes: 10 additions & 9 deletions snake/console.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@

import fcntl
import termios
import struct
import os


def getTerminalSize():
def get_terminal_size():
env = os.environ

def ioctl_GWINSZ(fd):
def ioctl_gwinsz(fd):
try:
cr = struct.unpack(
'hh',
fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')
)
except:
return
)
except Exception as e:
return e
return cr

cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
cr = ioctl_gwinsz(0) or ioctl_gwinsz(1) or ioctl_gwinsz(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
cr = ioctl_gwinsz(fd)
os.close(fd)
except:
except Exception as e:
error = e.message
print(error)
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
Expand Down
1 change: 0 additions & 1 deletion snake/controls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import __main__
import graphics
import game
Expand Down
39 changes: 19 additions & 20 deletions snake/game.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import stage
import gameloop
import math
Expand All @@ -24,34 +23,34 @@ def init():


def update():
moveSnake()
checkCatch()
checkPositionAllowed()
move_snake()
check_catch()
check_position_allowed()


def checkCatch():
def check_catch():
if not len(snake) or not len(apples):
return

for i, apple in enumerate(apples):
if (snake[0][0]) == apple[0] and (snake[0][1]) == apple[1]:
eatApple(i)
eat_apple(i)


def eatApple(i):
def eat_apple(i):
global grow, score

apples.pop(i)
spawnApple()
spawn_apple()
grow += config.food_values['apple']
score += 1


def moveSnake():
def move_snake():
global grow, lastPos

last_unchanged = None
lastPos = (snake[len(snake)-1][0], snake[len(snake)-1][1])
lastPos = (snake[len(snake) - 1][0], snake[len(snake) - 1][1])
for i, part in enumerate(snake):
if i == 0:
x = part[0] + speed * direction[0]
Expand All @@ -68,7 +67,7 @@ def moveSnake():
grow -= 1


def getGameArea():
def get_game_area():
w = math.fabs(stage.boundaries['right'] - stage.boundaries['left'])
h = math.fabs(stage.boundaries['top'] - stage.boundaries['bottom'])

Expand All @@ -85,14 +84,14 @@ def reset():
apples = []
grow = config.initial_size - 1

apples_count += int(math.floor(getGameArea() / config.apple_domain))
apples_count += int(math.floor(get_game_area() / config.apple_domain))

for i in range(0, apples_count):
spawnApple()
spawn_apple()


def spawnApple():
if len(apples) >= getGameArea():
def spawn_apple():
if len(apples) >= get_game_area():
return

x = random.randrange(stage.boundaries['left'], stage.boundaries['right'])
Expand All @@ -108,13 +107,13 @@ def spawnApple():
if part[0] == x and part[1] == y:
position_free = False

if position_free and not isOutOfBoundaries(x, y):
if position_free and not is_out_of_boundaries(x, y):
apples.append((x, y))
else:
spawnApple()
spawn_apple()


def isOutOfBoundaries(x, y):
def is_out_of_boundaries(x, y):
if x < stage.boundaries['left'] or x > stage.boundaries['right'] - 1:
return True

Expand All @@ -124,7 +123,7 @@ def isOutOfBoundaries(x, y):
return False


def checkPositionAllowed():
def check_position_allowed():
global lives

collides_with_body = False
Expand All @@ -136,7 +135,7 @@ def checkPositionAllowed():
collides_with_body = True
break

if (collides_with_body or isOutOfBoundaries(x, y)):
if collides_with_body or is_out_of_boundaries(x, y):
gameloop.reset()
lives -= 1
if lives == 0:
Expand Down
7 changes: 3 additions & 4 deletions snake/gameloop.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import time
import graphics
import game
Expand Down Expand Up @@ -48,7 +47,7 @@ def start():
if state == 0:
step()
elif state == 1:
graphics.drawGameOver()
graphics.draw_game_over()


def stop():
Expand All @@ -61,10 +60,10 @@ def init():
global state

game.init()
graphics.drawGame()
graphics.draw_game()
state = 0


def reset():
game.reset()
graphics.drawGame()
graphics.draw_game()
Loading