Posts

Python Game - Snake Game

Image
Snake Game Code: main.py from turtle import Screen from snake import Snake from food import Food from scoreboard import Scoreboard import time screen = Screen() screen.setup( width = 600 , height = 600 ) screen.bgcolor( "black" ) screen.title( "My Snake Game" ) screen.tracer( 0 ) snake = Snake() food = Food() scoreboard = Scoreboard() screen.listen() screen.onkey(snake.up, "Up" ) screen.onkey(snake.down, "Down" ) screen.onkey(snake.left, "Left" ) screen.onkey(snake.right, "Right" ) game_is_on = True while game_is_on: time.sleep( 0.1 ) screen.update() snake.move() # detect collision with food if snake.head.distance(food) < 15 : scoreboard.increase_score() snake.extend() food.refresh() # detect collision with wall if snake.head.xcor() > 280 or snake.head.xcor() < - 300 or snake.head.ycor() > 280 or snake.head.ycor() < - 280 : game_is_on = False sc

Python Game - Turtle Race

 Python Game - Turtle Race Instructions: 1. You need to install the turtle module and random module into your python folder, you can install them by using the following code in the terminal. >>>pip install turtle >>>pip install random  Code: from turtle import Turtle, Screen import random is_race_on = False screen = Screen() screen.setup( width = 500 , height = 400 ) user_bet = screen.textinput( title = "Make your bet" , prompt = "Which turtle will win the race? Enter the color" ) colors = [ "red" , "orange" , "green" , "yellow" , "blue" , "purple" ] y_positions = [- 70 , - 40 , - 10 , 20 , 50 , 80 ] all_turtles = [] for turtle_index in range ( 0 , 6 ): new_turtle = Turtle( shape = 'turtle' ) new_turtle.penup() new_turtle.color(colors[turtle_index]) new_turtle.goto( x =- 230 , y =y_positions[turtle_index]) all_turtles.append(new_turtle) if user_bet: is_race_

Draw any Image using Python Turtle Module

Image
 Instructions: 1. The below code will draw an Iron man image as the iron man image is passed to the program. 2. You have to install the library pywhatkit into your python directory by using the command  >>>pip install pywhatkit   Resources: Code: from pywhatkit import image_to_ascii_art as a  img_path = 'ironman.jpg' text = 'ironman.txt'          a(img_path, text) import turtle as t  s_x = -320                   s_y = 250 p = t.Pen() t.bgcolor('black') p.up() p.width(2) f_m = 0 d_m = 4 # function to select the color def set_col(c):     chars = {"*": 'white', "S" : 'green', "#" : 'green', "&" : 'white', "@":'black', "$" : 'white', "%" : 'white', "!":'blue', ":" :'darkgreen', ".":'grey'}     col = chars[c]     p.pencolor(col) def d(m, s_char):      p.up()      if s_char != &

Convert Image To Sketch Using Python

Image
Before you move forward don't forget to follow us on Instagram: @python.quest Instructions: 1. Before you run this code make sure you have installed the following libraries:          a) imageio          b) scipy          c) openCV 2. Make sure you have the following Elon musk image with the name elon.jpg in your program folder. Download this image from here 3. You are free to copy-paste this code now: import numpy as np import imageio import scipy.ndimage import cv2 img = "elon.jpg" def rgb2gray (rgb): return np.dot(rgb[..., : 3 ], [ 0.2989 , 0.5870 , .1140 ]) def dodge (front,back): final_sketch = front* 255 /( 255 -back) final_sketch[final_sketch > 255 ] = 255 final_sketch[back == 255 ] = 255 return final_sketch.astype( 'uint8' ) ss = imageio.imread(img) gray = rgb2gray(ss) i = 255 -gray blur = scipy.ndimage.filters.gaussian_filter(i, sigma = 13 ) r = dodge(blur, gray) cv2.imwrite( 'elon.png' ,r) Output: