第 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中文网其他相关文章!