游戏最基本系统

import pygame

# 1初始化操作
pygame.init()
# 2创建游戏窗口
# set_mode(大小)
window = pygame.display.set_mode((400, 600))
# 设置游戏名
pygame.display.set_caption('我的游戏')
# 设置背景颜色
window.fill((255, 255, 255))
pygame.display.flip()
# 3.让游戏保持一直运行的状态
# game loop 游戏循环(检测事件)
while True:
    # 4.检测事件(比如鼠标,键盘的点击检测)
    # 固定写法,event,get可以一次获取多个事件,所以要循环遍历
    for event in pygame.event.get():
        # 判断是否退出(点×)
        if event.type == pygame.QUIT:
            # 退出函数(一般不会这么简单,要保存)
            exit()

 

显示图片

import pygame

pygame.init()
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption('显示图片')
window.fill((255, 255, 255))

# =======一般写游戏开始页面静态图=========
# 1.准备图片,把图片加载到pycharm中
# pygame.image.load返回的是一个图片对象
image1 = pygame.image.load('playground.jpg')

# 2.渲染图片(把图片放到到游戏窗口中)
# blit(渲染对象,坐标)坐标以左上角作为坐标原点

window.blit(image1, (0, 0))
# 3.操作图片(改变图片位置,大小等)
# )1获取图片大小:w,h=image1.get_size
# )2旋转和缩放
#  scale(缩放对象,缩放目标大小)会发生生形变
new1 = pygame.transform.scale(image1, (100, 100))
window.blit(new1, (210, 0))
#  rotozoom(缩放或者旋转对象,旋转角度(逆时针),缩放比例)
new2 = pygame.transform.rotozoom(image1, 0, 0.5)
window.blit(new2, (0, 200))
# 4.刷新显示页面图片才能显示到游戏窗口中,每次修改游戏窗口都要刷新才能执行
pygame.display.flip()  # 第一次刷新用这个,第二次用pygame.display.update

while True:
    # ======一般写游戏帧的刷新=====
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

 

文字的使用

import pygame

pygame.init()
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption('显示文字')
window.fill((255, 255, 255))

# ==========显示文字===========
# 1.创建字体对象
# pygame.font.SysFont()系统字体,不常用
# Font(字体文件路径,字号)
font = pygame.font.Font('aa.ttf', 30)
# 2. 创建文字对象
# render(文章内容,Ture,文字颜色,背景颜色(可不写))
text = font.render('游戏你好', True, (255, 0, 0), (255, 255, 0))
# 3.渲染到窗口上
window.blit(text, (0, 0))
# 4.操作文字对象
# 1获取图片大小:w,h=image1.get_size
# )2旋转和缩放
#  scale(缩放对象,缩放目标大小)会发生生形变
#  rotozoom(缩放或者旋转对象,旋转角度(逆时针),缩放比例)
w, h = text.get_size()
window.blit(text, (800 - w, 600 - h))
new1 = pygame.transform.rotozoom(text, 0, 2.5)
window.blit(new1, (0, 200))
# 5.刷新
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

 

显示图形

mport pygame
from math import pi

pygame.init()
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption('显示图形')
window.fill((255, 255, 255))
# ==========显示图形===========
# 1,画直线pygame.draw.line(画在哪里,线的颜色,(线的起点,线的终点),线宽(默认为1))
pygame.draw.line(window, (255, 0, 0), (10, 20), (200, 20))

# 2,画折线lines(画在哪里,线的颜色,是否闭合(T/F),线的点坐标(可以有很多个),线宽(默认为1))
points = [(10, 300), (100, 160), (180, 260), (300, 100)]
pygame.draw.lines(window, (0, 255, 0), True, points, 3)

# 3,画圆circle(画在哪里,线的颜色,(圆心坐标,半径),线宽(默认为0填充))
pygame.draw.circle(window, (0, 0, 255), (200, 250), 100, 2)

# 4.画矩形rect(画在哪里,线的颜色,矩形范围(x坐标,y坐标,长,宽),线宽(默认为0填充))
pygame.draw.rect(window, (120, 20, 60), (100, 70, 100, 200), 2)

# 5,画椭圆ellipse(画在哪里,线的颜色矩形范围(x坐标,y坐标,长,宽),线宽(默认为0填充))
pygame.draw.ellipse(window, (255, 0, 0), (100, 70, 200, 100), 2)

# 6.画弧形arc(画在哪里,线的颜色矩形范围(x坐标,y坐标,长,宽),起始弧度,终止弧度,线宽)
pygame.draw.arc(window, (0, 0, 0), (100, 70, 100, 200), 0, pi, 5)

pygame.display.flip()
while True:
    # ======一般写游戏帧的刷新=====
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

 

动画原理

import pygame

pygame.init()
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption('显示图形')
window.fill((255, 255, 255))

# 1 显示静态图
y = 100
r = 50
pygame.draw.circle(window, (255, 0, 0), (100, 100), 50)

pygame.display.flip()
# 游戏循环
# 统计循环变量
num = 1
while True:
    # 1,移动动画
    num += 1
    # if num % 1000 == 0:
    #     pygame.draw.circle(window, (255, 255, 255), (100, y), 50)
    #     y = y + 1
    #     pygame.draw.circle(window, (255, 0, 0), (100, y), 50)
    #     pygame.display.update()
    # 2,缩放动画
    if num % 10000 == 0:
        r += 1
        pygame.draw.circle(window, (255, 0, 0), (100, 100), r)
        pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

 

动画的应用

import pygame

h = 800
w = 600
pygame.init()
window = pygame.display.set_mode((h, w))
pygame.display.set_caption('动画应用')
window.fill((255, 255, 255))
# 静态球
x, y = 200, 30
r = 30
y_speed = 1
pygame.draw.circle(window, (0, 255, 0), (x, y), r)
pygame.display.flip()
num = 0
while True:
    # ======一般写游戏帧的刷新=====
    num += 1
    # 小球反弹
    if num % 5000 == 0:
        pygame.draw.circle(window, (255, 255, 255), (x, y), r)
        y += y_speed
        if y >= (h - 8 * r):
            y_speed = y_speed * -1
        if y <= r:
            y_speed = y_speed * -1
        pygame.draw.circle(window, (0, 255, 0), (x, y), r)
        pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

 

事件检测

import pygame
from random import randint

h = 800
w = 600
pygame.init()
window = pygame.display.set_mode((h, w))
pygame.display.set_caption('鼠标事件')
window.fill((255, 255, 255))
pygame.display.flip()

font = pygame.font.Font('aa.ttf', 30)
tx = 0
count = 0  # 统计事件发生的速度
while True:
    # 检测事件
    for event in pygame.event.get():
        # for循环中的代码只有事件发生后才会运行
        count += 1
        print(count)
        # event的type属性是用来区分不同类型的事件
        """
        QUIT:点击关闭按钮对应的事件
        1.鼠标事件
            MOUSEBUTTONDOWN:鼠标摁下
            MOUSEBUTTONUP:鼠标弹起
            MOUSEMOTION:鼠标移动 
            
            鼠标位置属性(点在哪)-pos 
            
        2.键盘事件 
             KEYDOWN 键盘摁下
             KEYUP 键盘弹起
             
             键盘按键对应值的属性(哪个键被摁了):key
        """
        if event.type == pygame.QUIT:
            exit()
        # =================鼠标事件=================
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:  # 如果是摁下鼠标左键
                print('左键鼠标摁下', event.pos)
                mx, my = event.pos  # 取出鼠标的x坐标和y坐标
                pygame.draw.circle(window, (255, 255, 0), (mx, my), 50)
                pygame.display.update()
            elif event.button == 3:  # 如果是摁下鼠标右键
                print('右键鼠标摁下', event.pos)
                mx, my = event.pos  # 取出鼠标的x坐标和y坐标
                pygame.draw.circle(window, (255, 255, 255), (mx, my), 50)
                pygame.display.update()
        if event.type == pygame.MOUSEBUTTONUP:
            print('鼠标弹起', event.pos)
        if event.type == pygame.MOUSEMOTION:
            print('鼠标移动', event.pos)
            mx, my = event.pos  # 取出鼠标的x坐标和y坐标
            r = randint(0, 255)
            g = randint(0, 255)
            b = randint(0, 255)
            pygame.draw.circle(window, (r, g, b), (mx, my), 10)
            pygame.display.update()
        # ================键盘事件==================
        if event.type == pygame.KEYDOWN:
            print('键盘摁下', chr(event.key))
            if chr(event.key) == 'f':
                print('闪现')
            text = font.render(chr(event.key), True, (255, 0, 0))
            window.blit(text, (tx, 300))
            tx += 15
            pygame.display.update()
        if event.type == pygame.KEYUP:
            print('键盘弹起', chr(event.key))
            # get_pressed会返回0或1来判断
            # keys = pygame.key.get_pressed()
            # if keys[pygame.K_w]:  # 如果按下方向键下
            #     mario.rect.y += 10
            # if keys[pygame.K_s]:
            #     mario.rect.y -= 10

 

控制按钮

import pygame

h, w = 800, 600
pygame.init()
window = pygame.display.set_mode((h, w))
pygame.display.set_caption('鼠标事件')
window.fill((255, 255, 255))
pygame.display.flip()

font = pygame.font.Font('aa.ttf', 30)

# 1,确定按钮
bx1, by1, bw, bh = 30, 100, 100, 50
pygame.draw.rect(window, (255, 0, 0), (bx1, by1, bw, bh))
text1 = font.render('确定', True, (255, 255, 255))
tw1, th1 = text1.get_size()
tx1 = bx1 + bw / 2 - tw1 / 2
ty1 = by1 + bh / 2 - th1 / 2
window.blit(text1, (tx1, ty1))

# 2,取消按钮
bx2, by2 = 30, 200
pygame.draw.rect(window, (0, 255, 0), (bx2, by2, 100, 50))
text2 = font.render('取消', True, (255, 255, 255))
tw2, th2 = text2.get_size()
tx2 = bx2 + bw / 2 - tw2 / 2
ty2 = by2 + bh / 2 - th2 / 2
window.blit(text2, (tx2, ty2))
pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        # 按钮点击事件
        if event.type == pygame.MOUSEBUTTONDOWN:
            mx, my = event.pos
            # 是否点击了确定按钮
            if bx1 + bw >= mx >= bx1 and by1 + bh >= my >= by1:
                pygame.draw.rect(window, (200, 200, 200), (bx1, by1, bw, bh))
                window.blit(text1, (tx1, ty1))
                pygame.display.update()

            # 是否点击了取消按钮
            elif bx2 + bw >= mx >= bx2 and by2 + bh >= my >= by2:
                pygame.draw.rect(window, (200, 200, 200), (bx2, by2, bw, bh))
                window.blit(text2, (tx2, ty2))
                pygame.display.update()
        if event.type == pygame.MOUSEBUTTONUP:
            pygame.draw.rect(window, (255, 0, 0), (bx1, by1, bw, bh))
            window.blit(text1, (tx1, ty1))
            pygame.display.update()

            pygame.draw.rect(window, (0, 255, 0), (bx2, by2, bw, bh))
            window.blit(text2, (tx2, ty2))
            pygame.display.update()

一个基于pygame制作的游戏截图(非自制)

pyspark怎么退出 pygame退出_ci