import pygame
BLACK = pygame.color.Color('Black')
YELLOW = pygame.color.Color('Yellow')
BLUE = pygame.color.Color('Blue')
pygame.init()
screen = pygame.display.set_mode([700,500])
screen_rect = screen.get_rect()
pygame.display.set_caption("Trial to make PONG")
blue_rect = pygame.Rect(10, 250, 20, 60)
yellow_rect = pygame.Rect(670, 250, 20, 60)
ball_rect = pygame.Rect(50, 50, 50, 50)
ball_x_speed = 5
ball_y_speed = 5
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# check all pressed keys and move the paddles
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]: blue_rect.move_ip(0, -5)
if pressed[pygame.K_DOWN]: blue_rect.move_ip(0, 5)
if pressed[pygame.K_w]: yellow_rect.move_ip(0, -5)
if pressed[pygame.K_s]: yellow_rect.move_ip(0, 5)
# ensure paddles stay on screen
blue_rect.clamp_ip(screen_rect)
yellow_rect.clamp_ip(screen_rect)
# move the ball
ball_rect.move_ip(ball_x_speed, ball_y_speed)
# check if the ball needs to change direction
if ball_rect.x + ball_rect.width > screen_rect.width or ball_rect.x < 0:
ball_x_speed = ball_x_speed * -1
if ball_rect.y + ball_rect.height> screen_rect.height or ball_rect.y < 0:
ball_y_speed = ball_y_speed * -1
# draw everything
screen.fill(BLACK)
pygame.draw.ellipse(screen, BLUE, ball_rect)
pygame.draw.rect(screen,BLUE, blue_rect)
pygame.draw.rect(screen,YELLOW, yellow_rect)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Oyunda iki kürek var ve bir top etrafta zıplıyor. Topun raketle vurulduğunda bir çarpışma noktası yapmaya çalıştım. Pong'u yeniden yaratmaya çalışıyorum. Çarpış noktası işe yaramadı (belki de doğru yapılamadığı için).Top nasıl fırlatılır kürek fırlatılır
Kürek (seklinde Mavi ve Sarı) ve top (ball_rect
) arasında bir çarpış noktası nasıl oluşturabilirim diye merak ettim, top küreden sıçradı mı?
Sadece arka duvardan sıçrayan topu tutuyor gibi görünüyor. Kürekleri de kontrol etmeniz gerekecek. Kod, y koordinatlarını hesaba katmanız gerekmediği sürece, topun duvar kontrolüne benzer olacaktır. – sbochins