pygame参考文档
https://www.pygame.org/docs/tut/PygameIntro.html
1.pygame加载图片
import sys, pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
ball = pygame.image.load("intro_ball.gif")
red = pygame.Color(255, 0, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#画线
pygame.draw.line(screen, red, (10, 10), (200, 200), 10)
#画矩形 pygame.draw.rect(screen,red,(10,20,200,300),10)#坐标加长宽
#画圆 pygame.draw.circle(screen,red,(100,100),50,5)
#图片的绘制
screen.blit(ball, (100, 100))
pygame.display.flip()
2.pygame加载字体和音乐
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((500, 500))
red = pygame.Color(255, 0, 0)
#字体,从系统中获取字体,必须是存在的
# font = pygame.font.SysFont('华文新魏', 40, False, False)# 是否粗体,斜体
#从字体文件中加载字体
font=pygame.font.Font('./STXINWEI.TTF',40)
#文字对象渲染
text = font.render('得分', True, red)
#加载音乐
bg_music = pygame.mixer.music.load("neverM.mp3")
pygame.mixer.music.set_volume(0.2)#0~1,值越小音量越小
pygame.mixer.music.play(-1)#-1,循环播放背景音乐
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#设置字体位置
screen.blit(text, (20, 20))
#更新
pygame.display.flip()
3.pygame实现图片帧率切换
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((500, 500))
image = pygame.image.load('hero1.png')
image2 = pygame.image.load('hero2.png')
clock = pygame.time.Clock()
counter = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#利用帧速率进行图片切换控制,一秒钟60帧
clock.tick(60)
#屏幕绘制白色背景
screen.fill(pygame.Color(255, 255, 255))
if counter % 5 == 0:
screen.blit(image, (20, 20))
#绘制图片
else:
screen.blit(image2, (20, 20))
pygame.display.flip()
counter+=1
碰撞检测
import sys, pygame
pygame.init()
size = width, height = 320, 240
print(type(size))
speed = [2, 2]
black = 0, 0, 0
#得到屏幕对象
screen = pygame.display.set_mode((320, 240))
class Block(pygame.sprite.Sprite):
# Constructor. Pass in the color of the block,
# and its x and y position
def __init__(self, color, width, height,init_pos):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
self.rect.topleft=init_pos
screen = pygame.display.set_mode(size)
#实例化精灵对象
sprite_1 = Block(pygame.Color(255, 0, 0), 50, 50, (50, 50))
sprite_2 = Block(pygame.Color(0, 255, 0), 50, 50, (150, 150))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
#向屏幕上加载精灵
screen.blit(sprite_1.image, sprite_1.rect)
screen.blit(sprite_2.image, sprite_2.rect)
#矩形检测碰撞
rest = pygame.sprite.collide_rect(sprite_1, sprite_2)
print("rest", rest)
#指定可碰区域的大小
rest2 = pygame.sprite.collide_rect_ratio(1)(sprite_1, sprite_2)
print(rest2)
pygame.display.flip()