
第 1 课:python 基础知识和 pygame 设置
第二课:了解游戏组件
第三课:游戏物理与运动
第 4 课:使用声音和音乐
立即学习“Python免费学习笔记(深入)”;
第五课:游戏状态和级别
第 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!")
目标: 创建一个游戏,球从屏幕顶部落下,玩家必须用球拍接住它。
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()
添加评分:
增加难度:
第一周到此结束。您(学生)现在应该熟悉 python 基础知识、pygame 设置以及创建简单的互动游戏。我鼓励您尝试练习以加深您的理解。
以上就是简介:Python 游戏第 1 周的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号