Vixente

4 GDScript request of a Pong game to ChatGPT

Mar 22nd, 2023 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 9.86 KB | Gaming | 0 0
  1.  
  2. Prompt:
  3. "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."
  4.  
  5. # Code 1:
  6.  
  7. extends Node2D
  8.  
  9. const BALL_SPEED = 400
  10. const BALL_ACCELERATION = 100
  11. const BALL_COLOR = Color(0, 1, 0)
  12. const BALL_HIT_COLOR = Color(1, 1, 1)
  13. const BALL_MAX_SPEED = 800
  14. const BALL_EXPLOSION_FORCE = 1000
  15. const SCORE_LIMIT = 5
  16.  
  17. const PADDLE_SPEED = 500
  18. const PADDLE_COLOR_LEFT = Color(0, 0, 1)
  19. const PADDLE_COLOR_RIGHT = Color(1, 0, 0)
  20. const PADDLE_SIZE = Vector2(20, 100)
  21. const PADDLE_OFFSET = 50
  22.  
  23. const WALL_SIZE = Vector2(10, 800)
  24. const WALL_COLOR = Color(1, 1, 1)
  25.  
  26. var ball_velocity = Vector2()
  27. var ball_speed = BALL_SPEED
  28. var ball_color = BALL_COLOR
  29.  
  30. var score_left = 0
  31. var score_right = 0
  32.  
  33. func _ready():
  34.     # create walls
  35.     var wall_left = create_wall(Vector2(0, 0))
  36.     var wall_right = create_wall(Vector2(get_viewport_rect().size.x - WALL_SIZE.x, 0))
  37.     var wall_top = create_wall(Vector2(0, 0), true)
  38.     var wall_bottom = create_wall(Vector2(0, get_viewport_rect().size.y - WALL_SIZE.y), true)
  39.    
  40.     # create paddles
  41.     var paddle_left = create_paddle(PADDLE_COLOR_LEFT, PADDLE_OFFSET, get_viewport_rect().size.y/2)
  42.     var paddle_right = create_paddle(PADDLE_COLOR_RIGHT, get_viewport_rect().size.x - PADDLE_SIZE.x - PADDLE_OFFSET, get_viewport_rect().size.y/2)
  43.    
  44.     # create ball
  45.     var ball = create_ball(get_viewport_rect().size/2)
  46.  
  47. func _process(delta):
  48.     # move paddles
  49.     move_paddle($PaddleLeft, "w", "s")
  50.     move_paddle($PaddleRight, "up", "down")
  51.    
  52.     # move ball
  53.     move_ball()
  54.    
  55.     # check collisions
  56.     check_collisions()
  57.  
  58. func create_wall(position, horizontal=false):
  59.     var wall = KinematicBody2D.new()
  60.     var shape = RectangleShape2D.new()
  61.     shape.set_extents(WALL_SIZE/2)
  62.     var collision = CollisionShape2D.new()
  63.     collision.shape = shape
  64.     wall.add_child(collision)
  65.     wall.set_position(position)
  66.     wall.set_collision_layer_bit(0, false) # disable collisions with ball and paddles
  67.     wall.set_collision_layer_bit(1, true) # enable collisions with ball and paddles
  68.     if horizontal:
  69.         wall.set_rotation(deg2rad(90))
  70.     wall.set_scale(Vector2(1, -1)) # flip vertically to match screen coordinates
  71.     wall.set_name("Wall")
  72.     add_child(wall)
  73.     return wall
  74.  
  75. func create_paddle(color, x, y):
  76.     var paddle = KinematicBody2D.new()
  77.     var shape = RectangleShape2D.new()
  78.     shape.set_extents(PADDLE_SIZE/2)
  79.     var collision = CollisionShape2D.new()
  80.     collision.shape = shape
  81.     paddle.add_child(collision)
  82.     paddle.set_position(Vector2(x, y))
  83.     paddle.set_collision_layer_bit(0, false) # disable collisions with other paddles and walls
  84.     paddle.set_collision_layer_bit(1, true) # enable collisions with
  85.  
  86. # Network error    
  87. # Code 2:
  88.  
  89. extends Node2D
  90.  
  91. var score_left = 0
  92. var score_right = 0
  93. var ball_speed = 300
  94.  
  95. func _ready():
  96.     spawn_players()
  97.     spawn_ball()
  98.  
  99. func spawn_players():
  100.     var player_left = Paddle.new("Left", Color.rgb(0, 0, 255))
  101.     var player_right = Paddle.new("Right", Color.rgb(255, 0, 0))
  102.     add_child(player_left)
  103.     add_child(player_right)
  104.     player_left.set_position(Vector2(50, get_viewport_rect().size.y / 2))
  105.     player_right.set_position(Vector2(get_viewport_rect().size.x - 50, get_viewport_rect().size.y / 2))
  106.  
  107. func spawn_ball():
  108.     var ball = Ball.new(Color.rgb(0, 255, 0))
  109.     add_child(ball)
  110.     ball.set_position(Vector2(get_viewport_rect().size.x / 2, get_viewport_rect().size.y / 2))
  111.     var direction = Vector2(rand_range(-1, 1), rand_range(-1, 1)).normalized()
  112.     ball.set_velocity(direction * ball_speed)
  113.  
  114. func score(player_name):
  115.     if player_name == "Left":
  116.         score_left += 1
  117.     elif player_name == "Right":
  118.         score_right += 1
  119.     if score_left >= 5 or score_right >= 5:
  120.         game_over()
  121.     else:
  122.         spawn_ball()
  123.  
  124. func game_over():
  125.     # Implement game over logic here
  126.  
  127. class Paddle extends KinematicBody2D:
  128.  
  129.     export var paddle_color = Color.white
  130.     export var paddle_speed = 500
  131.     export var paddle_size = Vector2(20, 100)
  132.  
  133.     var name = ""
  134.  
  135.     func _ready():
  136.         set_collision_layer_bit(0, false) # disable default layer for paddles
  137.         set_collision_layer_bit(1, true) # enable layer 1 for paddles
  138.         set_collision_mask_bit(0, false) # disable collision with default layer
  139.         set_collision_mask_bit(1, true) # enable collision with layer 1
  140.         set_shape(Rect2(Vector2(0, 0), paddle_size))
  141.  
  142.     func _physics_process(delta):
  143.         var input_vector = Vector2()
  144.         if name == "Left":
  145.             if Input.is_action_pressed("paddle_up"):
  146.                 input_vector.y -= 1
  147.             if Input.is_action_pressed("paddle_down"):
  148.                 input_vector.y += 1
  149.         elif name == "Right":
  150.             if Input.is_action_pressed("paddle2_up"):
  151.                 input_vector.y -= 1
  152.             if Input.is_action_pressed("paddle2_down"):
  153.                 input_vector.y += 1
  154.         move_and_slide(input_vector * paddle_speed)
  155.  
  156.     func set_name(name):
  157.         self.name = name
  158.  
  159.     func set_color(color):
  160.         paddle_color = color
  161.  
  162.     func set_position(position):
  163.         set_translation(position)
  164.  
  165. class Ball extends KinematicBody2D:
  166.  
  167.     export var ball_color = Color.white
  168.     export var ball_radius = 10
  169.     export var ball_mass = 1
  170.  
  171.     var velocity = Vector2()
  172.  
  173.     func _ready():
  174.         set_collision_layer_bit(0, false) # disable default layer for ball
  175.         set_collision_layer_bit(1, true) # enable layer 1 for ball
  176.         set_collision_mask_bit(0, false) # disable collision with default layer
  177.         set_collision_mask_bit
  178.  
  179. # Network error..
  180.  
  181. # Added to the prompt:."Please comment the code and implement the game over."
  182.  
  183. # Code 3:
  184.  
  185. extends Node2D
  186.  
  187. const BALL_SPEED = 300
  188. const MAX_BALL_SPEED = 600
  189. const BALL_ACCELERATION = 50
  190. const BALL_COLOR = Color(0, 1, 0) # Green
  191. const BALL_EXPLOSION_COLOR = Color(1, 1, 1) # White
  192. const BALL_EXPLOSION_DURATION = 0.5 # seconds
  193. const BALL_SCORE_COLOR = Color(1, 1, 1) # White
  194. const PADDLE_WIDTH = 20
  195. const PADDLE_HEIGHT = 100
  196. const PADDLE_SPEED = 500
  197. const PADDLE_COLOR_LEFT = Color(0, 0, 1) # Blue
  198. const PADDLE_COLOR_RIGHT = Color(1, 0, 0) # Red
  199. const WALL_WIDTH = 20
  200. const WALL_COLOR = Color(1, 1, 1) # White
  201. const SCORE_FONT = "res://path/to/font.tres"
  202. const SCORE_SIZE = 48
  203. const MAX_SCORE = 5
  204.  
  205. var ball: KinematicBody2D
  206. var ball_velocity: Vector2 = Vector2.ZERO
  207. var ball_color: Color = BALL_COLOR
  208. var left_paddle: KinematicBody2D
  209. var right_paddle: KinematicBody2D
  210. var left_score: int = 0
  211. var right_score: int = 0
  212. var left_score_label: Label
  213. var right_score_label: Label
  214. var game_over: bool = false
  215.  
  216. func _ready():
  217.     # Spawn the ball in the middle of the screen
  218.     ball = KinematicBody2D.new()
  219.     var ball_shape = CircleShape2D.new()
  220.     ball_shape.radius = 10
  221.     var ball_collision = CollisionShape2D.new()
  222.     ball_collision.shape = ball_shape
  223.     ball_collision.name = "ball"
  224.     ball.add_child(ball_collision)
  225.     ball.position = Vector2.ZERO
  226.     ball.velocity = ball_velocity
  227.     add_child(ball)
  228.    
  229.     # Spawn the left paddle on the left side of the screen
  230.     left_paddle = KinematicBody2D.new()
  231.     var left_paddle_shape = Rect2(-PADDLE_WIDTH/2, -PADDLE_HEIGHT/2, PADDLE_WIDTH, PADDLE_HEIGHT)
  232.     var left_paddle_collision = CollisionShape2D.new()
  233.     left_paddle_collision.shape = left_paddle_shape
  234.     left_paddle_collision.name = "left_paddle"
  235.     left_paddle.add_child(left_paddle_collision)
  236.    
  237.        
  238. # Code 4:
  239.  
  240. extends Node2D
  241.  
  242. # Constants
  243. const BALL_SPEED = 400
  244. const BALL_COLOR = Color.green()
  245. const BALL_EXPLODE_COLOR = Color.white()
  246. const BALL_EXPLODE_TIME = 1.0
  247. const BALL_MIN_SPEED = 200
  248. const BALL_MAX_SPEED = 800
  249. const SCORE_TO_WIN = 5
  250.  
  251. # Nodes
  252. var left_paddle: Paddle
  253. var right_paddle: Paddle
  254. var ball: Ball
  255. var left_score_label: Label
  256. var right_score_label: Label
  257.  
  258. # Variables
  259. var left_score = 0
  260. var right_score = 0
  261. var ball_speed = BALL_SPEED
  262.  
  263. func _ready():
  264.     # Spawn the paddles
  265.     left_paddle = Paddle.new()
  266.     left_paddle.color = Color.blue()
  267.     left_paddle.position = Vector2(-300, 0)
  268.     add_child(left_paddle)
  269.     right_paddle = Paddle.new()
  270.     right_paddle.color = Color.red()
  271.     right_paddle.position = Vector2(300, 0)
  272.     add_child(right_paddle)
  273.  
  274.     # Spawn the ball
  275.     ball = Ball.new()
  276.     ball.color = BALL_COLOR
  277.     ball.position = Vector2(0, 0)
  278.     ball.velocity = Vector2(rand_range(-1, 1), rand_range(-1, 1)).normalized() * ball_speed
  279.     add_child(ball)
  280.  
  281.     # Spawn the score labels
  282.     left_score_label = Label.new()
  283.     left_score_label.text = str(left_score)
  284.     left_score_label.align = Label.ALIGN_CENTER
  285.     left_score_label.position = Vector2(-100, -200)
  286.     add_child(left_score_label)
  287.     right_score_label = Label.new()
  288.     right_score_label.text = str(right_score)
  289.     right_score_label.align = Label.ALIGN_CENTER
  290.     right_score_label.position = Vector2(100, -200)
  291.     add_child(right_score_label)
  292.  
  293.     # Set up collision shapes
  294.     left_paddle.get_node("CollisionShape2D").set_shape_as_one_way_collision(0,
  295.  
  296. # Network Error
Tags: Godot chatGPT
Add Comment
Please, Sign In to add comment