Best Python code snippet using green
Learning_pygame.py
Source:Learning_pygame.py  
1import pygame2import os3pygame.font.init()4# create variables for the dimensions and then inser them into pygame.display5WIDTH, HEIGHT = 900, 5006WIN = pygame.display.set_mode((WIDTH, HEIGHT))7pygame.display.set_caption("First Game!")8# variables for reused numbers, always helpful to store numbers u are going to use many times9WHITE = (255, 255, 255)10BLACK = (0, 0, 0)11RED = (255, 0, 0)12YELLOW = (255, 255, 0)13BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)14HEALTH_FONT = pygame.font.SysFont('comic sans ms', 40)15WINNER_FONT = pygame.font.SysFont('comic sans ms', 100)16FPS = 6017VEL = 418BULLET_VEL = 819MAX_BULLETS = 1020SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 4021YELLOW_HIT = pygame.USEREVENT + 122RED_HIT = pygame.USEREVENT + 223# calling the assets folder and taking the images from there using the OS import24YELLOW_SPACESHIP_IMAGE = pygame.image.load(os.path.join('Assets', 'spaceship_yellow.png'))25YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)26RED_SPACESHIP_IMAGE = pygame.image.load(os.path.join('Assets', 'spaceship_red.png'))27RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)28SPACE = pygame.transform.scale(pygame.image.load(os.path.join('Assets', 'space.png')), (WIDTH, HEIGHT))29# all drawing will go under this define. .update is what allows the color to change30def draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health):31    WIN.blit(SPACE, (0, 0)) # if SPACE (the background) was after the blit method then you would not see the ship because the order of drawing things matters32    pygame.draw.rect(WIN, BLACK, BORDER)33    red_health_text = HEALTH_FONT.render("Health: " + str(red_health), 1, WHITE)34    yellow_health_text = HEALTH_FONT.render("Health: " + str(yellow_health), 1, WHITE)35    WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))36    WIN.blit(yellow_health_text, (10, 10))37    WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))38    WIN.blit(RED_SPACESHIP, (red.x, red.y))39    for bullet in red_bullets:40        pygame.draw.rect(WIN, RED, bullet)41    for bullet in yellow_bullets:42        pygame.draw.rect(WIN, YELLOW, bullet)43    pygame.display.update()  # any changes above will updated from display.update44# Controls for the yellow ship45def yellow_handle_movement(keys_pressed, yellow):46    if keys_pressed[pygame.K_a] and yellow.x - VEL > 0: # Left47        yellow.x -= VEL48    if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x: # right49        yellow.x += VEL50    if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: # up51        yellow.y -= VEL52    if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.height < HEIGHT - 13: # down53        yellow.y += VEL54# Controls for the red ship55def red_handle_movement(keys_pressed, red):56    if keys_pressed[pygame.K_LEFT] and red.x - VEL > BORDER.x + BORDER.width: # left57        red.x -= VEL58    if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH: # right59        red.x += VEL60    if keys_pressed[pygame.K_UP] and red.y - VEL > 0: # Up61        red.y -= VEL62    if keys_pressed[pygame.K_DOWN] and red.y + VEL + red.height < HEIGHT - 13: # down63        red.y += VEL64        65# bullet speed, and removing the bullets when they hit the other ship and the back wall66def handle_bullets(yellow_bullets, red_bullets, yellow, red):67    for bullet in yellow_bullets:68        bullet.x += BULLET_VEL69        if red.colliderect(bullet):70            pygame.event.post(pygame.event.Event(RED_HIT))71            yellow_bullets.remove(bullet)72        elif bullet.x > WIDTH:73            yellow_bullets.remove(bullet)74    for bullet in red_bullets:75        bullet.x -= BULLET_VEL76        if yellow.colliderect(bullet):77            pygame.event.post(pygame.event.Event(YELLOW_HIT))78            red_bullets.remove(bullet)79        elif bullet.x < 0:80            red_bullets.remove(bullet)81def draw_winner(text):82    draw_text = WINNER_FONT.render(text, 1, WHITE)83    WIN.blit(draw_text, (WIDTH/2 - draw_text.get_width() / 2, HEIGHT/2 - draw_text.get_height()/2))84    pygame.display.update()85    pygame.time.delay(5000)86# this loop allows you to end the game87def main():88    red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)89    yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)90    red_bullets = []91    yellow_bullets = []92    red_health = 1093    yellow_health = 1094    clock = pygame.time.Clock()95    run = True96    while run:97        clock.tick(FPS)98        for event in pygame.event.get():99            if event.type == pygame.QUIT:100                run = False101                pygame.quit()102            103            if event.type == pygame.KEYDOWN:104                if event.key == pygame.K_LCTRL and len(yellow_bullets) < MAX_BULLETS:105                    bullet = pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height//2 - 2, 10, 5)106                    yellow_bullets.append(bullet)107                if event.key == pygame.K_RCTRL and len(red_bullets) < MAX_BULLETS:108                    bullet = pygame.Rect(red.x, red.y + red.height//2 - 2, 10, 5)109                    red_bullets.append(bullet)110            if event.type == RED_HIT:111                red_health -= 1112            if event.type == YELLOW_HIT:113                yellow_health -= 1114        winner_text = ""115        if red_health <= 0:116            winner_text = "Yellow Wins!"117        if yellow_health <= 0:118            winner_text = "Red Wins!"119        if winner_text != "":120            draw_winner(winner_text)121            break122        # these are the declared functons we call above for their each individual uses123        keys_pressed = pygame.key.get_pressed()124        yellow_handle_movement(keys_pressed, yellow)125        red_handle_movement(keys_pressed, red)126        handle_bullets(yellow_bullets, red_bullets, yellow, red)127        draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health)128    main()129# dont quite understand this130if __name__ == "__main__":...main.py
Source:main.py  
1import pygame2import os3WIDTH, HEIGHT = 900, 5004WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))5pygame.display.set_caption("Game time baby")6VELOCITY = 57BULLET_VEL = 78MAX_BULLETS = 59WHITE = (255, 255, 255)10BLACK = (0,0,0)11RED = (255, 0, 0)12YELLOW = (255, 255, 0)13BORDER = pygame.Rect(WIDTH // 2 - 5, 0, 10, HEIGHT)14SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 65, 5515FPS = 6016YELLOW_GOT_HIT = pygame.USEREVENT + 1 # code for custom user events that we can check for and handle17RED_GOT_HIT = pygame.USEREVENT + 218YELLOW_SPACESHIP_IMG = pygame.image.load(os.path.join('Assets', 'spaceship_yellow.png'))19YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(20    YELLOW_SPACESHIP_IMG, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)21RED_SPACESHIP_IMG = pygame.image.load(os.path.join('Assets', 'spaceship_red.png'))22RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(23    RED_SPACESHIP_IMG, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)24BACKGROUND = pygame.image.load(os.path.join('Assets', 'above_earth.png'))25def handle_red_movement(keys_pressed, red):26    if keys_pressed[pygame.K_a] and red.x - VELOCITY > 0: # LEFT for red ship27        red.x -= VELOCITY28    if keys_pressed[pygame.K_d] and red.x + VELOCITY + red.width < BORDER.x: # RIGHT29        red.x += VELOCITY30    if keys_pressed[pygame.K_w] and red.y - VELOCITY > 0: #UP31        red.y -= VELOCITY32    if keys_pressed[pygame.K_s] and red.y + VELOCITY + red.height < HEIGHT - 15: #DOWN33        red.y += VELOCITY34def handle_yellow_movement(keys_pressed, yellow):35    if keys_pressed[pygame.K_LEFT] and yellow.x - VELOCITY > BORDER.x + BORDER.width: # LEFT for yellow ship36        yellow.x -= VELOCITY37    if keys_pressed[pygame.K_RIGHT] and yellow.x + VELOCITY + yellow.width < WIDTH: # RIGHT38        yellow.x += VELOCITY39    if keys_pressed[pygame.K_UP] and yellow.y - VELOCITY > 0: #UP40        yellow.y -= VELOCITY41    if keys_pressed[pygame.K_DOWN] and yellow.y + VELOCITY + yellow.height < HEIGHT - 15: #DOWN42        yellow.y += VELOCITY43def draw_window(red, yellow, red_bullets, yellow_bullets):44    WINDOW.fill(WHITE)45    pygame.draw.rect(WINDOW, BLACK, BORDER)46    WINDOW.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y)) # order of drawing matters. Spaceship would not be visible if we had done it before color fill47    WINDOW.blit(RED_SPACESHIP, (red.x, red.y))48    for bullet in red_bullets:49        pygame.draw.rect(WINDOW, RED, bullet)50    for bullet in yellow_bullets:51        pygame.draw.rect(WINDOW, YELLOW, bullet)52    pygame.display.update()53def handle_bullets(yellow_bullets, red_bullets, yellow, red):54    # loop through red bullets and check if they hit end of screen or collide with other character 55    # vice versa for yellow56    for bullet in red_bullets:57        bullet.x += BULLET_VEL58        if pygame.Rect.colliderect(bullet, yellow):59            pygame.event.post(pygame.event.Event(YELLOW_GOT_HIT))60            red_bullets.remove(bullet)61            62        elif bullet.x > WIDTH:63            red_bullets.remove(bullet)64    for bullet in yellow_bullets:65        bullet.x -= BULLET_VEL66        if red.colliderect(bullet): 67            pygame.event.post(pygame.event.Event(RED_GOT_HIT))68            yellow_bullets.remove(bullet)69        elif bullet.x < 0:70            yellow_bullets.remove(bullet)71def main():72    red = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)73    yellow = pygame.Rect(850, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)74    yellow_bullets = []75    red_bullets = []76    clock = pygame.time.Clock()77    run = True78    while run:79        clock.tick(FPS) # controls speed of while loop - run it 60 times per second at maximum80        for event in pygame.event.get():81            if event.type == pygame.QUIT:82                run = False83                pygame.quit()84            if event.type == pygame.KEYDOWN:85                if event.key == pygame.K_r and len(red_bullets) < MAX_BULLETS:86                    bullet = pygame.Rect(87                        red.x + red.width, red.y + red.height // 2 - 2, 10, 5) # display x, display y, rect width, rect height88                    red_bullets.append(bullet)89                90                if event.key == pygame.K_m and len(yellow_bullets) < MAX_BULLETS:91                    bullet = pygame.Rect(yellow.x, yellow.y + yellow.height // 2 - 2, 10, 5) # display x, display y, rect width, rect height92                    yellow_bullets.append(bullet)93        94        keys_pressed = pygame.key.get_pressed()95        handle_red_movement(keys_pressed, red)96        handle_yellow_movement(keys_pressed, yellow)97        handle_bullets(yellow_bullets, red_bullets, red, yellow)98        99        draw_window(red, yellow, red_bullets, yellow_bullets)100        101    102if __name__ == "__main__": #only run main if we directly run this file...tocolor.js
Source:tocolor.js  
1function toyellow(){2	/* 绿åé» */3	$('.green').removeClass('green').addClass('yellow');4	$('.green-g').removeClass('green-g').addClass('yellow-y');5	$('.green-none').removeClass('green-none').addClass('yellow-none');6	$('.green-left').removeClass('green-left').addClass('yellow-left');7	$('.green-box').removeClass('green-box').addClass('yellow-box');8	/*èåé»  */9	$('.blue').removeClass('blue').addClass('yellow');10	$('.blue-b').removeClass('blue-b').addClass('yellow-y');11	$('.blue-none').removeClass('blue-none').addClass('yellow-none');12	$('.blue-left').removeClass('blue-left').addClass('yellow-left');13	$('.blue-box').removeClass('blue-box').addClass('yellow-box');14	/* 红åé» */15	$('.red').removeClass('red').addClass('yellow');...colors-yellow-deprecated.js
Source:colors-yellow-deprecated.js  
1const scaleYellow = require('./scale-yellow');2const colorsYellow = {3  'color-yellow-dark': scaleYellow['yellow-800'],4  'color-yellow-base': scaleYellow['yellow-600'],5  'color-yellow-mid': scaleYellow['yellow-500'],6  'color-yellow-light': scaleYellow['yellow-400'],7  'color-yellow-lightest': scaleYellow['yellow-200'],8};...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!!
