2016-03-31 30 views
4
import pygame 
#Colors, Allways you need colors!!!! 
BLACK = (0, 0, 0) 
GREEN = (0, 255, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
ORANGE = (255, 115, 0) 
YELLOW = (242, 255, 0) 
BROWN = (115, 87, 39) 
PURPLE = (298, 0, 247) 
GRAY = (168, 168, 168) 
PINK = (255, 0, 234) 
BLUE = (0, 0 , 255) 
pygame.init() 
# Screen 
screen = pygame.display.set_mode([700,500]) 
#Name of thewindow 
pygame.display.set_caption("Trial to make PONG") 
# Any variables! 

x_speed = 0 
y_speed = 0 

x_coord = 10 
y_coord = 250 

x = 670 
y = 250 
other_speed = 0 
other_speed2 = 0 

rect_x = 50 
rect_y = 50 

rect_change_x = 5 
rect_change_y = 5 
clock = pygame.time.Clock() 
#Sounds,maybe needed? 

#Main Loop__________ 

done = False 

while not done: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 

      # User pressed down on a key 
     elif event.type == pygame.KEYDOWN: 
      # Figure out if it was an arrow key. If so 
      # adjust speed. 

      if event.key == pygame.K_UP: 
       y_speed = -5 
      elif event.key == pygame.K_DOWN: 
       y_speed = 5 
      elif event.key == pygame.K_w: 
       other_speed2 = -5 
      elif event.key == pygame.K_s: 
       other_speed2 = 5 

    # User let up on a key 
     elif event.type == pygame.KEYUP: 
     # If it is an arrow key, reset vector back to zero 
      if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: 
       x_speed = 0 
      elif event.key == pygame.K_UP or event.key == pygame.K_DOWN: 
       y_speed = 0 
      elif event.key == pygame.K_w or event.key == pygame.K_s: 
       other_speed2 = 0 


# Move the object according to the speed vector. 
    x_coord += x_speed 
    y_coord += y_speed 
    x += x_speed 
    y += other_speed2 


    screen.fill(BLACK) 

    pygame.draw.rect(screen,BLUE,[x_coord,y_coord,20,60]) 
    pygame.draw.rect(screen,YELLOW,[x,y,20,60]) 

    if x > 650 or x < 0: 

    # Draw the rectangle 
    pygame.draw.ellipse(screen, BLUE, [rect_x, rect_y, 50, 50]) 

    # Move the rectangle starting point 
    rect_x += rect_change_x 
    rect_y += rect_change_y 

    if rect_x > 650 or rect_x < 0: 
     rect_change_x = rect_change_x * -1 
    if rect_y > 450 or rect_y < 0: 
     rect_change_y = rect_change_y * -1 





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

pygame.quit() 

私は青と黄色の長方形と呼ばれるこのポンゲームで2つのパドルがあります。私はそれらを移動することができますが、彼らは画面から移動します。それをどうやって防ぐのですか? Ivはオンラインで見ましたが、何も動作していないようです。私は青/黄色のパドルがそれを打つとき、彼らはそれ以上動きませんが、コードを破壊しなければならない方法をどうしたらいいのか、衝突ポイントの引数を作るためにスクリーンの周りに長方形を置くことについて考えましたか?助けてくれてありがとう..オブジェクトが画面外に移動する

+1

書式設定は少し –

+0

が – HALLOPEOPLE

答えて

4

y座標を変更する前に、画面の端からずれていないかどうかを確認する必要があります。

if y_coord + y_speed >= 0 and y_coord + y_speed + 60 <= 500: 
    y_coord += y_speed 

数字を使用するのが少し混乱することがありますが、ハードコーディングを避けるべきです。 display_height,display_width、およびy_speedが可変である方が良いです。基本的には、初期化変数の外では、数値として0を持つ必要があります。また、if文に+ y_speedを残したときにどうなるかを注意してください。

+0

私はあなたが言ったように試みましたが、それはパドル(青のもの)だけでなく、以前よりも画面に行くよりも、同じことが起こる場合、私は+ y_speedのifステートメントを残します。とにかく助けていただきありがとうございます。 – HALLOPEOPLE

+0

お待ちください、あなたはそれを間違って実装しています。私はコードをPythonに直接接続し、完全に機能しました。コードをコピー&ペーストしてフォーマットを確認しましたか?既に存在する 'y_coord + = y_speed'の前に' 'y_coord + y_speed> = 0とy_coord + y_speed + 60 <= 500:'行を追加するだけです。私はそれを削除せずにこれを追加したような気がします。 – MANA624

+0

ああ。それを見て、それは私のせいでした。ありがとう、それは働いた。そして私はこれを私の他の黄色のパドルにも適用できますか? – HALLOPEOPLE

0

画面の高さを500ピクセルに設定しているようです。パドルが移動する前に画面の限界に達しているかどうかを確認することができます。

newY = y_coord + y_speed 
if newY >= 0 and newY + PADDLE_HEIGHT < SCREEN_HEIGHT: 
    y_coord = newy 
+0

は、あなたの答えをありがとう...それを教えたが、それはように見えるdoes notのアップマックです仕事...私はパドルを動かそうとすると左右にわずかに動きますが、それ以降は動きません。あなたがパドルhiehgtを250以上の大きさに変更すると、まだ画面から外れます。 – HALLOPEOPLE

3

Rect classを使用することをお勧めします。そのようなケースを簡単に処理できるからです。また、コードがより洗練され、短くなります。

Rectの使用例を次に示します。私は単純に画面を離れることができないプレイヤーのパドルを確保するためにclamp_ipを使用することに注意してください:

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() 
+0

こんにちは!ちょうどいくつかの質問があります。まず、ドキュメントを読み込んだ後でクランプを明確にするだけです。パドルが画面のRectの外側にある場合はパドルを調整し、残した場合はそれを左端に戻します。第二に、移動後に締め付けるべきではない?現在、私はパドルがクランプされていることを想像し、(例えば、エッジを越えて)上に移動し、ややオフスクリーンで描かれます。事前に感謝 –

+1

1)はい。 2)クランプはすでに移動後です。 'clamp_ip'の直前の行。 – sloth

+0

私の間違いは、クランプの後に動いているボールです!パドルではない –

関連する問題