{"id":169752,"student_id":12205,"content":"import pygame\nimport random\n\n# Initialize Pygame\npygame.init()\n\n# Set the game screen dimensions\nSCREEN_WIDTH = 640\nSCREEN_HEIGHT = 480\nscreen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))\n\n# Load the game font\nfont = pygame.font.Font(None, 36)\n\n# Load the game sounds\nbounce_sound = pygame.mixer.Sound('bounce.wav')\n\n# Set the ball and paddle properties\nBALL_RADIUS = 10\nBALL_COLOR = (255, 255, 255)\nBALL_SPEED = 5\nball = pygame.draw.circle(screen, BALL_COLOR, (SCREEN_WIDTH//2, SCREEN_HEIGHT//2), BALL_RADIUS)\n\nPADDLE_WIDTH = 100\nPADDLE_HEIGHT = 10\nPADDLE_COLOR = (255, 255, 255)\npaddle = pygame.draw.rect(screen, PADDLE_COLOR, (SCREEN_WIDTH//2-PADDLE_WIDTH//2, SCREEN_HEIGHT-PADDLE_HEIGHT-10, PADDLE_WIDTH, PADDLE_HEIGHT))\n\n# Set the game loop\nrunning = True\nwhile running:\n # Handle events\n for event in pygame.event.get():\n if event.type == pygame.QUIT:\n running = False\n\n # Move the paddle based on user input\n keys = pygame.key.get_pressed()\n if keys[pygame.K_LEFT] and paddle.left \u003e 0:\n paddle.move_ip(-5, 0)\n if keys[pygame.K_RIGHT] and paddle.right \u003c SCREEN_WIDTH:\n paddle.move_ip(5, 0)\n\n # Move the ball and handle collisions\n ball.move_ip(BALL_SPEED, BALL_SPEED)\n if ball.left \u003c 0 or ball.right \u003e SCREEN_WIDTH:\n BALL_SPEED = -BALL_SPEED\n bounce_sound.play()\n if ball.top \u003c 0:\n BALL_SPEED = -BALL_SPEED\n bounce_sound.play()\n if ball.colliderect(paddle):\n BALL_SPEED = -BALL_SPEED\n bounce_sound.play()\n\n # Draw the game screen\n screen.fill((0, 0, 0))\n pygame.draw.circle(screen, BALL_COLOR, ball.center, BALL_RADIUS)\n pygame.draw.rect(screen, PADDLE_COLOR, paddle)\n score = font.render(\"Score: \" + str(random.randint(0, 100)), True, (255, 255, 255))\n screen.blit(score, (10, 10))\n pygame.display.flip()\n\n# Quit Pygame\npygame.quit()\nIn this example code, we use Pygame to create a simple game of bouncing ball. The game features a ball that bounces off the walls and the player-controlled paddle, and the player earns points for every bounce. The code loads the game font and sounds, sets the ball and paddle properties, and then updates their position based on user input and collision detection. The game screen is drawn using a black background color, and the ball, paddle, and score are drawn on top of it using various Pygame drawing methods. Finally, we use the font object to render the score, and the flip() method to update the display.\n\n\n\n\n\n\n\n","created_at":"2020-07-23T15:25:01.106+08:00","updated_at":"2023-04-03T14:54:08.485+08:00","name":"測試","language":"python","screenshot":{"url":"https://cdn9.koding.school/uploads/project/screenshot/169752/21b5cb8e84e4d8f498e8f6b88f77655a.jpg"},"parent_id":4,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":39,"hashid":"zpeswq9g","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":2932205,"file_name":"calm.mp3","project_id":169752,"asset_id":182924,"created_at":"2020-07-23T15:25:01.119+08:00","updated_at":"2020-07-23T15:25:01.119+08:00"},{"id":2932206,"file_name":"koding.png","project_id":169752,"asset_id":182923,"created_at":"2020-07-23T15:25:01.121+08:00","updated_at":"2020-07-23T15:25:01.121+08:00"},{"id":2932207,"file_name":"bg.jpg","project_id":169752,"asset_id":182922,"created_at":"2020-07-23T15:25:01.122+08:00","updated_at":"2020-07-23T15:25:01.122+08:00"},{"id":2932208,"file_name":"cloud.png","project_id":169752,"asset_id":182921,"created_at":"2020-07-23T15:25:01.123+08:00","updated_at":"2020-07-23T15:25:01.123+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
截圖
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦