【python游戏开发入门】pygame下载与安装教程
- Pygame 是一个利用SDL实现的模块。
- SDL(Simple DirectMedia Layer)是一套开源代码的跨平台多媒体开发库,使用C语言写成。多用于开发游戏、模拟器、媒体播放器等多媒体应用领域。
- Pygame功能:绘制图形、显示图像、动画效果、与键盘/鼠标和游戏手柄等外设交互、播放声音、碰撞检测。
import pygame
import sys
pygame.init()
size = width, height = 600, 400
speed = [-8, 4]
bg = (255, 255, 255)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请大家多多关照!")
turtle = pygame.image.load("resized_image.jpg")
turtle = pygame.transform.scale(turtle, (width // 4, height // 4))
position = turtle.get_rect()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
position = position.move(speed)
if position.left < 0 or position.right > width:
turtle = pygame.transform.flip(turtle, True, False)
speed[0] = -speed[0]
if position.top < 0 or position.bottom > height:
turtle = pygame.transform.flip(turtle, True, True)
speed[1] = -speed[1]
screen.fill(bg)
screen.blit(turtle, position)
pygame.display.flip()
pygame.time.delay(1)