Week 7 ( Blackbox AI, Python )
Blackbox
Blackbox is an Ai tool that was able to teach me Python. Python is something I been wanting to understand and learn. I was able to type " teach me python". Black box provided me with sources such as
1) python.org
4) dataquest.io
It gave me sources that was all to go to and learn Python. These sources talked about variance and tools when it comes too python. It was able to go step by step when it came to functions, loops, basic operations, modules and lambda functions.
1# String
2name = "John Doe"
3
4# Integer
5age = 25
6
7# Float
8height = 5.9
9
10# Boolean
11is_student = True
Blackbox Ai gave me a data structure and was giving me functions using the " def" keyword.
It only gave me a few steps and tips such say variables, data structure, control flow and functions.
1# Function 2def greet(name): 3 print(f"Hello, {name}!") 4 5greet("John Doe")
Overall, Blackbox AI to use and I even had it code me a ping pong game in python and it ended up in a draw.
I enjoyed using this website and Ai tool. This helped me understand how python works and I will continue to look at other websites.
1import sys
2import pygame
3
4# Initialize Pygame
5pygame.init()
6
7# Set up some constants
8WIDTH, HEIGHT = 800, 600
9BALL_RADIUS = 10
10PADDLE_WIDTH, PADDLE_HEIGHT = 20, 100
11PADDLE_SPEED = 5
12
13# Set up the colors
14WHITE = (255, 255, 255)
15BLACK = (0, 0, 0)
16
17# Create the game screen
18screen = pygame.display.set_mode((WIDTH, HEIGHT))
19pygame.display.set_caption("Ping Pong")
20
21# Create the paddles
22paddle_left = pygame.Rect(50, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
23paddle_right = pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
24
25# Create the ball
26ball = pygame.Rect(WIDTH // 2 - BALL_RADIUS, HEIGHT // 2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2)
27ball_speed_x, ball_speed_y = 3, 3
28
29def move_paddle(paddle, up_key, down_key):
30 if paddle.y + paddle.height < HEIGHT:
31 if up_key:
32 paddle.y -= PADDLE_SPEED
33 if paddle.y > 0:
34 if down_key:
35 paddle.y += PADDLE_SPEED
36
37def move_ball(ball, ball_speed_x, ball_speed_y):
38 ball.x += ball_speed_x
39 ball.y += ball_speed_y
40
41 if ball.y + ball.height > HEIGHT:
42 ball_speed_y *= -1
43 if ball.y < 0:
44 ball_speed_y *= -1
45
46 if ball.x + ball.width > WIDTH:
47 ball.x = WIDTH // 2 - ball.width // 2
48 ball.y = HEIGHT // 2 - ball.height // 2
49 ball_speed_x *= -1
50
51 if ball.x < 0:
52 ball.x = WIDTH // 2 - ball.width // 2
53 ball.y = HEIGHT // 2 - ball.height // 2
54 ball_speed_x *= -1
55
56# Game loop
57running = True
58while running:
59 for event in pygame.event.get():
60 if event.type == pygame.QUIT:
61 running = False
62
63 # Get the current key presses
64 keys = pygame.key.get_pressed()
65 move_paddle(paddle_left, keys[pygame.K_w], keys[pygame.K_s])
66 move_paddle(paddle_right, keys[pygame.K_UP], keys[pygame.K_DOWN])
67
68 # Move the ball
69 move_ball(ball, ball_speed_x, ball_speed_y)
70
71 # Check for collisions
72 if ball.colliderect(paddle_left) or ball.colliderect(paddle_right):
73 ball_speed_x *= -1
74
75 # Fill the screen with white
76 screen.fill(WHITE)
77
78 # Draw the paddles and ball
79 pygame.draw.rect(screen, BLACK, paddle_left)
80 pygame.draw.rect(screen, BLACK, paddle_right)
81 pygame.draw.circle(screen, BLACK, (ball.x, ball.y),
Comments
Post a Comment