#Import the pygame library
import pygame

#Initialize the game
pygame.init()

#Set the window size
window_width = 800
window_height = 600

#Create the window
window = pygame.display.set_mode((window_width, window_height))

#Set the window title
pygame.display.set_caption('Pong')

#Set the clock
clock = pygame.time.Clock()

#Define colors
white = (255, 255, 255)
black = (0, 0, 0)

#Define game variables
ball_pos_x = window_width/2
ball_pos_y = window_height/2
ball_radius = 10
ball_vel_x = 3
ball_vel_y = 3

#Define paddles
paddle_width = 10
paddle_height = 80
paddle_1_pos_x = 20
paddle_1_pos_y = window_height/2 - paddle_height/2
paddle_2_pos_x = window_width - 20 - paddle_width
paddle_2_pos_y = window_height/2 - paddle_height/2

#Game loop
while True:
    #Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

#Update positions
ball_pos_x += ball_vel_x
ball_pos_y += ball_vel_y

 #Check for collisions
if ball_pos_x - ball_radius <= 0:
        ball_vel_x *= -1
elif ball_pos_x + ball_radius >= window_width:
        ball_vel_x *= -1
elif ball_pos_y - ball_radius <= 0:
        ball_vel_y *= -1
elif ball_pos_y + ball_radius >= window_height:
        ball_vel_y *= -1

#Draw the window
window.fill(black)
pygame.draw.circle(window, white, (int(ball_pos_x), int(ball_pos_y)), ball_radius)
pygame.draw.rect(window, white, (paddle_1_pos_x, paddle_1_pos_y, paddle_width, paddle_height))
pygame.draw.rect(window, white, (paddle_2_pos_x, paddle_2_pos_y, paddle_width, paddle_height))
pygame.display.update()

#Delay to get 60 fps
clock.tick(60)
#นำเข้าไลบรารี pygame นำเข้า pygame #เริ่มต้นเกม pygame.init() #ตั้งค่าขนาดหน้าต่าง window_width = 800 window_height = 600 #สร้างหน้าต่าง window = pygame.display.set_mode((window_width, window_height)) #ตั้งชื่อหน้าต่าง pygame.display.set_caption('Pong') #ตั้งนาฬิกา นาฬิกา = pygame.time.Clock() #กำหนดสี ขาว = (255, 255, 255) สีดำ = (0, 0, 0) #กำหนดตัวแปรเกม ball_pos_x = window_width/2 ball_pos_y = window_height/2 ball_radius = 10 ball_vel_x = 3 ball_vel_y = 3 #กำหนดไม้พาย Paddle_width = 10 ไม้พาย_height = 80 ไม้พาย_1_pos_x = 20 ไม้พาย_1_pos_y = window_height/2 - ไม้พาย_height/2 ไม้พาย_2_pos_x = window_width - 20 - ไม้พาย_ความกว้าง ไม้พาย_2_pos_y = window_ ส่วนสูง/2 - Paddle_height/2 #Game loop while True: #Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() #Update ตำแหน่ง ball_pos_x += ball_vel_x ball_pos_y += ball_vel_y #ตรวจสอบการชนถ้า ball_pos_x - ball_radius <= 0: ball_vel_x *= -1 elif ball_pos_x + ball_radius >= window_width: ball_vel_x *= -1 elif ball_pos_y - ball_radius <= 0: ball_vel_y *= -1 elif ball_pos_y + ball_radius >= window_height: ball_vel_y *= -1 #Draw หน้าต่าง window.fill(black) pygame.draw.circle(window, white, (int(ball_pos_x), int(ball_pos_y)), ball_radius) pygame.draw.rect(window , สีขาว, (paddle_1_pos_x, Paddle_1_pos_y, Paddle_width, Paddle_height)) pygame.draw.rect(หน้าต่าง, สีขาว, (paddle_2_pos_x, Paddle_2_pos_y, Paddle_width, Paddle_height)) pygame.display.update() #Delay เพื่อให้ได้ 60 fps clock.tick( 60)
spam fails_task lang_mismatch pii not_appropriate hate_speech sexual_content quality toxicity humor helpfulness creativity violence