{ "cells": [ { "cell_type": "code", "execution_count": 42, "id": "d0e8ea8e-2315-49ed-aa07-86f37561d09d", "metadata": {}, "outputs": [], "source": [ "import pygame\n", "import random\n", "import math\n", "\n", "# init pygame\n", "pygame.init()\n", "\n", "# init the game's screen with certain size\n", "screen_width = 800\n", "screen_height = 600 \n", "paddle_length = 50\n", "paddle_deep = 10\n", "\n", "screen = pygame.display.set_mode([screen_width, screen_height])\n", "\n", "pygame.display.set_caption(\"Pong modified game\")\n", "\n", "def paddle(x, y, plen, pdeep):\n", " pygame.draw.rect(screen, [255,205,0], [x,y,plen,pdeep])\n", " # pygame.display.update()\n", "\n", "def screen_update():\n", " pygame.display.update()\n", " \n", "def game_message(msg, x, y, color):\n", " font_style = pygame.font.SysFont(None, 50)\n", " mesg = font_style.render(msg, True, color)\n", " screen.blit(mesg, [x, y])\n", " screen_update()\n", "\n", "def ball(x, y, radius):\n", " pygame.draw.circle(screen, [0,120,120], [x, y], radius, 0)\n", "\n", "x1 = screen_width/2\n", "y1 = screen_height-10\n", "paddle(x1, y1, paddle_length, paddle_deep)\n", "screen_update()\n", "\n", "step = 15\n", "edge = 10" ] }, { "cell_type": "code", "execution_count": 43, "id": "c2241ce6-607e-41e3-a5bd-231d0c519846", "metadata": {}, "outputs": [], "source": [ "# clock\n", "clock = pygame.time.Clock()\n", "\n", "xdiff = 0\n", "ydiff = 0\n", "\n", "ballradius = 8\n", "ballstep = 10\n", "ballx = random.randint(0, screen_width)\n", "bally = random.randint(0, screen_height)\n", "ball(ballx, bally, ballradius)\n", "screen_update()\n", "bx = random.randint(-ballstep, ballstep)\n", "by = random.randint(-ballstep, ballstep)\n", "\n", "# Game loop: receive user events and process them\n", "playing = True\n", "gamespeed = 20\n", "while playing:\n", " pygame.display.set_caption(str(bx)+':'+str(by))\n", " for event in pygame.event.get():\n", " if event.type == pygame.QUIT:\n", " playing = False\n", " if event.type == pygame.KEYDOWN: # if key pressed\n", " if event.key == pygame.K_ESCAPE:\n", " playing = False\n", " if event.key == pygame.K_LEFT:\n", " pygame.display.set_caption(\"Left\")\n", " xdiff = -edge\n", " ydiff = 0\n", " if event.key == pygame.K_RIGHT:\n", " pygame.display.set_caption(\"Right\")\n", " xdiff = edge\n", " ydiff = 0\n", " if event.key == pygame.K_SPACE:\n", " xdiff = 0\n", " ydiff = 0\n", " # if paddle is not out of screen then move it\n", " if 0 <= x1+xdiff and x1+xdiff <= screen_width-paddle_length:\n", " x1 += xdiff\n", "\n", " # Make bouncing ball\n", " # write your code here...\n", "\n", " # clear screen\n", " screen.fill([0,0,0]) # fill screen with black color\n", " # draw the paddle\n", " paddle(x1, y1, paddle_length, paddle_deep)\n", " # draw the ball\n", " ball(ballx, bally, ballradius)\n", " screen_update()\n", "\n", " clock.tick(gamespeed)\n", "\n", "# quit the game\n", "pygame.quit()" ] }, { "cell_type": "code", "execution_count": null, "id": "0e9456be-55e0-402c-a8a7-6c0c3c8a8b2b", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }