Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Prompt:
- "Based on the game 'pong' create a gdscript (godot 4) which spawns two players each opposite side of the screen, surrounded by a wall, both players have to score hits on the opponent back wall with the ball. The game uses physics but with no gravity, when the ball hits one of the player paddles gets pushed so it can gain momentum, also when the ball hits one of the walls (upper or lower) gets a bit of propulsion (1/10 of a player's paddle). A score of each player should be shown at the center top of the screen. The left player paddle should be blue and the right one red. The ball is green, but when it gains more speed it should turn yellow. And when it hits something it should turn white for a moment, the go back to the previous color. Apart from scoring, the ball shld explode if it hits one of the player's back wall. Then reappear to continue another round. The game ends when one of the player scores 5."
- # Code 1:
- extends Node2D
- const BALL_SPEED = 400
- const BALL_ACCELERATION = 100
- const BALL_COLOR = Color(0, 1, 0)
- const BALL_HIT_COLOR = Color(1, 1, 1)
- const BALL_MAX_SPEED = 800
- const BALL_EXPLOSION_FORCE = 1000
- const SCORE_LIMIT = 5
- const PADDLE_SPEED = 500
- const PADDLE_COLOR_LEFT = Color(0, 0, 1)
- const PADDLE_COLOR_RIGHT = Color(1, 0, 0)
- const PADDLE_SIZE = Vector2(20, 100)
- const PADDLE_OFFSET = 50
- const WALL_SIZE = Vector2(10, 800)
- const WALL_COLOR = Color(1, 1, 1)
- var ball_velocity = Vector2()
- var ball_speed = BALL_SPEED
- var ball_color = BALL_COLOR
- var score_left = 0
- var score_right = 0
- func _ready():
- # create walls
- var wall_left = create_wall(Vector2(0, 0))
- var wall_right = create_wall(Vector2(get_viewport_rect().size.x - WALL_SIZE.x, 0))
- var wall_top = create_wall(Vector2(0, 0), true)
- var wall_bottom = create_wall(Vector2(0, get_viewport_rect().size.y - WALL_SIZE.y), true)
- # create paddles
- var paddle_left = create_paddle(PADDLE_COLOR_LEFT, PADDLE_OFFSET, get_viewport_rect().size.y/2)
- var paddle_right = create_paddle(PADDLE_COLOR_RIGHT, get_viewport_rect().size.x - PADDLE_SIZE.x - PADDLE_OFFSET, get_viewport_rect().size.y/2)
- # create ball
- var ball = create_ball(get_viewport_rect().size/2)
- func _process(delta):
- # move paddles
- move_paddle($PaddleLeft, "w", "s")
- move_paddle($PaddleRight, "up", "down")
- # move ball
- move_ball()
- # check collisions
- check_collisions()
- func create_wall(position, horizontal=false):
- var wall = KinematicBody2D.new()
- var shape = RectangleShape2D.new()
- shape.set_extents(WALL_SIZE/2)
- var collision = CollisionShape2D.new()
- collision.shape = shape
- wall.add_child(collision)
- wall.set_position(position)
- wall.set_collision_layer_bit(0, false) # disable collisions with ball and paddles
- wall.set_collision_layer_bit(1, true) # enable collisions with ball and paddles
- if horizontal:
- wall.set_rotation(deg2rad(90))
- wall.set_scale(Vector2(1, -1)) # flip vertically to match screen coordinates
- wall.set_name("Wall")
- add_child(wall)
- return wall
- func create_paddle(color, x, y):
- var paddle = KinematicBody2D.new()
- var shape = RectangleShape2D.new()
- shape.set_extents(PADDLE_SIZE/2)
- var collision = CollisionShape2D.new()
- collision.shape = shape
- paddle.add_child(collision)
- paddle.set_position(Vector2(x, y))
- paddle.set_collision_layer_bit(0, false) # disable collisions with other paddles and walls
- paddle.set_collision_layer_bit(1, true) # enable collisions with
- # Network error
- # Code 2:
- extends Node2D
- var score_left = 0
- var score_right = 0
- var ball_speed = 300
- func _ready():
- spawn_players()
- spawn_ball()
- func spawn_players():
- var player_left = Paddle.new("Left", Color.rgb(0, 0, 255))
- var player_right = Paddle.new("Right", Color.rgb(255, 0, 0))
- add_child(player_left)
- add_child(player_right)
- player_left.set_position(Vector2(50, get_viewport_rect().size.y / 2))
- player_right.set_position(Vector2(get_viewport_rect().size.x - 50, get_viewport_rect().size.y / 2))
- func spawn_ball():
- var ball = Ball.new(Color.rgb(0, 255, 0))
- add_child(ball)
- ball.set_position(Vector2(get_viewport_rect().size.x / 2, get_viewport_rect().size.y / 2))
- var direction = Vector2(rand_range(-1, 1), rand_range(-1, 1)).normalized()
- ball.set_velocity(direction * ball_speed)
- func score(player_name):
- if player_name == "Left":
- score_left += 1
- elif player_name == "Right":
- score_right += 1
- if score_left >= 5 or score_right >= 5:
- game_over()
- else:
- spawn_ball()
- func game_over():
- # Implement game over logic here
- class Paddle extends KinematicBody2D:
- export var paddle_color = Color.white
- export var paddle_speed = 500
- export var paddle_size = Vector2(20, 100)
- var name = ""
- func _ready():
- set_collision_layer_bit(0, false) # disable default layer for paddles
- set_collision_layer_bit(1, true) # enable layer 1 for paddles
- set_collision_mask_bit(0, false) # disable collision with default layer
- set_collision_mask_bit(1, true) # enable collision with layer 1
- set_shape(Rect2(Vector2(0, 0), paddle_size))
- func _physics_process(delta):
- var input_vector = Vector2()
- if name == "Left":
- if Input.is_action_pressed("paddle_up"):
- input_vector.y -= 1
- if Input.is_action_pressed("paddle_down"):
- input_vector.y += 1
- elif name == "Right":
- if Input.is_action_pressed("paddle2_up"):
- input_vector.y -= 1
- if Input.is_action_pressed("paddle2_down"):
- input_vector.y += 1
- move_and_slide(input_vector * paddle_speed)
- func set_name(name):
- self.name = name
- func set_color(color):
- paddle_color = color
- func set_position(position):
- set_translation(position)
- class Ball extends KinematicBody2D:
- export var ball_color = Color.white
- export var ball_radius = 10
- export var ball_mass = 1
- var velocity = Vector2()
- func _ready():
- set_collision_layer_bit(0, false) # disable default layer for ball
- set_collision_layer_bit(1, true) # enable layer 1 for ball
- set_collision_mask_bit(0, false) # disable collision with default layer
- set_collision_mask_bit
- # Network error..
- # Added to the prompt:."Please comment the code and implement the game over."
- # Code 3:
- extends Node2D
- const BALL_SPEED = 300
- const MAX_BALL_SPEED = 600
- const BALL_ACCELERATION = 50
- const BALL_COLOR = Color(0, 1, 0) # Green
- const BALL_EXPLOSION_COLOR = Color(1, 1, 1) # White
- const BALL_EXPLOSION_DURATION = 0.5 # seconds
- const BALL_SCORE_COLOR = Color(1, 1, 1) # White
- const PADDLE_WIDTH = 20
- const PADDLE_HEIGHT = 100
- const PADDLE_SPEED = 500
- const PADDLE_COLOR_LEFT = Color(0, 0, 1) # Blue
- const PADDLE_COLOR_RIGHT = Color(1, 0, 0) # Red
- const WALL_WIDTH = 20
- const WALL_COLOR = Color(1, 1, 1) # White
- const SCORE_FONT = "res://path/to/font.tres"
- const SCORE_SIZE = 48
- const MAX_SCORE = 5
- var ball: KinematicBody2D
- var ball_velocity: Vector2 = Vector2.ZERO
- var ball_color: Color = BALL_COLOR
- var left_paddle: KinematicBody2D
- var right_paddle: KinematicBody2D
- var left_score: int = 0
- var right_score: int = 0
- var left_score_label: Label
- var right_score_label: Label
- var game_over: bool = false
- func _ready():
- # Spawn the ball in the middle of the screen
- ball = KinematicBody2D.new()
- var ball_shape = CircleShape2D.new()
- ball_shape.radius = 10
- var ball_collision = CollisionShape2D.new()
- ball_collision.shape = ball_shape
- ball_collision.name = "ball"
- ball.add_child(ball_collision)
- ball.position = Vector2.ZERO
- ball.velocity = ball_velocity
- add_child(ball)
- # Spawn the left paddle on the left side of the screen
- left_paddle = KinematicBody2D.new()
- var left_paddle_shape = Rect2(-PADDLE_WIDTH/2, -PADDLE_HEIGHT/2, PADDLE_WIDTH, PADDLE_HEIGHT)
- var left_paddle_collision = CollisionShape2D.new()
- left_paddle_collision.shape = left_paddle_shape
- left_paddle_collision.name = "left_paddle"
- left_paddle.add_child(left_paddle_collision)
- # Code 4:
- extends Node2D
- # Constants
- const BALL_SPEED = 400
- const BALL_COLOR = Color.green()
- const BALL_EXPLODE_COLOR = Color.white()
- const BALL_EXPLODE_TIME = 1.0
- const BALL_MIN_SPEED = 200
- const BALL_MAX_SPEED = 800
- const SCORE_TO_WIN = 5
- # Nodes
- var left_paddle: Paddle
- var right_paddle: Paddle
- var ball: Ball
- var left_score_label: Label
- var right_score_label: Label
- # Variables
- var left_score = 0
- var right_score = 0
- var ball_speed = BALL_SPEED
- func _ready():
- # Spawn the paddles
- left_paddle = Paddle.new()
- left_paddle.color = Color.blue()
- left_paddle.position = Vector2(-300, 0)
- add_child(left_paddle)
- right_paddle = Paddle.new()
- right_paddle.color = Color.red()
- right_paddle.position = Vector2(300, 0)
- add_child(right_paddle)
- # Spawn the ball
- ball = Ball.new()
- ball.color = BALL_COLOR
- ball.position = Vector2(0, 0)
- ball.velocity = Vector2(rand_range(-1, 1), rand_range(-1, 1)).normalized() * ball_speed
- add_child(ball)
- # Spawn the score labels
- left_score_label = Label.new()
- left_score_label.text = str(left_score)
- left_score_label.align = Label.ALIGN_CENTER
- left_score_label.position = Vector2(-100, -200)
- add_child(left_score_label)
- right_score_label = Label.new()
- right_score_label.text = str(right_score)
- right_score_label.align = Label.ALIGN_CENTER
- right_score_label.position = Vector2(100, -200)
- add_child(right_score_label)
- # Set up collision shapes
- left_paddle.get_node("CollisionShape2D").set_shape_as_one_way_collision(0,
- # Network Error
Add Comment
Please, Sign In to add comment