• BQNOJ
  • Trang chủ
  • Danh sách bài
  • Các bài nộp
  • Thành viên
    >
    • Tổ chức
    • Đóng góp
  • Các kỳ thi
  • Thông tin
    >
    • Máy chấm
    • Custom Checkers
    • Github
VI EN Đăng nhập  hoặc  Đăng ký

Haloya_Sato

  • Thông tin
  • Thống kê
  • Blog

Số bài đã giải: 45
Hạng điểm: #94
Tổng điểm: 10,56
Đóng góp: 0

Xem các bài nộp

Từ PYTHON 02

Thông tin

import pygame import random import time

Khởi tạo Pygame

pygame.init()

Kích thước màn hình

WIDTH, HEIGHT = 800, 600 screen = pygame.display.setmode((WIDTH, HEIGHT)) pygame.display.setcaption("Phản Bóng 1 Người Chơi") time.sleep(4)

Màu sắc

WHITE = (255, 255, 255) BLACK = (0, 0, 0) GREEN = (0, 255, 0) RED = (255, 0, 0)

Kích thước đối tượng

PLAYERWIDTH, PLAYERHEIGHT = 30, 15 BALL_RADIUS = 5

Vị trí và tốc độ của người chơi

player1pos = [WIDTH // 2 - PLAYERWIDTH // 2, HEIGHT - PLAYERHEIGHT - 10] player2pos = [WIDTH // 2 - PLAYERWIDTH // 2, 10] player1speed = 0 player2_speed = 0

Vị trí và tốc độ của quả bóng

ballpos = [WIDTH // 2, HEIGHT // 2] ballspeed = [random.choice([-2, 2]), random.choice([-2, 2])]

Biến kiểm soát trò chơi

running = True clock = pygame.time.Clock()

while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False

# Điều khiển người chơi
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
    player1_pos[0] -= 5
if keys[pygame.K_RIGHT]:
    player1_pos[0] += 5

# Tự động di chuyển player2 để đỡ bóng  
if ball_pos[1] < 250:  # Chỉ di chuyển khi bóng trên player2  
    if player2_pos[0] < ball_pos[0]:  
        player2_pos[0] += random.randint(3,5)  # Di chuyển sang phải  
    if player2_pos[0] > ball_pos[0]:  
        player2_pos[0] -= random.randint(3,5)  # Di chuyển sang trái

# Giới hạn di chuyển
player1_pos[0] = max(0, min(player1_pos[0], WIDTH - PLAYER_WIDTH))
player2_pos[0] = max(0, min(player2_pos[0], WIDTH - PLAYER_WIDTH))

# Cập nhật vị trí của quả bóng
ball_pos[0] += ball_speed[0]
ball_pos[1] += ball_speed[1]

# Va chạm với người chơi
if (player1_pos[0] < ball_pos[0] < player1_pos[0] + PLAYER_WIDTH and
        player1_pos[1] < ball_pos[1] < player1_pos[1] + PLAYER_HEIGHT):
    ball_speed[1] = -ball_speed[1]
    ball_speed[0] = random.choice([-2, 2])

if (player2_pos[0] < ball_pos[0] < player2_pos[0] + PLAYER_WIDTH and
        player2_pos[1] < ball_pos[1] < player2_pos[1] + PLAYER_HEIGHT):
    ball_speed[1] = -ball_speed[1]
    ball_speed[0] = random.choice([-2, 2])

# Va chạm với tường
if ball_pos[0] <= BALL_RADIUS or ball_pos[0] >= WIDTH - BALL_RADIUS:
    ball_speed[0] = -ball_speed[0]
if ball_pos[1] <= BALL_RADIUS:
    ball_speed[1] = -ball_speed[1]
if ball_pos[1] >= HEIGHT - BALL_RADIUS:
    print("Bạn đã thua!")
    running = False
if ball_pos[1] <= 5:
    print("Bạn đã thắng!")
    running = False

# Vẽ lại màn hình
screen.fill(BLACK)
pygame.draw.rect(screen, GREEN, (player1_pos[0], player1_pos[1], PLAYER_WIDTH, PLAYER_HEIGHT))
pygame.draw.rect(screen, RED, (player2_pos[0], player2_pos[1], PLAYER_WIDTH, PLAYER_HEIGHT))
pygame.draw.circle(screen, WHITE, (int(ball_pos[0]), int(ball_pos[1])), BALL_RADIUS)

pygame.display.flip()
clock.tick(60)

time.sleep(2) pygame.quit()

Huy hiệu

Người dùng này không có huy hiệu nào.

«    »
CN
T2
T3
T4
T5
T6
T7
Ít
Nhiều

dựa trên nền tảng DMOJ | theo dõi chúng tôi trên Github và Facebook