Best Python code snippet using hypothesis
snake_game.py
Source:snake_game.py
...30 """31 text = score_font.render("Score: " + str(score), True, orange)32 game_display.blit(text, [0, 0]) # text to display and position33# MIN 9:3534def draw_snake(snake_size, snake_pixels):35 """36 draws the snake 37 snake_pixels -> list with the position of the pixels 38 """39 for pixel in snake_pixels : # TRY TO TAKE THIS LOOP OUT40 # draws a rectangle (pixel)41 # pixel[0] and pixel[1] -> position x and y of the pixel42 # pixel[0], pixel[1] -> snake starts with one pixel sized snake_size43 pygame.draw.rect(game_display, white, [44 pixel[0], pixel[1], snake_size, snake_size])45def run_game() :46 game_over = False47 game_close = False # closing the game48 # starting position49 x = width / 250 y = height / 251 # snake starts without speed52 x_speed = 053 y_speed = 054 # holds the length that the snake gets55 snake_pixels = []56 snake_length = 157 # target position58 target_x = round(random.randrange(0, width - snake_size) / 10.0) * 10.059 target_y = round(random.randrange(0, height - snake_size) / 10.0) * 10.060 # game moves61 while not game_over :62 # handling loss63 while game_close :64 game_display.fill(black)65 game_over_message = message_font.render("Game Over!", True, red)66 game_display.blit(game_over_message, [width / 2, height / 2])67 print(snake_length - 1)68 for event in pygame.event.get() :69 if event.type == pygame.KEYDOWN :70 #get off of the loops -> get off of the game71 if event.key == pygame.K_1 :72 game_over = True73 game_close = False74 #restarts game75 if event.key == pygame.K_2 :76 run_game()77 # QUIT = opposite of the pygame.init() -> runs code that deactivates the Pygame library78 if event.type == pygame.QUIT :79 game_over = True80 game_close = False81 82 for event in pygame.event.get():83 if event.type == pygame.QUIT:84 game_over == True85 # checks if a key is pressed86 if event.type == pygame.KEYDOWN:87 # snake will move in a speed as big as it size88 if event.key == pygame.K_LEFT:89 x_speed = - snake_size # x coordinate is decresed to left90 y_speed = 0 # TRY TO REMOVE THIS LATER91 if event.key == pygame.K_RIGHT:92 x_speed = snake_size93 y_speed = 094 if event.key == pygame.K_UP:95 x_speed = 096 y_speed = - snake_size # y coordinate is decresed downwards97 if event.key == pygame.K_DOWN:98 x_speed = 099 y_speed = snake_speed100 # checks if the snake reached the screen limits101 if x >= width or x < 0 or y >= height or y < 0:102 game_close = True103 # continous movement of the snake104 x += x_speed105 y += y_speed106 # drawing in the pygame window107 game_display.fill(black) # background108 pygame.draw.rect(game_display, orange, [target_x, target_y, snake_size, snake_size]) # target109 # add snake head to the list so the snake is moving110 snake_pixels.append([x, y])111 if len(snake_pixels) > snake_length :112 # removes snake tail from the list so the snake doesnt grow automatically113 del snake_pixels[0]114 # iterates till before the last block of pixels -> the head115 for pixel in snake_pixels[:-1] :116 # checks if one of the snake pixels is in the head position -> snake hits itself117 if pixel == [x, y]:118 game_close = True119 draw_snake(snake_size, snake_pixels)120 # print score121 print(snake_length - 1)122 pygame.display.update()123 # checks if snake finds a target124 if x == target_x and y == target_y:125 # position a new target126 target_x = round(random.randrange(127 0, width - snake_size) / 10.0) * 10.0128 target_y = round(random.randrange(129 0, height - snake_size) / 10.0) * 10.0130 snake_length += 1131 clock.tick(snake_speed)132 133 pygame.quit()...
snake-game.py
Source:snake-game.py
1#importing libraries2import turtle3import random4import time567#creating turtle screen8screen = turtle.Screen()9screen.title('DATAFLAIR-SNAKE GAME')10screen.setup(width = 700, height = 700)11screen.tracer(0)12turtle.bgcolor('turquoise')13141516##creating a border for our game1718turtle.speed(5)19turtle.pensize(4)20turtle.penup()21turtle.goto(-310,250)22turtle.pendown()23turtle.color('black')24turtle.forward(600)25turtle.right(90)26turtle.forward(500)27turtle.right(90)28turtle.forward(600)29turtle.right(90)30turtle.forward(500)31turtle.penup()32turtle.hideturtle()3334#score35score = 036delay = 0.1373839#snake40snake = turtle.Turtle()41snake.speed(0)42snake.shape('square')43snake.color("black")44snake.penup()45snake.goto(0,0)46snake.direction = 'stop'474849#food50fruit = turtle.Turtle()51fruit.speed(0)52fruit.shape('circle')53fruit.color('red')54fruit.penup()55fruit.goto(30,30)5657old_fruit=[]5859#scoring60scoring = turtle.Turtle()61scoring.speed(0)62scoring.color("black")63scoring.penup()64scoring.hideturtle()65scoring.goto(0,300)66scoring.write("Score :",align="center",font=("Courier",24,"bold"))676869#######define how to move70def snake_go_up():71 if snake.direction != "down":72 snake.direction = "up"7374def snake_go_down():75 if snake.direction != "up":76 snake.direction = "down"7778def snake_go_left():79 if snake.direction != "right":80 snake.direction = "left"8182def snake_go_right():83 if snake.direction != "left":84 snake.direction = "right"8586def snake_move():87 if snake.direction == "up":88 y = snake.ycor()89 snake.sety(y + 20)9091 if snake.direction == "down":92 y = snake.ycor()93 snake.sety(y - 20)9495 if snake.direction == "left":96 x = snake.xcor()97 snake.setx(x - 20)9899 if snake.direction == "right":100 x = snake.xcor()101 snake.setx(x + 20)102103# Keyboard bindings104screen.listen()105screen.onkeypress(snake_go_up, "Up")106screen.onkeypress(snake_go_down, "Down")107screen.onkeypress(snake_go_left, "Left")108screen.onkeypress(snake_go_right, "Right")109110#main loop111112while True:113 screen.update()114 #snake and fruit coliisions115 if snake.distance(fruit)< 20:116 x = random.randint(-290,270)117 y = random.randint(-240,240)118 fruit.goto(x,y)119 scoring.clear()120 score+=1121 scoring.write("Score:{}".format(score),align="center",font=("Courier",24,"bold"))122 delay-=0.001123 124 ## creating new_ball125 new_fruit = turtle.Turtle()126 new_fruit.speed(0)127 new_fruit.shape('square')128 new_fruit.color('red')129 new_fruit.penup()130 old_fruit.append(new_fruit)131 132133 #adding ball to snake134 135 for index in range(len(old_fruit)-1,0,-1):136 a = old_fruit[index-1].xcor()137 b = old_fruit[index-1].ycor()138139 old_fruit[index].goto(a,b)140 141 if len(old_fruit)>0:142 a= snake.xcor()143 b = snake.ycor()144 old_fruit[0].goto(a,b)145 snake_move()146147 ##snake and border collision 148 if snake.xcor()>280 or snake.xcor()< -300 or snake.ycor()>240 or snake.ycor()<-240:149 time.sleep(1)150 screen.clear()151 screen.bgcolor('turquoise')152 scoring.goto(0,0)153 scoring.write(" GAME OVER \n Your Score is {}".format(score),align="center",font=("Courier",30,"bold"))154155156 ## snake collision157 for food in old_fruit:158 if food.distance(snake) < 20:159 time.sleep(1)160 screen.clear()161 screen.bgcolor('turquoise')162 scoring.goto(0,0)163 scoring.write(" GAME OVER \n Your Score is {}".format(score),align="center",font=("Courier",30,"bold"))164165166 167 time.sleep(delay)168169turtle.Terminator()170171172173174
...
snake.py
Source:snake.py
2class cube(object):3 def __init__(self, x_dir, y_dir):4 self.x = x_dir5 self.y = y_dir6class snake(object):7 def __init__(self, head_x, head_y):8 self.head = cube(head_x, head_y)9 self.body = [self.head]10def move_snake(snake_obj):11 head_last_x = snake_obj.head.x12 head_last_y = snake_obj.head.y13 prev_last_x = snake_obj.body[-1].x14 prev_last_y = snake_obj.body[-1].y15 key_pressed = input()16 if key_pressed == 'w':17 snake_obj.head.y -= 118 if len(snake_obj.body) > 1:19 switch_last(head_last_x, head_last_y, snake_obj)20 elif key_pressed == 's':21 snake_obj.head.y += 122 if len(snake_obj.body) > 1:23 switch_last(head_last_x, head_last_y, snake_obj)24 elif key_pressed == 'a':25 snake_obj.head.x -= 126 if len(snake_obj.body) > 1:27 switch_last(head_last_x, head_last_y, snake_obj)28 elif key_pressed == 'd':29 snake_obj.head.x += 130 if len(snake_obj.body) > 1:31 switch_last(head_last_x, head_last_y, snake_obj)32 elif key_pressed == 'q':33 global is_running34 is_running = False35 return prev_last_x, prev_last_y36def switch_last(head_last_x, head_last_y, snake_obj):37 last = snake_obj.body[-1]38 last.x = head_last_x39 last.y = head_last_y40def erase_snake(snake_obj, board):41 for element in snake_obj.body:42 board[element.y][snake_obj.head.x] = 043def draw_snake_on_board(board, snake_obj):44 for cube in snake_obj.body:45 board[cube.y][cube.x] = 146def draw_apple_on_board(board):47 y = random.randint(0, len(board)-1)48 x = random.randint(0, len(board[0])-1)49 board[y][x] = 250 return y, x51def print_board(board):52 for row in board:53 print(row)54def main(board):55 global is_running56 57 width = len(board[0])58 height = len(board)59 snake_obj = snake(width//2, height//2)60 draw_snake_on_board(board, snake_obj)61 y_apple, x_apple = draw_apple_on_board(board)62 print_board(board)63 64 while is_running:65 erase_snake(snake_obj, board)66 prev_last_x, prev_last_y = move_snake(snake_obj)67 if snake_obj.head.x == x_apple and snake_obj.head.y == y_apple:68 snake_obj.body.append(cube(prev_last_x, prev_last_y))69 y_apple, x_apple = draw_apple_on_board(board)70 draw_snake_on_board(board, snake_obj)71 print_board(board)72 73 return 'Game over'74board = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],75 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],76 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],77 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],78 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],79 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],80 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!