Okay, I can help with that!
- Who made me: I am a large language model, trained by Google.
-
Comparison: 9.9 is bigger than 9.11.
- When comparing decimal numbers, you look at the digits from left to right.
- Both numbers have '9' in the ones place.
- Next, look at the first decimal place (the tenths place). 9.9 has a '9' and 9.11 has a '1'.
- Since 9 is bigger than 1, 9.9 is the larger number.
-
Simple Classic Snake Game in Python:
Here's a simple snake game using the
pygamelibrary.Before you run:
- You need to have Python installed.
- You need to install the
pygamelibrary. Open your terminal or command prompt and type:
Game Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
import pygame import time import random # Initialize pygame pygame.init() # Define colors white = (255, 255, 255) yellow = (255, 255, 102) black = (0, 0, 0) red = (213, 50, 80) green = (0, 255, 0) blue = (50, 153, 213) # Set display dimensions dis_width = 600 dis_height = 400 dis = pygame.display.set_mode((dis_width, dis_height)) pygame.display.set_caption('Simple Snake Game') # Clock for controlling game speed clock = pygame.time.Clock() snake_block_size = 10 snake_speed = 15 # Font styles font_style = pygame.font.SysFont("bahnschrift", 25) score_font = pygame.font.SysFont("comicsansms", 35) def show_score(score): value = score_font.render("Your Score: " + str(score), True, yellow) dis.blit(value, [0, 0]) def our_snake(snake_block_size, snake_list): for x in snake_list: pygame.draw.rect(dis, black, [x[0], x[1], snake_block_size, snake_block_size]) def message(msg, color): mesg = font_style.render(msg, True, color) # Center the message text_rect = mesg.get_rect(center=(dis_width / 2, dis_height / 2)) dis.blit(mesg, text_rect) def gameLoop(): game_over = False game_close = False # Starting position of the snake (center of the screen) x1 = dis_width / 2 y1 = dis_height / 2 # Change in position x1_change = 0 y1_change = 0 # Snake body (list of blocks) snake_List = [] Length_of_snake = 1 # Position of the food foodx = round(random.randrange(0, dis_width - snake_block_size) / 10.0) * 10.0 foody = round(random.randrange(0, dis_height - snake_block_size) / 10.0) * 10.0 while not game_over: # --- Game Over Screen --- while game_close == True: dis.fill(blue) message("You Lost! Press Q-Quit or C-Play Again", red) show_score(Length_of_snake - 1) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game_over = True game_close = False if event.key == pygame.K_c: gameLoop() # Restart game if event.type == pygame.QUIT: game_over = True game_close = False # --- Event Handling --- for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: # Prevent snake from immediately reversing if x1_change == 0: x1_change = -snake_block_size y1_change = 0 elif event.key == pygame.K_RIGHT: if x1_change == 0: x1_change = snake_block_size y1_change = 0 elif event.key == pygame.K_UP: if y1_change == 0: y1_change = -snake_block_size x1_change = 0 elif event.key == pygame.K_DOWN: if y1_change == 0: y1_change = snake_block_size x1_change = 0 # --- Game Logic --- # Check for boundary collision if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0: game_close = True # Update snake position x1 += x1_change y1 += y1_change # --- Drawing --- dis.fill(blue) pygame.draw.rect(dis, green, [foodx, foody, snake_block_size, snake_block_size]) snake_Head = [] snake_Head.append(x1) snake_Head.append(y1) snake_List.append(snake_Head) # Keep snake length correct if len(snake_List) > Length_of_snake: del snake_List[0] # Check for collision with self for x in snake_List[:-1]: if x == snake_Head: game_close = True # Draw the snake our_snake(snake_block_size, snake_List) # Show score show_score(Length_of_snake - 1) # Update the display pygame.display.update() # --- Food Collision --- if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, dis_width - snake_block_size) / 10.0) * 10.0 foody = round(random.randrange(0, dis_height - snake_block_size) / 10.0) * 10.0 Length_of_snake += 1 # Control game speed clock.tick(snake_speed) # Uninitialize pygame and quit pygame.quit() quit() # Start the game gameLoop()
How to Play:
- Save the code above as a Python file (e.g.,
snake_game.py). - Make sure you've installed
pygame. - Open a terminal or command prompt, navigate to the directory where you saved the file.
- Run the game using:
python snake_game.py - Use the arrow keys to control the snake.
- Eat the green food to grow.
- Avoid hitting the walls or running into yourself!
- If you lose, press 'C' to play again or 'Q' to quit.
Enjoy the game!