您尚未登录。

楼主 #1 2020-01-19 19:02:34

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

试一试python调用cairo 2d库, 看看配合 pygame 能不能做游戏

python3 安装 pycairo:

python3 -m pip install pycairo

演示程序: https://pycairo.readthedocs.io/en/latest/

import cairo

with cairo.SVGSurface("example.svg", 200, 200) as surface:
    context = cairo.Context(surface)
    x, y, x1, y1 = 0.1, 0.5, 0.4, 0.9
    x2, y2, x3, y3 = 0.6, 0.1, 0.9, 0.5
    context.scale(200, 200)
    context.set_line_width(0.04)
    context.move_to(x, y)
    context.curve_to(x1, y1, x2, y2, x3, y3)
    context.stroke()
    context.set_source_rgba(1, 0.2, 0.2, 0.6)
    context.set_line_width(0.02)
    context.move_to(x, y)
    context.line_to(x1, y1)
    context.move_to(x2, y2)
    context.line_to(x3, y3)
    context.stroke()

example.svg

离线

楼主 #2 2020-01-19 22:00:20

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Re: 试一试python调用cairo 2d库, 看看配合 pygame 能不能做游戏

https://github.com/pygobject/pycairo/tree/master/examples

https://raw.githubusercontent.com/pygobject/pycairo/master/examples/pygame-demo.py

#!/usr/bin/env python
"""demonstrate pycairo and pygame"""

from __future__ import print_function

import math
import sys

import cairo
import pygame


def draw(surface):
    x, y, radius = (250, 250, 200)
    ctx = cairo.Context(surface)
    ctx.set_line_width(15)
    ctx.arc(x, y, radius, 0, 2.0 * math.pi)
    ctx.set_source_rgb(0.8, 0.8, 0.8)
    ctx.fill_preserve()
    ctx.set_source_rgb(1, 1, 1)
    ctx.stroke()


def input(events):
    for event in events:
        if event.type == pygame.QUIT:
            sys.exit(0)
        else:
            print(event)


def main():
    width, height = 512, 512
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)

    pygame.init()
    pygame.display.set_mode((width, height))
    screen = pygame.display.get_surface()

    draw(surface)

    # Create PyGame surface from Cairo Surface
    buf = surface.get_data()
    image = pygame.image.frombuffer(buf, (width, height), "ARGB")
    # Tranfer to Screen
    screen.blit(image, (0, 0))
    pygame.display.flip()

    while True:
        input(pygame.event.get())


if __name__ == "__main__":
    main()

离线

楼主 #3 2020-01-25 22:54:09

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Re: 试一试python调用cairo 2d库, 看看配合 pygame 能不能做游戏

import pygame
import sys

pygame.init()

screen = pygame.display.set_mode((800, 600))

ball = pygame.image.load("ball.png");
screen.fill((255, 255, 255))

ball_x = 0
ball_y = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if(event.key == pygame.K_LEFT):
                ball_x -= 10
            elif (event.key == pygame.K_RIGHT):
                ball_x += 10
            elif(event.key == pygame.K_DOWN):
                ball_y += 10
            elif (event.key == pygame.K_UP):
                ball_y -= 10

    screen.fill((255, 255, 255))

    screen.blit(ball, (ball_x, ball_y))

    pygame.display.update();

大年初一无聊, 继续学习pygame , 导入小球 ball.png 文件, 显示到窗口, 按上/下/左/右键可以移动.

离线

楼主 #4 2020-01-25 23:21:29

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Re: 试一试python调用cairo 2d库, 看看配合 pygame 能不能做游戏

上面的 上下左右键只能触发一次, 改改可以长按了, 有点游戏的感觉了:

import pygame
import sys

pygame.init()

screen = pygame.display.set_mode((800, 600))

ball = pygame.image.load("ball.png");
screen.fill((255, 255, 255))

ball_x = 0
ball_y = 0

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

    if pygame.key.get_pressed()[pygame.K_LEFT]:
        ball_x -= 1
    elif pygame.key.get_pressed()[pygame.K_RIGHT]:
        ball_x += 1
    elif pygame.key.get_pressed()[pygame.K_DOWN]:
        ball_y += 1
    elif pygame.key.get_pressed()[pygame.K_UP]:
        ball_y -= 1

    if ball_x < 0:
        ball_x = 0

    if ball_x > 800:
        ball_x = 800

    if ball_y < 0:
        ball_y = 0

    if ball_y > 600:
        ball_y = 600
        
    screen.fill((255, 255, 255))

    screen.blit(ball, (ball_x, ball_y))

    pygame.display.update();

离线

楼主 #5 2020-01-26 10:10:04

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Re: 试一试python调用cairo 2d库, 看看配合 pygame 能不能做游戏

import pygame
import sys

pygame.init()

screen = pygame.display.set_mode((800, 600))

ball = pygame.image.load("football_new.png")
ball_width  = ball.get_width()
ball_height = ball.get_height()

screen.fill((255, 255, 255))

ball_x = 0
ball_y = 0

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

    if pygame.key.get_pressed()[pygame.K_LEFT]:
        ball_x -= 1
    elif pygame.key.get_pressed()[pygame.K_RIGHT]:
        ball_x += 1
    elif pygame.key.get_pressed()[pygame.K_DOWN]:
        ball_y += 1
    elif pygame.key.get_pressed()[pygame.K_UP]:
        ball_y -= 1

    if ball_x < 0:
        ball_x = 0

    if ball_x > 800 - ball_width:
        ball_x = 800 - ball_width

    if ball_y < 0:
        ball_y = 0

    if ball_y > 600 - ball_height:
        ball_y = 600 - ball_height

    screen.fill((120, 120, 120))

    screen.blit(ball, (ball_x, ball_y))

    pygame.display.update();

把小球的高度和宽度考虑进去了, 现在不会消失在边框附近了.

离线

楼主 #6 2020-01-26 10:24:25

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Re: 试一试python调用cairo 2d库, 看看配合 pygame 能不能做游戏

import pygame
import sys

pygame.init()

screen = pygame.display.set_mode((800, 600))

ball = pygame.image.load("football_new.png")
ball_width  = ball.get_width()
ball_height = ball.get_height()

screen.fill((255, 255, 255))

ball_x = 0
ball_y = 0

#方向 0 向上, 1 向下
direction = 1
响应次数 = 0
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    if pygame.key.get_pressed()[pygame.K_LEFT]:
        响应次数 += 1
        if 响应次数 >= 5:
            ball_x -= 1
            响应次数 = 0
    elif pygame.key.get_pressed()[pygame.K_RIGHT]:
        响应次数 += 1
        if 响应次数 >= 5:
            ball_x += 1
            响应次数 = 0

    if direction == 1:
        ball_y += 1
    elif direction == 0:
        ball_y -= 1

    if ball_x < 0:
        ball_x = 0

    if ball_x > 800 - ball_width:
        ball_x = 800 - ball_width

    if ball_y < 0:
        ball_y = 0
        direction = 1

    #到最下面
    if ball_y > 600 - ball_height:
        ball_y = 600 - ball_height
        direction = 0

    screen.fill((120, 120, 120))

    screen.blit(ball, (ball_x, ball_y))

    pygame.display.update();

球球可以自动上下跳动.

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn