Pygame游戏开发基础—余婷

# 01-游戏最小系统
import pygame # 引入pygame

pygame.init() # 初始化
screen = pygame.display.set_mode((600, 400)) # 创建窗口
pygame.display.set_caption('窗体标题') # 设置标题

# 游戏事件循环
while True:
    for event in pygame.event.get():
        # 退出游戏
        if event.typt == pygame.QUIT:
            exit()
            
# 02-显示图片
import pygame

pygame.init() # 初始化
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('显示图片')

# 设置背景颜色
sereen.fill((255, 255, 255))

# 加载图片
img = pygame.image.load('files/boer.jpg')

# 渲染图片
# blit(渲染对象, 坐标)
screen.blit(img, (0, 0))

# 操作图片
# 获取图片大小
w, h = img.get_size()

# 将图片显示在右下角
screen.blit(img, (600-w, 400-h))

# 旋转和缩放
# transfrom.scale(缩放对象, 目标大小) - 不按比例缩放
new_img1 = pygame.transform.scale(img, (100, 100))
screen.blit(new_img1, (210, 0))

# transfrom.rotozoom(缩放/旋转对象, 旋转角度, 缩放比例) - 按比例缩放
new_img2 = pygame.transfrom.rotozoom(img, 90, 0.5)
screen.blit(new_img2, (0, 200))

# 刷新页面
# pygame.display.flip()    - 第一次刷新
# pygame.display.update()  - 第一次以后
pygame.display.flip() 


while True:
    for event in pygame.event.get():
        if event.typt == pygame.QUIT:
            exit()
            
# 03-显示文字
import pygame

pygame.init() # 初始化
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('显示文字')
sereen.fill((255, 255, 255))

# 显示文字
# 创建字体对象
# font.Font(字体文件, 字号)
font = pygame.font.Font('files/a.ttf', 30)

# 创建文件对象
# rander(文字内容, True, 文字颜色, 背景颜色)
text = font.rander('Pygame 你好!', True, (255, 0, 0), (255, 255, 0))

# 渲染到窗口
screen.blit(text, (0, 0))

# 操作文字对象
# 获取文字大小
w, h = text.get_size()
screen.blit(text, (600-w, 400-h))

# 缩放和旋转
new_text1 = pygame.transform.scale(text, (200, 50))
screen.blit(new_text1, (400, 50))

new_text2 = pygame.transform.rotozoom(text, 90 ,2)
screen.blit(new_text2, (0, 120))

pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.typt == pygame.QUIT:
            exit()
            
# 04-显示图形
import pygame

pygame.init() # 初始化
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('显示图形')
sereen.fill((255, 255, 255))

# 显示图形
# 画直线
# line(窗口, 线的颜色, 线的起点, 线的终点, 线宽=1)
pygame.draw.line(screen, (255, 0, 0), False, (10, 20), (200, 20))

# 画折线
# lines(窗口, 线的颜色, 是否闭合, 点列表, 线宽=1)
points = [(10, 300), (100, 160), (180, 260), (300, 100)]
pygame.draw.lins(screen, (0, 255, 0), points, 2)

# 画圆
# circle(窗口, 线的颜色, 圆心坐标, 半径, 线宽=0 - 填充)
pygame.draw.circle(screen, (0, 0, 255), (200, 250), 100)

# 画矩形
# rect(窗口, 线的颜色, 矩形范围(x,y,宽度,高度), 线宽=0 - 填充)
pygame.draw.rect(screen, (120, 120, 60), (30, 70, 200, 100), 3)

# 画椭圆
ellipse(窗口, 线的颜色, 矩形范围(x,y,宽度,高度), 线宽=0 - 填充)
pygame.draw.ellipse(screen, (255, 0, 0), (30, 70, 200, 100) ,3)

# 画弧线
# arc(窗口, 线的颜色, 矩形范围(x,y,宽度,高度), 起始弧度, 终止弧度, 线宽=1)
from math import pi
pygame.draw.arc(screen, (0, 0, 0), (30, 70, 200, 100), 0, pi, 5)

'''
--- 弧度 ---
2 * pi = 360°
pi = 180°

     90
     
180        0

     270
     
'''

pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.typt == pygame.QUIT:
            exit()
            
# 05-动画原理
import pygame

WIN_WIDTH = 400 # 窗口宽度
WIN_HEIGHT = 600 # 窗口高度

pygame.init() # 初始化

screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('动画原理')
sereen.fill((255, 255, 255))
pygame.display.flip()

# 显示静态球
y = 100
r = 50

pygame.draw.circle(screen, (255, 0, 0), (100, y), 50)
pygame.display.update()

num = 1
while True:
    num += 1
    
    # 移动动画
    if num % 10 == 0:
        pygame.draw.circle(screen, (255, 255, 255), (100, y), 50)
        # screen.blit(img, (ix, iy))
        y = y + 1
        pygame.draw.circle(screen, (255, 0, 0), (100, y), 50)
        pygame.display.update()
        
    # 缩放动画
    if num % 10 == 0:
        pygame.draw.circle(screen, (255, 255, 255), (100, y), r)
        r += 1
        pygame.draw.circle(screen, (255, 0, 0), (100, y), r)
        pygame.display.update()
        
    # 旋转动画
    if num % 10 == 0:
        screen.fill((255, 255, 255))
        pygame.draw.circle(screen, (255, 0, 0), (100, y), r)
        # pygame.draw.rect(screen, (255, 255, 255), (ix, iy, iw, ih))
        d += 1
        new_image = pygame.transform.rotozoom(img, d, 1)
        screen.blit(new_image, (ix, iy))
        pygame.display.update
    
    # 移动和缩放
    if num % 10 == 0:
        pygame.draw.circle(screen, (255, 255, 255), (100, y), r)
        # screen.blit(img, (ix, iy))
        y += 10
        r += 1
        pygame.draw.circle(screen, (255, 0, 0), (100, y), r)
        pygame.display.update()
        
    
    for event in pygame.event.get():
        if event.typt == pygame.QUIT:
            exit()
            
# 06-动画灵活应用
import pygame

WIN_WIDTH = 400 # 窗口宽度
WIN_HEIGHT = 600 # 窗口高度

pygame.init() # 初始化

screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('动画灵活应用')
sereen.fill((255, 255, 255))
pygame.display.flip()

# 画静态球
x, y = 200, 30
r = 30
y_speed = 1
pygame.draw.circle(screen, (0, 255, 0), (x, y), r)
pygame.display.update()

num = 0
while True:
    num += 1
    # 上下反弹效果
    if num % 10 == 0:
        screen.fill((255, 255, 255))
        
        # 修改y坐标
        y += y_speed
        
        # 检测边界
        if y >= WIN_HEIGHT - r: # 底边
            y_speed = y_speed * -1
        if y <= r: # 顶边
            y_speed = y_speed * -1
            
        pygame.draw.circle(screen, (0, 255, 0), (x, y), r)
        pygame.display.update()
    
    for event in pygame.event.get():
        if event.typt == pygame.QUIT:
            exit()
            
# 07-事件检测
import pygame
from random import randint

WIN_WIDTH = 400 # 窗口宽度
WIN_HEIGHT = 600 # 窗口高度

pygame.init() # 初始化

screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('事件检测')
sereen.fill((255, 255, 255))
pygame.display.flip()

# 字体
font = pygame.font.Font('files/font.ttf', 30)

count = 0 # 统计事件发生的次数
while True:    
    for event in pygame.event.get():
        count += 1
        print(str(count))
        
        # event.typt 用来区分不同类型的事件
        '''
        鼠标事件
        MOUSEBUTTONDOWN - 鼠标按下
        MOUSEBUTTONUP - 鼠标弹起
        MOUSERMOTION - 鼠标移动
        
        鼠标位置属性(点在哪) - pos
        
        键盘事件
        KEYDOWN - 按键按下
        KEYUP - 按键弹起
        
        按键值属性(哪个按键) - key
        '''
        if event.typt == pygame.QUIT:
            exit()
            
        elif event.type == pygame.MOUSEBUTTONDOWN:
            print('鼠标按下', event.pos)
            mx, my = event.pos # 取鼠标的坐标
            pygame.drwa.circle(screen, (255, 255, 0), (mx, my), 50)
            pygame.display.update()
            
        elif event.type == pygame.MOUSEBUTTONUP:
            print('鼠标弹起')
            
        elif event.type == pygame.MOUSERMOTION:
            print('鼠标移动', event.pos)
            mx, my = event.pos # 取鼠标的坐标
            r = randint(0, 255)
            g = randint(0, 255)
            b = randint(0, 255)
            
            pygame.drwa.circle(screen, (r, g, b), (mx, my), 50)
            pygame.display.update()
            
        elif event.type == pygame.KEYDOWN:
            print('按键按下', event.key, chr(event.key))
            text = fount.render(chr(event.key), True, (255, 0, 0))
            screen.fill((255, 255, 255))
            screen.blit(text,(0, 300))
            pygame.display.update()
            
        elif event.type == pygame.KEYUP:
            print('按键弹起', event.key)
# 08-按钮点击
import pygame

WIN_WIDTH = 400 # 窗口宽度
WIN_HEIGHT = 600 # 窗口高度

pygame.init() # 初始化

screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('按钮点击')
sereen.fill((255, 255, 255))
pygame.display.flip()

bx, by, bw, bh = 30, 100, 100, 50
font = pygame.font.Font('files/font.ttf', 30)

# 确定按钮
pygame.draw.rect(screen, (255, 0, 0),(bx, by, bw, bh))
text1 = font.render('确定', True, (255, 255, 255))
tw1, th1 = text1.get_size() # 取text1宽高
tx1 = bx + bw/2 - tw1/2
ty1 = by + bh/2 - th1/2
screen.blit(text1, (tx1, ty1))

# 取消按钮
pygame.draw.rect(screen, (0, 255, 0), (bx, by + 100, bw, bh))
text2 = font.render('取消', True, (255, 255, 255))
tw2, th2 = text2.get_size()
tx2 = bx + bw/2 - tw2/2
ty2 = by + 100 + bh/2 - th2/2
screen.blit(text2, (tx2, ty2))

pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.typt == pygame.QUIT:
            exit()
            
        if event.type == pygame.MOUSEBUTTONDOWN:
            mx, my = event.pos
            # 是否点击确认按钮
            if (bx <= mx <= bx + bw) and (by <= my <= by + bh):
                # 按钮点击的反应
                pygame.draw.rect(screen, (200, 200, 200),(bx, by, bw, bh))
                screen.blit(text1, (tx1, ty1))
                pygame.display.update()
                
                print('确定按钮被点击')
                
            # 是否点击取消按钮
            elif (bx <= mx <= bx + bw) and (by + 100 <= my <= by + 100 + bh):
                # 按钮点击的反应
                pygame.draw.rect(screen, (200, 200, 200), (bx, by + 100, bw, bh))
                screen.blit(text2, (tx2, ty2))
                
                print('取消按钮被点击')
                
        if event.type == pygame.MOUSEBUTTONUP:
            if (bx <= mx <= bx + bw) and (by <= my <= by + bh):
                pygame.draw.rect(screen, (255, 0, 0),(bx, by, bw, bh))
                screen.blit(text1, (tx1, ty1))
                pygame.display.update()
                
            elif (bx <= mx <= bx + bw) and (by + 100 <= my <= by + 100 + bh):
                pygame.draw.rect(screen, (0, 255, 0), (bx, by + 100, bw, bh))
                screen.blit(text2, (tx2, ty2))
                pygame.display.update()
# 09-角色移动
import pygame

WIN_WIDTH = 400 # 窗口宽度
WIN_HEIGHT = 600 # 窗口高度

pygame.init() # 初始化

screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('角色移动')
sereen.fill((255, 255, 255))
pygame.display.flip()

# 放坦克图片
tank_up = pygame.image.load('files/p1tankU.gif') # 向上
tank_down = pygame.image.load('files/p1tankD.gif') # 向下
tank_left = pygame.image.load('files/p1tankL.gif') # 向左
tank_right = pygame.image.load('files/p1tankR.gif') # 向右
tank = tank_up # 默认

tank_x, tank_y = 200, 450
screen.blit.(tank, (tank_x, tank_y))
pygame.display.update()

is_move = False # 是否移动
x_speed = 0 # x速度
y_speed = 0 # y速度

while True:
    if is_move == Ture:
        screen.fill((255, 255, 255))
        # tank_y -=2
        tank_x += x_speed
        tank_y += y_speed
        screen.blit(tank, (tank_x, tank_y))
        pygame.display.update()

    for event in pygame.event.get():
        if event.typt == pygame.QUIT:
            exit()
        if event.type == pygame.KEYDOWN:
            # 上
            if chr(event.key) == 'w':
                is_move = True
                x_speed = 0
                y_speed = -2
                tank = tank_up
                
            # 左    
            if chr(event.key) == 'a':
                is_move = True
                x_speed = -2
                y_speed = 0
                tank = tank_left
                
            # 下
            if chr(event.key) == 's':
                is_move = True
                x_speed = 0
                y_speed = 2
                tank = tank_down
                
            # 右
            if chr(event.key) == 'd':
                is_move = True
                x_speed = 2
                y_speed = 0
                tank = tank_right
                
        if event.type == pygame.KEYUP:
            is_move = False
            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值