クラス 1: Python の基本と Pygame のセットアップ
クラス 2: ゲームコンポーネントを理解する
クラス 3: ゲームの物理学と動き
クラス 4: サウンドと音楽の操作
クラス 5: ゲームの状態とレベル
クラス 6: AI と敵の行動
クラス 7: ゲームの最適化とデバッグ
クラス 8: 最終プロジェクトのプレゼンテーションとまとめ
例:
# Integer score = 10 # Float player_speed = 2.5 # String player_name = "Chukwudi" # Boolean game_over = False
例:
# For loop for i in range(5): print("Hello", i) # While loop countdown = 5 while countdown > 0: print("Countdown:", countdown) countdown -= 1
例:
def greet_player(name): print("Welcome,", name) greet_player(player_name)
pip install pygame
例:
import pygame # Initialize Pygame pygame.init() # Create a game window screen = pygame.display.set_mode((800, 600)) # Set window title pygame.display.set_caption("My First Game") # Main game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Quit Pygame pygame.quit()
目標: ユーザーがマウスを使用して画面上に描画できる基本的なアプリを作成します。
import pygame # Initialize Pygame pygame.init() # Set up the screen screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Drawing App") # Colors white = (255, 255, 255) black = (0, 0, 0) # Set background color screen.fill(white) # Main loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEMOTION: if event.buttons[0]: # Left mouse button is pressed pygame.draw.circle(screen, black, event.pos, 5) pygame.display.flip() pygame.quit()
描画アプリを変更します:
シェイプの作成:
例:
# Load an image and create a sprite player_image = pygame.image.load("player.png") player_rect = player_image.get_rect() # Draw the sprite on the screen screen.blit(player_image, player_rect)
例:
for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: print("Left arrow key pressed")
例:
for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: print("Mouse button clicked at", event.pos)
例:
# Check if two rectangles overlap if player_rect.colliderect(other_rect): print("Collision detected!")
Goal: Create a game where a ball falls from the top of the screen, and the player must catch it with a paddle.
import pygame import random # Initialize Pygame pygame.init() # Screen setup screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Catch the Ball") # Colors white = (255, 255, 255) black = (0, 0, 0) # Player (Paddle) paddle = pygame.Rect(350, 550, 100, 10) # Ball ball = pygame.Rect(random.randint(0, 750), 0, 50, 50) ball_speed = 5 # Main game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Move paddle with arrow keys keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and paddle.left > 0: paddle.move_ip(-5, 0) if keys[pygame.K_RIGHT] and paddle.right < 800: paddle.move_ip(5, 0) # Move ball down ball.move_ip(0, ball_speed) # Check for collision if ball.colliderect(paddle): print("Caught!") ball.topleft = (random.randint(0, 750), 0) # Redraw screen screen.fill(white) pygame.draw.rect(screen, black, paddle) pygame.draw.ellipse(screen, black, ball) pygame.display.flip() pygame.quit()
Add Scoring:
Increase Difficulty:
This concludes Week 1. you (students) should now be comfortable with Python basics, Pygame setup, and creating simple interactive games. I encourage you to experiment with the exercises to deepen your understanding.
以上がイントロ: ゲーム用 Python 第 1 週の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。