Skip to content

Commit

Permalink
Python 2.7 to 3.6 converter
Browse files Browse the repository at this point in the history
  • Loading branch information
rakeshlinux committed May 20, 2018
1 parent 436211f commit a929e2f
Show file tree
Hide file tree
Showing 15 changed files with 805 additions and 56 deletions.
19 changes: 19 additions & 0 deletions Loops/Char_parallelogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Program to print character paralleogram
# made by : rakesh kumar
# Licence : MIT
def printString(string):
n = len(string)
for i in range(0,n):
print(string[i],end=" ")

string = "TESTINGPROGRAM"
n = len(string)
# print(n)
for i in range(1,n+1):
for j in range(i,n):
print(" ",end="")
printString(string[0:i])
print()
for i in range(1,n+1):
printString(string[i:])
print()
15 changes: 15 additions & 0 deletions Projects/snakegame/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#================================================================#
# Name : colors.py #
# Description : Common color definitions #
# Author : Adrian Antonana #
# Date : 29.07.2012 #
#================================================================#

RED = (255,0,0)
RED_DARK = (150,0,0)
GREEN = (0,255,0)
GREEN_DARK = (0,150,0)
BLUE = (0,0,255)
BLUE_DARK = (0,0,150)
WHITE = (255,255,255)
BLACK = (0,0,0)
47 changes: 47 additions & 0 deletions Projects/snakegame/food.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#=============================================================================#
# Name : food.py #
# Description : Food class definition for the snake game #
# Author : Adrian Antonana #
# Date : 29.07.2012 #
#=============================================================================#

# imports
import pygame as pg
import random as rnd
from colors import *

# block sizes
BLOCK_SIZE = 30
BLOCK_SIZE_INNER = 20

# food class definition
class food:

# class constructor
def __init__(self,surface,minx,maxx,miny,maxy):
self.surface = surface
self.posx = rnd.randint(minx,maxx-1)
self.posy = rnd.randint(miny,maxy-1)

# for drawing the food
self.foodblock = pg.Surface((BLOCK_SIZE,BLOCK_SIZE))
self.foodblock.set_alpha(255)
self.foodblock.fill(RED)
self.foodblockdark = pg.Surface((BLOCK_SIZE_INNER,BLOCK_SIZE_INNER))
self.foodblockdark.set_alpha(255)
self.foodblockdark.fill(RED_DARK)

# get food position
def getPos(self):
return (self.posx,self.posy)

# draw the food
def draw(self):
fb = self.foodblock
fbd = self.foodblockdark
sf = self.surface

# food is just two blocks
foodpos = self.getPos()
sf.blit(fb,(foodpos[1]*BLOCK_SIZE,foodpos[0]*BLOCK_SIZE))
sf.blit(fbd,(foodpos[1]*BLOCK_SIZE+5,foodpos[0]*BLOCK_SIZE+5))
115 changes: 115 additions & 0 deletions Projects/snakegame/snake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#==============================================================================================#
# Name : snake.py #
# Description : The snake class definition for the snake game. #
# Author : Adrian Antonana #
# Date : 29.07.2012 #
#==============================================================================================#

# imports
import pygame as pg
from colors import *

# motion direction constants
UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3

# block sizes and colors
BLOCK_SIZE = 30
BLOCK_SIZE_INNER = 20

# snake class definition
class snake:

# constructor
def __init__(self,surface,headposx=10,headposy=10):
self.surface = surface
self.length = 10
self.poslist = [(headposx,y) for y in reversed(range(headposy-self.length+1,headposy+1))]
self.motdir = RIGHT
self.crashed = False

# for drawing the snake
self.snakeblock = pg.Surface((BLOCK_SIZE,BLOCK_SIZE))
self.snakeblock.set_alpha(255)
self.snakeblock.fill(GREEN)
self.snakeblockdark = pg.Surface((BLOCK_SIZE_INNER,BLOCK_SIZE_INNER))
self.snakeblockdark.set_alpha(255)
self.snakeblockdark.fill(GREEN_DARK)

# for removing the snake
self.backblock = pg.Surface((BLOCK_SIZE,BLOCK_SIZE))
self.backblock.set_alpha(255)
self.backblock.fill(BLACK)

# get snake's head position
def getHeadPos(self):
return (self.poslist[0])

# get the motion direction
def getMotionDir(self):
return self.motdir

# get the snake positions list
def getPosList(self):
return self.poslist

# set the motion direction
def setMotionDir(self,motdir):
self.motdir = motdir

# increase the snake length by one
def incLength(self):
self.length += 1

# move the snake updates the positions list and checks if the snake has crashed
def move(self):
motdir = self.getMotionDir()
headpos = self.getHeadPos()

# update positions
if motdir == UP:
poslist = [(headpos[0]-1,headpos[1])]
elif motdir == DOWN:
poslist = [(headpos[0]+1,headpos[1])]
elif motdir == LEFT:
poslist = [(headpos[0],headpos[1]-1)]
elif motdir == RIGHT:
poslist = [(headpos[0],headpos[1]+1)]

poslist.extend(self.poslist[:-1])
self.poslist = poslist

# check if crashed
if self.getHeadPos() in self.getPosList()[1:]:
self.crashed = True

# check if the snake has crashed
def chrashed(self):
return self.crashed

# grow the snake. add a new position at the end
def grow(self):
lastpos = self.getPosList()[-1]
self.length += 1
self.poslist.append((lastpos[0]-1,lastpos[1]))

# draw the snake
def draw(self):
skb = self.snakeblock
skbd = self.snakeblockdark
sf = self.surface

for blockpos in self.getPosList():
sf.blit(skb,(blockpos[1]*BLOCK_SIZE,blockpos[0]*BLOCK_SIZE))
sf.blit(skbd,(blockpos[1]*BLOCK_SIZE+5,blockpos[0]*BLOCK_SIZE+5))

# delete the snake
def remove(self):
bkb = self.backblock
sf = self.surface

# draw block for every snake position
for blockpos in self.getPosList():
sf.blit(bkb,(blockpos[1]*BLOCK_SIZE,blockpos[0]*BLOCK_SIZE))
Binary file added Projects/snakegame/snakecrash.wav
Binary file not shown.
Binary file added Projects/snakegame/snakeeat.wav
Binary file not shown.
173 changes: 173 additions & 0 deletions Projects/snakegame/snakegame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#===============================================================================================#
# Name : snakegame.py #
# Description : Python version of the snake game. #
# Author : Adrian Antonana #
# Date : 29.07.2012 #
#===============================================================================================#

# imports
import pygame as pg
import snake as snk
import food as fd
import sys
from . import *

# screen size and game speed
WIDTH = 25
HEIGHT = 25
SPEED = 8
SPEED_TICK = 2
SPEED_INC = 5
SHORT = 12
LONG = 1

# defining the outer wall blocks
wallblock = pg.Surface((snk.BLOCK_SIZE,snk.BLOCK_SIZE))
wallblock.set_alpha(255)
wallblock.fill(BLUE)
wallblockdark = pg.Surface((snk.BLOCK_SIZE_INNER,snk.BLOCK_SIZE_INNER))
wallblockdark.set_alpha(255)
wallblockdark.fill(BLUE_DARK)

#================================================================================================#
# Function Definitions #
#================================================================================================#

# check if the snake's head is outside the limits
def inLimits(snake):
headpos = snake.getHeadPos()
return not (headpos[0] < 1 or headpos[1] < 1 or headpos[0] >= HEIGHT+1 or headpos[1] >= WIDTH+1)

# draw walls
def drawWalls(surface):

# left and right walls
for y in range(HEIGHT+1):
surface.blit(wallblock,(0,y*snk.BLOCK_SIZE))
surface.blit(wallblockdark,(5,y*snk.BLOCK_SIZE+5))
surface.blit(wallblock,((WIDTH+1)*snk.BLOCK_SIZE,y*snk.BLOCK_SIZE))
surface.blit(wallblockdark,((WIDTH+1)*snk.BLOCK_SIZE+5,y*snk.BLOCK_SIZE+5))

# upper and bottom walls
for x in range(WIDTH+2):
surface.blit(wallblock,(x*snk.BLOCK_SIZE,0))
surface.blit(wallblockdark,(x*snk.BLOCK_SIZE+5,5))
surface.blit(wallblock,(x*snk.BLOCK_SIZE,(HEIGHT+1)*snk.BLOCK_SIZE,))
surface.blit(wallblockdark,(x*snk.BLOCK_SIZE+5,(HEIGHT+1)*snk.BLOCK_SIZE+5))

#================================================================================================#
# Main Game Part #
#================================================================================================#

# initialize pygame, clock for game speed and screen to draw
pg.init()

# initializing mixer, sounds, clock and screen
pg.mixer.init()
eatsound = pg.mixer.Sound("snakeeat.wav")
crashsound = pg.mixer.Sound("snakecrash.wav")
clock = pg.time.Clock()
screen = pg.display.set_mode(((WIDTH+2)*snk.BLOCK_SIZE,(HEIGHT+2)*snk.BLOCK_SIZE))
pg.display.set_caption("snake")
font = pg.font.SysFont(pg.font.get_default_font(),40)
gameovertext = font.render("GAME OVER",1,WHITE)
starttext = font.render("PRESS ANY KEY TO START",1,WHITE)
screen.fill(BLACK)

# we need a snake and something to eat
snake = snk.snake(screen,WIDTH/2,HEIGHT/2)
food = fd.food(screen,1,HEIGHT+1,1,WIDTH+1)

# food should not appear where the snake is
while food.getPos() in snake.getPosList():
food.__init__(screen,1,HEIGHT+1,1,WIDTH+1)

# only queue quit and and keydown events
# pg.event.set_allowed([pg.QUIT,pg.KEYDOWN])
pg.event.set_blocked([pg.MOUSEMOTION,pg.MOUSEBUTTONUP,pg.MOUSEBUTTONDOWN])

# will increase game speed every 10 times we eat
eaten = 0

# press any key to start!!!
drawWalls(screen)
screen.blit(starttext,((WIDTH-10)*snk.BLOCK_SIZE/2,HEIGHT*snk.BLOCK_SIZE/2))
pg.display.flip()
waiting = True
while waiting:
event = pg.event.wait()
if event.type == pg.KEYDOWN:
waiting = False
screen.fill(BLACK)

# main loop
running = True
while running:

# check crash or move outside the limits
if not inLimits(snake) or snake.crashed:
running = False
crashsound.play()
else:

# draw screen with snake and foods
food.draw()
snake.draw()
drawWalls(screen)
pg.display.flip()

# check if snake eates
if food.getPos() == snake.getHeadPos():
eatsound.play()
snake.grow()
# food should not appear where the snake is
food.__init__(screen,1,HEIGHT+1,1,WIDTH+1)
while food.getPos() in snake.getPosList():
food.__init__(screen,1,HEIGHT+1,1,WIDTH+1)
eaten += 1
# increase game speed
if eaten % SPEED_INC == 0:
SPEED += SPEED_TICK

# game speed control
clock.tick(SPEED)

# get the next event on queue
event = pg.event.poll()
if event.type == pg.QUIT:
sys.exit()
elif event.type == pg.KEYDOWN:
actmotdir = snake.getMotionDir()
if event.key == pg.K_ESCAPE:
sys.exit()
elif event.key == pg.K_UP and actmotdir != snk.DOWN:
snake.setMotionDir(snk.UP)
elif event.key == pg.K_DOWN and actmotdir != snk.UP:
snake.setMotionDir(snk.DOWN)
elif event.key == pg.K_RIGHT and actmotdir != snk.LEFT:
snake.setMotionDir(snk.RIGHT)
elif event.key == pg.K_LEFT and actmotdir != snk.RIGHT:
snake.setMotionDir(snk.LEFT)

# remove the snake and make movement
snake.remove()
snake.move()

# if crashed print "game over" and wait for esc key
clock.tick(LONG)
snake.draw()
drawWalls(screen)
snakeposlist = snake.getPosList()
blackblock = snake.backblock
for pos in snakeposlist[1:]:
screen.blit(blackblock,(pos[1]*snk.BLOCK_SIZE,pos[0]*snk.BLOCK_SIZE))
pg.display.flip()
clock.tick(SHORT)

while True:
screen.blit(gameovertext,((WIDTH-4)*snk.BLOCK_SIZE/2,HEIGHT*snk.BLOCK_SIZE/2))
pg.display.flip()
event = pg.event.wait()
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
sys.exit()
Loading

0 comments on commit a929e2f

Please sign in to comment.