Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0125-preview· openaiHugging Face Suzhen/CodeChat

I want to make a ping pong 2 players game on godot for my phone

  1. User

    I want to make a ping pong 2 players game on godot for my phone

  2. Assistant

    Creating a 2-players Ping Pong game for your phone in Godot involves several steps, including setting up the project, creating player paddles and the ball, adding movement, scoring, and finally ensuring it works well on a phone. Below is a simplified guide to get you started.

    Step 1: Setting Up the Project

    1. Open Godot and create a new project.
    2. Go to Project > Project Settings > Display > Window. Set "Width" and "Height" to match your phone's resolution for testing. You can adjust for different devices later.

    Step 2: Creating the Scene Structure

    1. Create a new scene (Main Scene).
    2. Add a Node2D as the root node and rename it to Main.
    3. Add two KinematicBody2D nodes as children of the Main, naming them Player1 and Player2. These will serve as the paddles.
    4. For each player, add a Sprite and a CollisionShape2D as children.
    5. Add another KinematicBody2D node for the ball, naming it Ball. Add a Sprite and a CollisionShape2D as children.

    Step 3: Designing Sprites

    1. Design simple rectangular sprites for the paddles and a circular one for the ball. You can create these images in an external program or use Godot's built-in shape drawing functionality.
    2. Set up the sprites for Player1, Player2, and Ball by dragging your created images into their respective Sprite nodes.

    Step 4: Adding Scripts

    1. Player Movement: Attach scripts to Player1 and Player2. Your script may look something like this for touch/mouse input:
    extends KinematicBody2D
    
    var speed = 400
    
    func _physics_process(delta):
        var motion = Vector2()
        if Input.is_action_pressed("ui_up"):
            motion.y -= 1
        elif Input.is_action_pressed("ui_down"):
            motion.y += 1
    
        motion = motion.normalized() * speed * delta
        move_and_slide(motion)
    
    1. For touchscreen controls, you might consider using InputEventScreenTouch or InputEventScreenDrag in the _input(event) method.

    2. Ball Movement: Add a script to the Ball node to handle its movement. Include logic for bouncing against walls and paddles.

    extends KinematicBody2D
    
    var velocity = Vector2(300, 300)
    
    func _physics_process(delta):
        var collision_info = move_and_collide(velocity * delta)
        if collision_info:
            velocity = velocity.bounce(collision_info.normal)
    

    Step 5: Implementing Logic for Scoring and Restarting

    1. Add Global script or use the Main node script to manage the game state, including scores, and restarting the game when a player scores.
    2. Use signals or direct function calls for when the ball reaches the left or right edge, indicating a score.

    Step 6: Testing and Exporting for Phones

    1. Test your game in the Godot editor. Adjust the game to work well with touch controls.
    2. When you're ready, export your game. Go to Project > Export, select your target platform (Android/iOS), and follow the dialogues to set up export options. You might need to download/export templates if you haven't done so yet.

    This guide gives a very foundational structure to your Ping Pong game. There are a lot of improvements and additional features you might consider, such as better control mechanics for touch, visual and audio effects, and AI for playing against the computer. Remember, making games is an iterative process. Start simple, then refine and expand.