第 1 課:Python 基礎與 Pygame 設定
第二課:了解遊戲組件
第三課:遊戲物理與運動
第 4 課:使用聲音與音樂
第 5 課:遊戲狀態與等級
第 6 課:AI 與敵人行為
第七課:遊戲最佳化與除錯
第 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中文網其他相關文章!