클래스 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 중국어 웹사이트의 기타 관련 기사를 참조하세요!