代码示例库

搜索、筛选并收藏你需要的 Python 代码片段。

重置
#入门 #字符串 #数字 #列表 #列表 #循环 #切片 #if #列表 #字典 #字典 #while #while #函数 #函数 #类 #类 #文件 #异常 #测试 #测试 #Pygame #Pygame #matplotlib #随机漫步 #CSV #API #Django #Django #Django

Pygame游戏循环

第12章 武装飞船

演示Pygame窗口创建与主循环。

import sys
import pygame

pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("外星人入侵")
bg_color = (230, 230, 230)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    screen.fill(bg_color)
    pygame.dis
...

外星人碰撞检测

第13章 外星人

演示sprite碰撞检测逻辑。

# 子弹与外星人群碰撞
hits = pygame.sprite.groupcollide(bullets, aliens, True, True)

if hits:
    for aliens_hit in hits.values():
        score += 10 * len(aliens_hit)

# 外星人到达底部
for alien in aliens.copy():
    if alien.rect.bottom >= screen_rect.bottom:
        ship_hit()
        break