2d游戏开发学习笔记(3)——基于pygame的小游戏开发

耗尽温柔 提交于 2020-03-15 20:28:26

-一.pygame基础知识

pygame有大量可以被独立使用的模块。

pygam.display-显示模块

pygame.keyboard-键盘模块

pygame.mouse-鼠标模块


二.pygame基础测试

1.首先安装pygame,打开cmd使用pip安装pygame:

pip install pygame

2.在pycharm中导入pygame模块

3.设置窗口大小和窗口名称

参考代码如下:

import pygame
from pygame.locals import *
import sys
def hello_world():
      pygame.init()
      pygame.display.set_mode((640,480))
      pygame.display.set_caption("hello world")
      while True:
          for event in pygame.event.get():
              if event.type==QUIT:
                  pygame.quit()
                  sys.exit()
              pygame.display.update()
if __name__=="__main__":
      hello_world()

运行效果如图:


精灵的使用

介绍:

Pygame 提供了两个类

pygame.sprite.Sprite —— 存储 图像数据 image 和 位置 rect 的 对象,pygame.sprite.Group

精灵可以认为成是一个个小图片,一种可以在屏幕上移动的图形对象,并且可以与其他图形对象交互。精灵图像可以是使用pygame绘制函数绘制的图像,也可以是原来就有的图像文件。

pygame.sprite.Sprite就是Pygame里面用来实现精灵的一个类,使用时,并不需要对它实例化,只需要继承他,然后按需写出自己的类就好了,因此非常简单实用。

#定义一个游戏精灵基类

class Gamesprite(pygame.sprite.Sprite) 

(1)坦克精灵

代码如下:

import pygame,sys
pygame.init()
class Tank(pygame.sprite.Sprite):
    def __init__(self,filename,initial_position):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load(filename)
        self.rect=self.image.get_rect()
        self.rect.bottomright=initial_position
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='tankU.bmp'
b=Tank(fi,[150,100])
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
    screen.blit(b.image,b.rect)
    pygame.display.update()


运行测试如下:


(2)精灵类测试

代码如下:

import pygame
from pygame.locals import *
class MySprite(pygame.sprite.Sprite):
    def __init__(self,target):
        pygame.sprite.Sprite.__init__(self)
        self.sprite_surface=target
        self.image=None
        self.master_image=None
        self.rect=None
        self.topleft=0,0
        self.frame=0
        self.old_fram=-1
        self.fram_width=1
        self.fram_height=1
        self.first_fram=0
        self.last_fram=0
        self.columns=1
        self.last_time=0
    def load(self,filename,width,height,columns):
        self.master_image=pygame.image.load(filename).convert_alpha()
        self.fram_width=width
        self.fram_height=height
        self.rect=0,0,width,height
        self.columns=columns
        rect=self.master_image.get_rect()
        self.last_fram=(rect.width//width)*(rect.height//height)-1
    def update(self,current_time,rate=60):
        if current_time>self.last_time+rate:
            self.frame+=1
        if self.frame>self.last_fram:
            self.frame=self.first_fram
            self.last_time=current_time
        if self.frame!=self.old_fram:
            frame_x=(self.frame%self.columns)*self.fram_width
            frame_y=(self.frame//self.columns)*self.fram_height
            rect=(frame_x,frame_y,self.fram_width,self.fram_height)
            self.image=self.master_image.subsurface(rect)
            self.old_fram=self.frame

pygame.init()
screen=pygame.display.set_mode((800,600),0,32)
pygame.display.set_caption("精灵类测试")
font=pygame.font.Font(None,18)
framerate=pygame.time.Clock()
cat=MySprite(screen)
cat.load("image/sprite2.png",92,95,4)
group=pygame.sprite.Group()
group.add(cat)
while True:
    framerate.tick(10)
    ticks=pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            exit()
        key=pygame.key.get_pressed()
        if key[pygame.K_ESCAPE]:
           exit()
    screen.fill((0,0,255))

    cat.update(ticks)
    screen.blit(cat.image,cat.rect)
    pygame.display.update()


运行测试如下:


(3)坦克大战小游戏

1.创建显示层,导入背景坦克

2.设置键盘监听,实现坦克的自由移动

参考代码如下

import os, sys, pygame
from pygame.locals import *


def control_tank(event):
    speed = [x, y] = [0, 0]
    speed_offset = 1
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            speed[0] -= speed_offset
        if event.key == pygame.K_RIGHT:
            speed[0] = speed_offset
        if event.key == pygame.K_UP:
            speed[1] -= speed_offset
        if event.key == pygame.K_DOWN:
            speed[1] = speed_offset
    if event.type == pygame.KEYUP:
        if event.type in [pygame.K_UP, pygame.K_DOWN, pygame.K_RIGHT, pygame.K_LEFT]:
            speed = [0, 0]
    return speed


def play_tank():
    pygame.init()
    window_size = Rect(0, 0, 640, 480)
    speed = [1, 1]
    color_white = (255, 255, 255)
    screen = pygame.display.set_mode(window_size.size)
    pygame.display.set_caption("坦克大战")
    tank_image = pygame.image.load('image/tankU.bmp')
    back_image = pygame.image.load('image/back_image.jpg')
    tank_rect = tank_image.get_rect()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            cur_speed = control_tank(event)
            tank_rect = tank_rect.move(cur_speed).clamp(window_size)
            screen.blit(back_image, (0, 0))
            screen.blit(tank_image, tank_rect)
            pygame.display.update()


if __name__ == "__main__":
    play_tank()

运行测试如下:


三.pygame制作小游戏

(1)贪吃蛇小游戏

1.定义变量和函数

2.创建显示层

3.初始化蛇和树莓的位置以及方向

4.编写蛇吃到树莓,树莓消失的并随机生成的事件(1为吃到,0为没有吃到 )

5.编写蛇死亡事件

参考代码如下:

import pygame,sys,time,random
from pygame.locals import *
# 定义颜色变量
redColour = pygame.Color(255,0,0)
blackColour = pygame.Color(0,0,0)
whiteColour = pygame.Color(255,255,255)
greyColour = pygame.Color(150,150,150)

# 定义gameOver函数
def gameOver(playSurface):
    gameOverFont = pygame.font.Font('arial.ttf',72)
    gameOverSurf = gameOverFont.render('Game Over', True, greyColour)
    gameOverRect = gameOverSurf.get_rect()
    gameOverRect.midtop = (320, 10)
    playSurface.blit(gameOverSurf, gameOverRect)
    pygame.display.flip()
    time.sleep(5)
    pygame.quit()
    sys.exit()

# 定义main函数
def main():
    # 初始化pygame
    pygame.init()
    fpsClock = pygame.time.Clock()
    # 创建pygame显示层
    playSurface = pygame.display.set_mode((640,480))
    pygame.display.set_caption('贪吃蛇')

    # 初始化变量
    snakePosition = [100,100]
    snakeSegments = [[100,100],[80,100],[60,100]]
    raspberryPosition = [300,300]
    raspberrySpawned = 1
    direction = 'right'
    changeDirection = direction
    while True:
        # 检测例如按键等pygame事件
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                # 判断键盘事件
                if event.key == K_RIGHT or event.key == ord('d'):
                    changeDirection = 'right'
                if event.key == K_LEFT or event.key == ord('a'):
                    changeDirection = 'left'
                if event.key == K_UP or event.key == ord('w'):
                    changeDirection = 'up'
                if event.key == K_DOWN or event.key == ord('s'):
                    changeDirection = 'down'
                if event.key == K_ESCAPE:
                    pygame.event.post(pygame.event.Event(QUIT))
        # 判断是否输入了反方向
        if changeDirection == 'right' and not direction == 'left':
            direction = changeDirection
        if changeDirection == 'left' and not direction == 'right':
            direction = changeDirection
        if changeDirection == 'up' and not direction == 'down':
            direction = changeDirection
        if changeDirection == 'down' and not direction == 'up':
            direction = changeDirection
        # 根据方向移动蛇头的坐标
        if direction == 'right':
            snakePosition[0] += 20
        if direction == 'left':
            snakePosition[0] -= 20
        if direction == 'up':
            snakePosition[1] -= 20
        if direction == 'down':
            snakePosition[1] += 20
        # 增加蛇的长度
        snakeSegments.insert(0,list(snakePosition))
        # 判断是否吃掉了树莓
        if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]:
            raspberrySpawned = 0
        else:
            snakeSegments.pop()
        # 如果吃掉树莓,则重新生成树莓
        if raspberrySpawned == 0:
            x = random.randrange(1,32)
            y = random.randrange(1,24)
            raspberryPosition = [int(x*20),int(y*20)]
            raspberrySpawned = 1
        # 绘制pygame显示层
        playSurface.fill(blackColour)
        for position in snakeSegments:
            pygame.draw.rect(playSurface,whiteColour,Rect(position[0],position[1],20,20))
            pygame.draw.rect(playSurface,redColour,Rect(raspberryPosition[0], raspberryPosition[1],20,20))

        # 刷新pygame显示层
        pygame.display.flip()
        # 判断是否死亡
        if snakePosition[0] > 620 or snakePosition[0] < 0:
            gameOver(playSurface)
        if snakePosition[1] > 460 or snakePosition[1] < 0:
            for snakeBody in snakeSegments[1:]:
                if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]:
                    gameOver(playSurface)
        # 控制游戏速度
        fpsClock.tick(5)

if __name__ == "__main__":
    main()



运行测试如下:


(2)打飞机游戏

1.导入Pygame库及需要的库;初始化游戏,并根据设置好的大小生成游戏窗口

2.载入背景图片,飞机图片,然后 生成飞机的surbsurface并初始化飞机开始的位置

3.设置监听键盘事件,让飞机动起来

  1. 显示子弹

5.随机生成敌机

6.检测碰撞

7.判断游戏结束

8.计分

代码参考如下:

import pygame
from sys import exit
from pygame.locals import *
import random
SCREEN_WIDTH=480
SCREEN_HEIGHT = 800
TYPE_SMALL=1
TYPE_MIDDLE=2
TYPE_BIG=3

class Bullet(pygame.sprite.Sprite):
    def __init__(self, bullet_img, init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = bullet_img
        self.rect = self.image.get_rect()
        self.rect.midbottom = init_pos
        self.speed = 10

    def move(self):
       self.rect.top -= self.speed

class Player(pygame.sprite.Sprite):
     def __init__(self, plane_img, player_rect, init_pos):
         pygame.sprite.Sprite.__init__(self)
         self.image = []                                 # 用来存储玩家飞机图片的列表
         for i in range(len(player_rect)):
             self.image.append(plane_img.subsurface(player_rect[i]).convert_alpha())
         self.rect = player_rect[0]                      # 初始化图片所在的矩形
         self.rect.topleft = init_pos                    # 初始化矩形的左上角坐标
         self.speed = 8                                  # 初始化玩家飞机速度,这里是一个确定的值
         self.bullets = pygame.sprite.Group()            # 玩家飞机所发射的子弹的集合
         self.is_hit = False                             # 玩家是否被击中

     # 发射子弹
     def shoot(self, bullet_img):
         bullet = Bullet(bullet_img, self.rect.midtop)
         self.bullets.add(bullet)

     # 向上移动,需要判断边界
     def moveUp(self):
        if self.rect.top <= 0:
            self.rect.top = 0
        else:
             self.rect.top -= self.speed

     # 向下移动,需要判断边界
     def moveDown(self):
         if self.rect.top >= SCREEN_HEIGHT - self.rect.height:
             self.rect.top = SCREEN_HEIGHT - self.rect.height
         else:
             self.rect.top += self.speed

     # 向左移动,需要判断边界
     def moveLeft(self):
         if self.rect.left <= 0:
             self.rect.left = 0
         else:
             self.rect.left -= self.speed

     # 向右移动,需要判断边界
     def moveRight(self):
         if self.rect.left >= SCREEN_WIDTH - self.rect.width:
             self.rect.left = SCREEN_WIDTH - self.rect.width
         else:
             self.rect.left += self.speed

class Enemy(pygame.sprite.Sprite):
     def __init__(self, enemy_img, enemy_down_imgs, init_pos):
         pygame.sprite.Sprite.__init__(self)
         self.image = enemy_img
         self.rect = self.image.get_rect()
         self.rect.topleft = init_pos
         self.down_imgs = enemy_down_imgs
         self.speed = 2


     def move(self):
         self.rect.top += self.speed

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Python打飞机大战')


background = pygame.image.load('resources/image/background.png').convert()

game_over = pygame.image.load('resources/image/gameover.png')


plane_img = pygame.image.load('resources/image/shoot.png')

player_rect = []
player_rect.append(pygame.Rect(0, 99, 102, 126))        # 玩家飞机图片
player_rect.append(pygame.Rect(165, 234, 102, 126))     # 玩家爆炸图片

player_pos = [200, 600]
player = Player(plane_img, player_rect, player_pos)


bullet_rect = pygame.Rect(1004, 987, 9, 21)
bullet_img = plane_img.subsurface(bullet_rect)


enemy1_rect = pygame.Rect(534, 612, 57, 43)
enemy1_img = plane_img.subsurface(enemy1_rect)
enemy1_down_imgs = plane_img.subsurface(pygame.Rect(267, 347, 57, 43))


enemies1 = pygame.sprite.Group()


enemies_down = pygame.sprite.Group()


shoot_frequency = 0
enemy_frequency = 0


score = 0

clock = pygame.time.Clock()


running = True


while running:

    clock.tick(60)
    if not player.is_hit:
        if shoot_frequency % 15 == 0:
            player.shoot(bullet_img)
        shoot_frequency += 1
        if shoot_frequency >= 15:
            shoot_frequency = 0


    if enemy_frequency % 50 == 0:
        enemy1_pos = [random.randint(0, SCREEN_WIDTH - enemy1_rect.width), 0]
        enemy1 = Enemy(enemy1_img, enemy1_down_imgs, enemy1_pos)
        enemies1.add(enemy1)
    enemy_frequency += 1
    if enemy_frequency >= 100:
        enemy_frequency = 0

    for bullet in player.bullets:
        # 以固定速度移动子弹
        bullet.move()
        # 移动出屏幕后删除子弹
        if bullet.rect.bottom < 0:
            player.bullets.remove(bullet)

    for enemy in enemies1:
        #2. 移动敌机
        enemy.move()
        #3. 敌机与玩家飞机碰撞效果处理
        if pygame.sprite.collide_circle(enemy, player):
            enemies_down.add(enemy)
            enemies1.remove(enemy)
            player.is_hit = True
            break
        #4. 移动出屏幕后删除敌人
        if enemy.rect.top < 0:
            enemies1.remove(enemy)

    #敌机被子弹击中效果处理
    #将被击中的敌机对象添加到击毁敌机 Group 中
    enemies1_down = pygame.sprite.groupcollide(enemies1, player.bullets, 1, 1)
    for enemy_down in enemies1_down:
        enemies_down.add(enemy_down)

    # 绘制背景
    screen.fill(0)
    screen.blit(background, (0, 0))

    # 绘制玩家飞机
    if not player.is_hit:
        screen.blit(player.image[0], player.rect) #将正常飞机画出来
    else:
        # 玩家飞机被击中后的效果处理
        screen.blit(player.image[1], player.rect) #将爆炸的飞机画出来
        running = False

    # 敌机被子弹击中效果显示
    for enemy_down in enemies_down:
        enemies_down.remove(enemy_down)
        score += 1
        screen.blit(enemy_down.down_imgs, enemy_down.rect) #将爆炸的敌机画出来


    # 显示子弹
    player.bullets.draw(screen)
    # 显示敌机
    enemies1.draw(screen)
    # 绘制得分
    score_font = pygame.font.Font(None, 36)
    score_text = score_font.render('score: '+str(score), True, (128, 128, 128))
    text_rect = score_text.get_rect()
    text_rect.topleft = [10, 10]
    screen.blit(score_text, text_rect)

    pygame.display.update()

    # 处理游戏退出
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    # 获取键盘事件(上下左右按键)
    key_pressed = pygame.key.get_pressed()

    # 处理键盘事件(移动飞机的位置)
    if key_pressed[K_w] or key_pressed[K_UP]:
        player.moveUp()
    if key_pressed[K_s] or key_pressed[K_DOWN]:
        player.moveDown()
    if key_pressed[K_a] or key_pressed[K_LEFT]:
        player.moveLeft()
    if key_pressed[K_d] or key_pressed[K_RIGHT]:
        player.moveRight()

# 游戏 Game Over 后显示最终得分
font = pygame.font.Font(None, 64)
text = font.render('Final Score: '+ str(score), True, (255, 0, 0))
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.centery = screen.get_rect().centery + 24
screen.blit(game_over, (0, 0))
screen.blit(text, text_rect)

# 显示得分并处理游戏退出
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    pygame.display.update()

运行测试如下:


(3)2048小游戏

介绍: 2048的游戏规则很简单,每次可以选择上下左右其中一个方向去滑动,每滑动一次,所有的数字方块都会往滑动的方向靠拢外,系统也会在空白的地方乱数出现一个数字方块,相同数字的方块在靠拢、相撞时会相加。系统给予的数字方块不是2就是4,玩家要想办法在这小小的16格范围中凑出“2048”这个数字方块。

参考代码如下:

import random
import sys
import pygame
from pygame.locals import *

PIXEL = 150
SCORE_PIXEL = 100
SIZE = 4


# 地图的类
class Map:
    def __init__(self, size):
        self.size = size
        self.score = 0
        self.map = [[0 for i in range(size)] for i in range(size)]
        self.add()
        self.add()

    # 新增2或4,有1/4概率产生4
    def add(self):
        while True:
            p = random.randint(0, self.size * self.size - 1)
            if self.map[p // self.size][p % self.size] == 0:
                x = random.randint(0, 3) > 0 and 2 or 4
                self.map[p // self.size][p % self.size] = x
                self.score += x
                break

    # 地图向左靠拢,其他方向的靠拢可以通过适当旋转实现,返回地图是否更新
    def adjust(self):
        changed = False
        for a in self.map:
            b = []
            last = 0
            for v in a:
                if v != 0:
                    if v == last:
                        b.append(b.pop() << 1)
                        last = 0
                    else:
                        b.append(v)
                        last = v
            b += [0] * (self.size - len(b))
            for i in range(self.size):
                if a[i] != b[i]:
                    changed = True
            a[:] = b
        return changed

    # 逆时针旋转地图90度
    def rotate90(self):
        self.map = [[self.map[c][r] for c in range(self.size)] for r in reversed(range(self.size))]

    # 判断游戏结束
    def over(self):
        for r in range(self.size):
            for c in range(self.size):
                if self.map[r][c] == 0:
                    return False
        for r in range(self.size):
            for c in range(self.size - 1):
                if self.map[r][c] == self.map[r][c + 1]:
                    return False
        for r in range(self.size - 1):
            for c in range(self.size):
                if self.map[r][c] == self.map[r + 1][c]:
                    return False
        return True

    def moveUp(self):
        self.rotate90()
        if self.adjust():
            self.add()
        self.rotate90()
        self.rotate90()
        self.rotate90()

    def moveRight(self):
        self.rotate90()
        self.rotate90()
        if self.adjust():
            self.add()
        self.rotate90()
        self.rotate90()

    def moveDown(self):
        self.rotate90()
        self.rotate90()
        self.rotate90()
        if self.adjust():
            self.add()
        self.rotate90()

    def moveLeft(self):
        if self.adjust():
            self.add()


# 更新屏幕
def show(map):
    for i in range(SIZE):
        for j in range(SIZE):
            # 背景颜色块
            screen.blit(map.map[i][j] == 0 and block[(i + j) % 2] or block[2 + (i + j) % 2], (PIXEL * j, PIXEL * i))
            # 数值显示
            if map.map[i][j] != 0:
                map_text = map_font.render(str(map.map[i][j]), True, (106, 90, 205))
                text_rect = map_text.get_rect()
                text_rect.center = (PIXEL * j + PIXEL / 2, PIXEL * i + PIXEL / 2)
                screen.blit(map_text, text_rect)
    # 分数显示
    screen.blit(score_block, (0, PIXEL * SIZE))
    score_text = score_font.render((map.over() and "Game over with score " or "Score: ") + str(map.score), True,
                                   (106, 90, 205))
    score_rect = score_text.get_rect()
    score_rect.center = (PIXEL * SIZE / 2, PIXEL * SIZE + SCORE_PIXEL / 2)
    screen.blit(score_text, score_rect)
    pygame.display.update()


map = Map(SIZE)
pygame.init()
screen = pygame.display.set_mode((PIXEL * SIZE, PIXEL * SIZE + SCORE_PIXEL))
pygame.display.set_caption("2048")
block = [pygame.Surface((PIXEL, PIXEL)) for i in range(4)]
# 设置颜色
block[0].fill((152, 251, 152))
block[1].fill((240, 255, 255))
block[2].fill((0, 255, 127))
block[3].fill((225, 255, 255))
score_block = pygame.Surface((PIXEL * SIZE, SCORE_PIXEL))
score_block.fill((245, 245, 245))
# 设置字体
map_font = pygame.font.Font(None, PIXEL)
score_font = pygame.font.Font(None, SCORE_PIXEL)
clock = pygame.time.Clock()
show(map)

while not map.over():
    # 12为实验参数
    clock.tick(12)
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    # 接收玩家操作
    pressed_keys = pygame.key.get_pressed()
    if pressed_keys[K_w] or pressed_keys[K_UP]:
        map.moveUp()
    elif pressed_keys[K_s] or pressed_keys[K_DOWN]:
        map.moveDown()
    elif pressed_keys[K_a] or pressed_keys[K_LEFT]:
        map.moveLeft()
    elif pressed_keys[K_d] or pressed_keys[K_RIGHT]:
        map.moveRight()
    show(map)

# 游戏结束
pygame.time.delay(3000)


运行测试如下:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!