来自新手小白的初识Pygame---Pie游戏

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/zyhoney1228/article/details/102622521

初识Pygame:Pie游戏

(。・∀・)ノ゙嗨,本文献给第一次学习python且想自己写游戏的小白们,本文我将会带领你们感受开发的乐趣,这个游戏虽然很简单,但是将由这个游戏作为一个契机,带你感受创造的喜悦,我们只是初次认识Pygame,学习绘制图像和文本的基础知识,并且编写一些代码。

了解Pie游戏

本文的示例是款叫作 Pie游戏。Pie 游戏使用Pygame来绘制填充的饼块。要在Pie游戏中使用Pygame绘制一个饼块, 用户按下与该饼块对应的数字键。然后,使用Pygame的绘制函数来绘制饼块。当按下针对所有饼块的按键而没有犯错的时候,玩家就获胜了。
如果还没有下载pygame的详情可见hello689的博客内容

使用Pygame

使用Pygame的第一步,是将Pygame库导入到Pygame程序中,以便可以使用它。

import pygame

下一个步骤是,导入Pygame中所有的变量,已准备好可以在我们的代码中访问他们。

from pygame.locals import *

现在,我们可以初始化Pygame了:

pygame.init()

初始化了Pygame,我们就可以访问这个库的所有资源了,下一步是获取对显示系统的访问,并且创建一个窗口。分辨率由你决定,但是,注意宽度和高度的参数要放在圆括号中(600,500)对变成了带有x,y属性的一个点。

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

打印文本

pygame支持使用pygame.font将文本输出到图形窗口。要绘制文本,我们必须先创建一个字体对象:

myfont = pygame.font.Font(None,60)

可以向Pygamne font.Font构造函数提供一个TrueType字体,诸如“Arial",但是,使用None (不带引号)将会导致使用默认的Pygame字体。字体大小60已经很大了,但是,这只是一个简单的示例。 现在,使用Pygame绘制文本并不是一个轻量型的进程,而是一个重量型的进程。这意味着,文本并不是快速地绘制到屏幕上,而是渲染到一个平面,然后再将其绘制到屏幕上。由于这是一个耗费时间的过程,建议首先在内存中创建文本平面(或图像),然后再将文本当作一个图像来绘制。当我们]必须实时地绘制文本的时候,直接绘制是没问题的;但是,如果文本是无法修改的,最好先把文本提前渲染到一个图像之上。

white = 255,255,255
blue = 0,0,255
textImage = myfont.render("hello pygame",True,white)

textImage对象是可以使用screen.blit()绘制的平面,我们]的高难度的绘制函数,将会在所有的游戏和示例中广泛地使用。第一个参数显然是文本消息,第二个参数是抗锯齿字体(为了提高质量)的一个标志,第三个参数是颜色(一个RGB值)。
要绘制文本,通常的过程是清除屏幕,进行绘制,然后刷新显示。让我们看看所有这三行代码:

screen.fill(blue)
screen.blit(textImage,(100,100))
pygame.display.update()

循环

我们所看到的简化的示例有两个问题。首先,它只是运行次然后就停止了。其次没有办法获取任何用户输入(即便它不会立即退出)。因此,让我们看看如何修正这两个问题。首先,我们需要一个循环。在Python中这通过关键字while 来实现。While 语将执行冒号后面的代码,直到条件为假。只要while条件为真,它将持续运行:

while True:

接下来,我们创建一个事件处理程序。在早期阶段,我们期望窗口发生的事情是,它能够等待用户关闭它。关闭事件可能是点击窗口右上角的“X",或者只是按下任何的键。注意,while循环中的代码是缩进的。在此之后缩进的任何代码,都将包含在这个while循环。

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

绘制矩形

要绘制矩形,通过多个参数来调用pygame.draw.rect0函数。这个程序所显示的窗口如图所示。这个示例比上个示例要高级些。 这个示例移动矩形,而不只是在屏幕中间绘制个矩形。 其工作的方法是, 在while 循环之外记录矩形的位置 (使用pos_x和pos_ y),并且创建一对速度变量(vel_x和vel_y)。在while循环之中,我们可以使用该速度来更新位置,然后,通过此逻辑来将矩形保持在屏幕上。其工作的方式是,任何时候,当矩形到达屏幕的边缘的时候,速度变量都取反。

import pygame,sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Rectangles")
pos_x=300
pos_y=250
vel_x=2
vel_y=1
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()

    screen.fill((0,0,200))

    #move the rectangle
    pos_x += vel_x
    pos_y += vel_y
    
    #keep rectangle on the screen
    if pos_x > 500 or pos_x < 0:
        vel_x = -vel_x
    if pos_y > 400 or pos_y < 0:
        vel_y = -vel_y
        
    #draw the rectangle
    color = 255,255,0
    width = 0 #solid fill
    pos = pos_x,pos_y,100,100
    pygame.draw.rect(screen,color,pos,width)
    
    pygame.display.update()

绘制矩形示例

绘制线条

我们可以使用pygame.draw.line()函数来绘制直线。绘制线条比绘制其他的形状更为复杂,这是因为必须提供线条的起点和终点。我们可以用任何的颜色以及想要的线条宽度来绘制线条。

import pygame,sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Lines")

while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
            
    screen.fill((0,80,0))
    
    #draw the line
    color = 100,255,200
    width = 8
    pygame.draw.line(screen,color,(100,100),(500,400),width)
    
    pygame.display.update()

绘制线条示例

绘制弧形

弧形是圆的一.部分,可以使用Pygame.draw.arc()函数来绘制它。这是另外一个复杂的形状,它需要额外的参数,我们必须提供个矩形来表示弧形的边界,首先是矩形左上角的位置,然后是其宽度和高度,弧形就将绘制于其中。接下来,我们必须提供开始角度和结束角度。通常,我们倾向于以度为单位来考量角度,但是几何三角学中用弧度为单位,而这也是我们所必须使用的圆的度量方法。要将角度转换弧度,使用math.radians()函数,用角度作为其参数。由于需要使用数学库,我们必须在程序的顶部导入math库。

import math
import pygame,sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Arcs")
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
            
    screen.fill((0,0,200))
    #draw the arc
    color = 255,0,255
    position = 200,150,200,200
    start_angle = math.radians(0)
    end_angle = math.radians(180)
    width = 8
    pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
    pygame.display.update()

绘制弧形示例

Pie游戏完整代码

Pie游戏是个非常简单的游戏, 没有什么难度,但是,它确实有个基本的游戏逻辑,并且当玩家获胜的时候有一个小小的奖品。游戏逻辑只是牵涉以任意顺序技下按键1,2, 3和4。随着按下每个数字,就会绘制对应的饼块。当所有4个饼块完成之后,改变颜色。
当玩家完成了整个饼块,颜色改变为亮绿色,并且数字和饼块都以亮绿色绘制以显示玩家获胜。这可能是一个简单的游戏, 但是,它展示了很多重要的Pygame概念,我们必须学习这些概念才能熟悉这个库。这个游戏还展示了Python代码的基本逻辑,不管你是否相信,这是基于状态编程的非常重要的方面。你看,如果玩家没有按正确的按键( 1、2、3和4), 4个饼块不会自动绘制。相反,当按下一个键的时候,设置一个状态标志,并且,该标志随后用来作为绘制饼块的根据。这是非常重要的概念,因为它展示了如何间接地处理事件和进行用户交互。

import math
import pygame,sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("The Pie Game - Press 1,2,3,4")
myfont = pygame.font.Font(None,60)

color = 200,80,60
width = 4
x = 300
y = 250
radius = 200
position = x-radius,y-radius,radius*2,radius*2

piece1 = False
piece2 = False
piece3 = False
piece4 = False

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == KEYUP:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
            elif event.key == pygame.K_1:
                piece1=True
            elif event.key == pygame.K_2:
                piece2=True
            elif event.key == pygame.K_3:
                piece3=True
            elif event.key == pygame.K_4:
                piece4=True

    #clear the screen
    screen.fill((0,0,200))

    #draw the four numbers
    textImg1 = myfont.render("1",True,color)
    screen.blit(textImg1,(x+radius/2-20,y-radius/2))
    textImg2 = myfont.render("2",True,color)
    screen.blit(textImg2,(x-radius/2,y-radius/2))
    textImg3 = myfont.render("3",True,color)
    screen.blit(textImg3,(x-radius/2,y+radius/2-20))
    textImg4 = myfont.render("4",True,color)
    screen.blit(textImg4,(x+radius/2-20,y+radius/2-20))

    #should the pieces be drawn?
    if piece1:
        start_angle = math.radians(0)
        end_angle = math.radians(90)
        pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
        pygame.draw.line(screen,color,(x,y),(x,y-radius),width)
        pygame.draw.line(screen,color,(x,y),(x+radius,y),width)
    if piece2:
        start_angle = math.radians(90)
        end_angle = math.radians(180)
        pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
        pygame.draw.line(screen,color,(x,y),(x,y-radius),width)
        pygame.draw.line(screen,color,(x,y),(x-radius,y),width)
    if piece3:
        start_angle = math.radians(180)
        end_angle = math.radians(270)
        pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
        pygame.draw.line(screen,color,(x,y),(x-radius,y),width)
        pygame.draw.line(screen,color,(x,y),(x,y+radius),width)
    if piece4:
        start_angle = math.radians(270)
        end_angle = math.radians(360)
        pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
        pygame.draw.line(screen,color,(x,y),(x,y+radius),width)
        pygame.draw.line(screen,color,(x,y),(x+radius,y),width)

    #is the pie finished?
    if piece1 and piece2 and piece3 and piece4:
        color = 0,255,0

    pygame.display.update()

绘制了两块的Pie游戏
全部绘制完成的Pie游戏
感谢大家的观看,以后还会继续更新,希望大家多多关注哟
文章参考资料:《pygame游戏编程入门》

猜你喜欢

转载自blog.csdn.net/zyhoney1228/article/details/102622521
今日推荐