Python+Pygame编写一个Pong游戏

目录
  • 前言
  • 代码教学
  • 最终代码

前言

这次,我们要用Pygame写一个Pong游戏

先看看效果:

需要的模块:Pygame

在python文件同目录下新建resources文件夹,在文件夹中新建Pong文件夹,文件夹中放入两个音频文件

代码教学

导入需要的模块

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

定义常量

COUNTDOWN=USEREVENT+1
path="resources/Pong/"

定义Class类,初始化函数内的代码:

pygame.init()
self.screen=pygame.display.set_mode((750,800))
pygame.display.set_caption("Pong")

self.mode="welcome"
self.ball=None
self.xspeed=0
self.yspeed=0
self.r=0
self.p1=None
self.p2=None
self.p1y=0
self.p2y=0
self.boardWidth=0
self.boardHeight=0
self.countdown=0
self.p1score=0
self.p2score=0
self.ballr=None
self.min=2
self.max=7
self.win=11
self.matchpoint=0
self.boardSpeed=self.max
self.ding=pygame.mixer.Sound(path+"ding.mp3")
self.bo=pygame.mixer.Sound(path+"bo.mp3")

定义listen函数

    def listen(self):
        for event in pygame.event.get():
            if event.type==QUIT:
                sys.exit()
            elif event.type==KEYDOWN:
                if event.key==K_k and self.mode=="welcome":
                    self.mode="playing"
                    self.ball=[750/2,800/2]
                    self.r=10
                    self.p1y=100
                    self.p2y=100
                    self.boardWidth=10
                    self.boardHeight=100
                    self.countdown=5
                    self.p1score=0
                    self.p2score=0
                    self.ballr=Rect(100,100,1,1)
                    pygame.time.set_timer(COUNTDOWN,1000)
            elif event.type==COUNTDOWN:
                self.countdown-=1
                if self.countdown==0:
                    self.countdown=0
                    pygame.time.set_timer(COUNTDOWN,0)
                    self.xspeed=random.randint(self.min,self.max) if random.randint(0,1)==0 else random.randint(-self.max,-self.min)
                    self.yspeed=random.randint(self.min,self.max) if random.randint(0,1)==0 else random.randint(-self.max,-self.min)
                else:
                    self.ding.play()

定义draw函数,用于屏幕显示,进入游戏时的ui:

        if self.mode=="welcome":
            self.screen.fill((0,0,0))
            ts=[
                "Welcome to Pong",
                "This game needs 2 players",
                "Press K to start"
            ]
            y=100
            for t in ts:
                to=self.print_text("simhei",35,t,(255,255,255))
                self.screen.blit(to,(100,y))
                y+=40

开始游戏后:

        elif self.mode=="playing":
            self.screen.fill((0,0,0))
            self.p1=pygame.draw.rect(self.screen,(255,255,255),(0,self.p1y,self.boardWidth,self.boardHeight))
            self.p2=pygame.draw.rect(self.screen,(255,255,255),(750-self.boardWidth,self.p2y,self.boardWidth,self.boardHeight))
            to=self.print_text("simhei",20,"Press WS to move",(255,255,255))
            to2=self.print_text("simhei",20,"Press ↑↓ to move",(255,255,255))
            tor=to.get_rect()
            tor2=to2.get_rect()
            d=10
            tor.bottomleft=d,800-d
            tor2.bottomright=750-d,800-d
            self.screen.blit(to,tor)
            self.screen.blit(to2,tor2)
            to=self.print_text("simhei",20,"Match Point!",(255,255,255))
            if self.matchpoint==1:
                self.screen.blit(to,(10,10))
            elif self.matchpoint==2:
                tor=to.get_rect()
                tor.topright=750-10,10
                self.screen.blit(to,tor)
            elif self.matchpoint==11:
                pygame.time.set_timer(COUNTDOWN,0)
                to=self.print_text("simhei",20,"Win!",(255,255,255))
                self.screen.blit(to,(10,10))
                to=self.print_text("simhei",20,"Lose!",(255,255,255))
                tor=to.get_rect()
                tor.topright=750-10,10
                self.screen.blit(to,tor)
            elif self.matchpoint==22:
                pygame.time.set_timer(COUNTDOWN,0)
                to=self.print_text("simhei",20,"Lose!",(255,255,255))
                self.screen.blit(to,(10,10))
                to=self.print_text("simhei",20,"Win!",(255,255,255))
                tor=to.get_rect()
                tor.topright=750-10,10
                self.screen.blit(to,tor)
            if not (self.matchpoint==11 or self.matchpoint==22):
                self.move()
                if not self.countdown:
                    pygame.draw.line(self.screen,(255,255,255),(750/2,0),(750/2,800),5)
                else:
                    to=self.print_text("simhei",72,str(self.countdown),(255,255,255))
                    tor=to.get_rect()
                    tor.midtop=750/2,50
                    self.screen.blit(to,tor)
            to=self.print_text("simhei",150,str(self.p1score),(255,255,255))
            to2=self.print_text("simhei",150,str(self.p2score),(255,255,255))
            tor=to.get_rect()
            tor2=to2.get_rect()
            tor.midtop=750/2/2,50
            tor2.midtop=750/2+750/2/2,50
            self.screen.blit(to,tor)
            self.screen.blit(to2,tor2)
            self.ballr=pygame.draw.circle(self.screen,(255,255,255),tuple(self.ball),self.r)

这里,为了可以显示文字,我们自己写一个print_text函数,用于显示文字

    @staticmethod
    def print_text(name,size,text,color):
        font=pygame.font.SysFont(name,size)
        image=font.render(text,True,color)
        return image

定义一个move函数,用于移动小球和两个玩家的“板”

    def move(self):
        if (not self.countdown) and (not (self.ballr.colliderect(self.p1) or self.ballr.colliderect(self.p2))):
            self.ball[0]+=self.xspeed
            self.ball[1]+=self.yspeed
            if self.ball[0]-self.r<=0:
                self.p2score+=1
                self.countdown=3
                self.ballr=Rect(100,100,1,1)
                self.ball=[750/2,800/2]
                pygame.time.set_timer(COUNTDOWN,1000)
            if self.ball[0]+self.r>=750:
                self.p1score+=1
                self.countdown=3
                self.ballr=Rect(100,100,1,1)
                self.ball=[750/2,800/2]
                pygame.time.set_timer(COUNTDOWN,1000)
            if self.ball[1]-self.r<=0 or self.ball[1]+self.r>=800:
                self.yspeed=-self.yspeed
                if self.yspeed<0:
                    self.yspeed=random.randint(-self.max,-self.min)
                else:
                    self.yspeed=random.randint(self.min,self.max)
                self.bo.play()
        elif self.ballr.colliderect(self.p1) or self.ballr.colliderect(self.p2):
            self.xspeed=-self.xspeed
            if self.xspeed<0:
                self.xspeed=random.randint(-self.max,-self.min)
            else:
                self.xspeed=random.randint(self.min,self.max)
            self.bo.play()
            self.ball[0]+=self.xspeed*2
        key=pygame.key.get_pressed()
        if key[K_w]:
            self.p1y-=self.boardSpeed
        if key[K_s]:
            self.p1y+=self.boardSpeed
        if key[K_UP]:
            self.p2y-=self.boardSpeed
        if key[K_DOWN]:
            self.p2y+=self.boardSpeed
        if self.p1y<=0:
            self.p1y=0
        if self.p2y<=0:
            self.p2y=0
        if self.p1y+self.boardHeight>=800:
            self.p1y=800-self.boardHeight
        if self.p2y+self.boardHeight>=800:
            self.p2y=800-self.boardHeight

再定义一个函数,用于检查是否有玩家已经到达赛点

    def checkMatchPoint(self):
        self.matchpoint=0
        if self.p1score==self.win:
            self.matchpoint=11
        if self.p2score==self.win:
            self.matchpoint=22
        if self.p1score+1==self.win:
            self.matchpoint=1
        if self.p2score+1==self.win:
            self.matchpoint=2
        if self.p1score+1==self.win and self.p2score+1==self.win:
            self.win+=1

定义用于进入游戏主循环的函数run

    def run(self):
        clock=pygame.time.Clock()
        while True:
            clock.tick(60)
            self.listen()
            if not (self.matchpoint==11 or self.matchpoint==22):
                self.checkMatchPoint()
            self.draw()
            pygame.display.update()

在类的外面,创建game对象,并进入游戏主循环

game=Game()
game.run()

最终代码

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

COUNTDOWN=USEREVENT+1
path="resources/Pong/"

class Game:
    def __init__(self):
        pygame.init()
        self.screen=pygame.display.set_mode((750,800))
        pygame.display.set_caption("Pong")

        self.mode="welcome"
        self.ball=None
        self.xspeed=0
        self.yspeed=0
        self.r=0
        self.p1=None
        self.p2=None
        self.p1y=0
        self.p2y=0
        self.boardWidth=0
        self.boardHeight=0
        self.countdown=0
        self.p1score=0
        self.p2score=0
        self.ballr=None
        self.min=2
        self.max=7
        self.win=11
        self.matchpoint=0
        self.boardSpeed=self.max
        self.ding=pygame.mixer.Sound(path+"ding.mp3")
        self.bo=pygame.mixer.Sound(path+"bo.mp3")

    def listen(self):
        for event in pygame.event.get():
            if event.type==QUIT:
                sys.exit()
            elif event.type==KEYDOWN:
                if event.key==K_k and self.mode=="welcome":
                    self.mode="playing"
                    self.ball=[750/2,800/2]
                    self.r=10
                    self.p1y=100
                    self.p2y=100
                    self.boardWidth=10
                    self.boardHeight=100
                    self.countdown=5
                    self.p1score=0
                    self.p2score=0
                    self.ballr=Rect(100,100,1,1)
                    pygame.time.set_timer(COUNTDOWN,1000)
            elif event.type==COUNTDOWN:
                self.countdown-=1
                if self.countdown==0:
                    self.countdown=0
                    pygame.time.set_timer(COUNTDOWN,0)
                    self.xspeed=random.randint(self.min,self.max) if random.randint(0,1)==0 else random.randint(-self.max,-self.min)
                    self.yspeed=random.randint(self.min,self.max) if random.randint(0,1)==0 else random.randint(-self.max,-self.min)
                else:
                    self.ding.play()

    def draw(self):
        if self.mode=="welcome":
            self.screen.fill((0,0,0))
            ts=[
                "Welcome to Pong",
                "This game needs 2 players",
                "Press K to start"
            ]
            y=100
            for t in ts:
                to=self.print_text("simhei",35,t,(255,255,255))
                self.screen.blit(to,(100,y))
                y+=40
        elif self.mode=="playing":
            self.screen.fill((0,0,0))
            self.p1=pygame.draw.rect(self.screen,(255,255,255),(0,self.p1y,self.boardWidth,self.boardHeight))
            self.p2=pygame.draw.rect(self.screen,(255,255,255),(750-self.boardWidth,self.p2y,self.boardWidth,self.boardHeight))
            to=self.print_text("simhei",20,"Press WS to move",(255,255,255))
            to2=self.print_text("simhei",20,"Press ↑↓ to move",(255,255,255))
            tor=to.get_rect()
            tor2=to2.get_rect()
            d=10
            tor.bottomleft=d,800-d
            tor2.bottomright=750-d,800-d
            self.screen.blit(to,tor)
            self.screen.blit(to2,tor2)
            to=self.print_text("simhei",20,"Match Point!",(255,255,255))
            if self.matchpoint==1:
                self.screen.blit(to,(10,10))
            elif self.matchpoint==2:
                tor=to.get_rect()
                tor.topright=750-10,10
                self.screen.blit(to,tor)
            elif self.matchpoint==11:
                pygame.time.set_timer(COUNTDOWN,0)
                to=self.print_text("simhei",20,"Win!",(255,255,255))
                self.screen.blit(to,(10,10))
                to=self.print_text("simhei",20,"Lose!",(255,255,255))
                tor=to.get_rect()
                tor.topright=750-10,10
                self.screen.blit(to,tor)
            elif self.matchpoint==22:
                pygame.time.set_timer(COUNTDOWN,0)
                to=self.print_text("simhei",20,"Lose!",(255,255,255))
                self.screen.blit(to,(10,10))
                to=self.print_text("simhei",20,"Win!",(255,255,255))
                tor=to.get_rect()
                tor.topright=750-10,10
                self.screen.blit(to,tor)
            if not (self.matchpoint==11 or self.matchpoint==22):
                self.move()
                if not self.countdown:
                    pygame.draw.line(self.screen,(255,255,255),(750/2,0),(750/2,800),5)
                else:
                    to=self.print_text("simhei",72,str(self.countdown),(255,255,255))
                    tor=to.get_rect()
                    tor.midtop=750/2,50
                    self.screen.blit(to,tor)
            to=self.print_text("simhei",150,str(self.p1score),(255,255,255))
            to2=self.print_text("simhei",150,str(self.p2score),(255,255,255))
            tor=to.get_rect()
            tor2=to2.get_rect()
            tor.midtop=750/2/2,50
            tor2.midtop=750/2+750/2/2,50
            self.screen.blit(to,tor)
            self.screen.blit(to2,tor2)
            self.ballr=pygame.draw.circle(self.screen,(255,255,255),tuple(self.ball),self.r)

    @staticmethod
    def print_text(name,size,text,color):
        font=pygame.font.SysFont(name,size)
        image=font.render(text,True,color)
        return image

    def move(self):
        if (not self.countdown) and (not (self.ballr.colliderect(self.p1) or self.ballr.colliderect(self.p2))):
            self.ball[0]+=self.xspeed
            self.ball[1]+=self.yspeed
            if self.ball[0]-self.r<=0:
                self.p2score+=1
                self.countdown=3
                self.ballr=Rect(100,100,1,1)
                self.ball=[750/2,800/2]
                pygame.time.set_timer(COUNTDOWN,1000)
            if self.ball[0]+self.r>=750:
                self.p1score+=1
                self.countdown=3
                self.ballr=Rect(100,100,1,1)
                self.ball=[750/2,800/2]
                pygame.time.set_timer(COUNTDOWN,1000)
            if self.ball[1]-self.r<=0 or self.ball[1]+self.r>=800:
                self.yspeed=-self.yspeed
                if self.yspeed<0:
                    self.yspeed=random.randint(-self.max,-self.min)
                else:
                    self.yspeed=random.randint(self.min,self.max)
                self.bo.play()
        elif self.ballr.colliderect(self.p1) or self.ballr.colliderect(self.p2):
            self.xspeed=-self.xspeed
            if self.xspeed<0:
                self.xspeed=random.randint(-self.max,-self.min)
            else:
                self.xspeed=random.randint(self.min,self.max)
            self.bo.play()
            self.ball[0]+=self.xspeed*2
        key=pygame.key.get_pressed()
        if key[K_w]:
            self.p1y-=self.boardSpeed
        if key[K_s]:
            self.p1y+=self.boardSpeed
        if key[K_UP]:
            self.p2y-=self.boardSpeed
        if key[K_DOWN]:
            self.p2y+=self.boardSpeed
        if self.p1y<=0:
            self.p1y=0
        if self.p2y<=0:
            self.p2y=0
        if self.p1y+self.boardHeight>=800:
            self.p1y=800-self.boardHeight
        if self.p2y+self.boardHeight>=800:
            self.p2y=800-self.boardHeight

    def checkMatchPoint(self):
        self.matchpoint=0
        if self.p1score==self.win:
            self.matchpoint=11
        if self.p2score==self.win:
            self.matchpoint=22
        if self.p1score+1==self.win:
            self.matchpoint=1
        if self.p2score+1==self.win:
            self.matchpoint=2
        if self.p1score+1==self.win and self.p2score+1==self.win:
            self.win+=1

    def run(self):
        clock=pygame.time.Clock()
        while True:
            clock.tick(60)
            self.listen()
            if not (self.matchpoint==11 or self.matchpoint==22):
                self.checkMatchPoint()
            self.draw()
            pygame.display.update()

game=Game()
game.run()

到此这篇关于Python+Pygame编写一个Pong游戏的文章就介绍到这了,更多相关Python Pygame Pong游戏内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Python Pygame实战之实现经营类游戏梦想小镇代码版

    目录 导语 一.注意事项 二.运行环境 三.代码展示 四.效果展示 导语 梦想还是要有的,万一实现了呢?!今天小编就来用代码实现自己专属的城市——特大都市:梦想小镇启航.顾名思义,梦想小镇是梦想花开之地. 我是一名模拟经营类游戏的发烧友,各种农场类.医院类.铁路类的游戏玩儿了很多年.以前在电脑上玩单机版,自从有了手游,就可以随时随地玩儿了(别被老板看到,哈哈哈). 经营类游戏有个特点就是变化少,时间长了难免觉得单调.so,朋友想看看我能不能写出这个类型的游戏,评论区问我给安排不?of cours

  • Python+Pygame实战之俄罗斯方块游戏的实现

    目录 导语 一.运行环境 二.代码展示 三.效果展示 导语 俄罗斯方块,作为是一款家喻户晓的游戏,陪伴70.80甚至90后,度过无忧的儿时岁月 它上手简单能自由组合.拼接技巧也很多. 你知道么,最原始的俄罗斯方块,是长这样婶儿的~ 是不是很有童年的味道?今天小编还要给大家,介绍一个全新版本——程序员的版本,期待期待 自从俄罗斯猫被制裁以后,很多人不禁担心起俄罗斯方块的命运. 虽然名字的含俄量很高,但这款游戏圈抗衰老神话肯定不会遭殃,因为它的版权归美国人所有,跟俄罗斯没半毛钱关系.很多玩了半辈子俄

  • Python+numpy实现一个蜘蛛纸牌游戏

    目录 1.过程 2.思路 3.配置 4.代码 四.效果图 1.过程 蜘蛛纸牌大家玩过没有?之前的电脑上自带的游戏,用他来摸鱼过的举个手. 但是现在的电脑上已经没有蜘蛛纸牌了.所以…… 可不可以自己做一个呢? 想法有了,实践开始. 首先,应该怎么写?首选的方案就是pygame和numpy. 最后选了numpy.一是因为作者用电脑的时间比较短,没有时间力,而手机的在线编译器可以用numpy,不能用pygame.二是因为之前了解过numpy,但是pygame当时都没安装,是昨天才安装完毕的三是因为想挑

  • Python利用3D引擎写一个Pong游戏

    目录 前言 实现方法 完整代码 前言 之前,我们用pygame做了一个2D的Pong游戏,今天我们做一个3D的,游戏画面如下: 用ad和←→操作,双人对战 实现该效果我们使用Python强大的3D引擎Ursina,基础的使用方法见这篇文章:详解Python 3D引擎Ursina如何绘制立体图形 接下来开始写代码吧! 实现方法 首先,导入ursina和随机库 from ursina import * import random as rd 定义两个玩家的分数 scorea=scoreb=0 然后,

  • Python+Pygame实战之炫舞小游戏的实现

    目录 导语 一.环境安装 二.代码展示 三.效果展示 1)简洁版炫舞 ​2)随机截图 3)MISS节拍 导语 昨天去老姐家里蹭饭,进门的时候就看到佳佳妹(我姐的女儿)低头霹雳吧啦一顿操作猛如虎,饭好了都还在玩儿,什么东西这么好玩?一走进就看到这是一款酷似炫舞的小游戏.(死去的回忆突然在攻击我~没想到现在还有这款游戏就是不知道升级了多少次啦) 不知道你们还记不记得曾经有个风靡一时的舞蹈游戏炫舞. 我读小学的时候,大概是09年吧,这个游戏非常火爆,如果你去网吧,十个女生里,有十一个都是在玩炫舞,像我

  • Python之freegames 零代码的22个小游戏集合

    简介 简介:零代码的22个小游戏集合 作者:Grant Jenks 版本:2.4.0 安装: D:\>pip install freegames -i https://pypi.tuna.tsinghua.edu.cn/simple/ Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/ Collecting freegames Downloading https://pypi.tuna.tsinghua.edu.cn/pac

  • Python+Pygame实战之文字剧情游戏的实现

    目录 前言 一.<巨龙之洞> 1)小故事 2)环境配置 3)代码展示 4)效果展示 二.<太空矿工> 1)小故事 2)环境配置 3)代码展示 4)效果展示 前言 哈喽!我是你们的栗子同学——又到周一,新的一天也要元气满满啊~ 想了下是不是你们还是喜欢游戏代码(肯定是 嗯嗯.jpg)今天换个口味给大家写一些文字游戏 吧!送我上热门蛮~下次再写爬虫的吧!喜欢啥写啥哦~ 今日游戏更新——自己取的名字哦,不是在推荐别的游戏,不要限流呀~(代码版本)<巨龙之洞>.<太空矿工

  • Python+Pygame实现接小弹珠游戏

    目录 游戏介绍 效果展示 游戏代码 项目资源 游戏介绍 小学生都不一定会晚的游戏,用挡板接住会反弹的小球,随着次数的增多,速度变快,分数增多. 效果展示 游戏代码 import pygame as pg import sys from random import randint import time pg.init() #对pygame内部各功能模块进行初始化创建及变量设置,默认调用 game_window = pg.display.set_mode((600, 500)) #初始化显示窗口,

  • Python+Kivy编写一个乒乓球游戏

    目录 前言 1.准备 2.简单使用 Kivy 3.Kivy - 添加简单图形 4. Kivy - 增加乒乓球球体 5. kivy - 增加乒乓球体运动 6. Kivy - 球拍移动事件 前言 好久没有写游戏系列教程了,今天恰好浏览到了 Kivy 这个开源跨平台的Python 框架,它能用于开发多点触控的用户界面程序,允许快速简单的交互设计,非常方便,于是有了制作本教程的想法. 本教程将教你如何使用 Kivy 编写一款乒乓球游戏.我们将从一个基本的应用程序开始,描述创建这个游戏的每个步骤. Kiv

  • Python Pygame实现落球游戏详解

    目录 引包 初始化配置 捕捉事件 填充屏幕让球下落 完整代码 引包 引入对应的包,和原来一样写一个打印文字的方法 import sys, random,  pygame from pygame.locals import * def print_text(font, x, y, text, color=(255, 255, 255)):     img_text = font.render(text, True, color)     screen.blit(img_text, (x, y))

  • Python Pygame实战之赛车游戏的实现

    目录 导语 一.环境安装 1)运行环境 2)素材环境 二.代码展示 三.效果展示 游戏开始—— 游戏界面—— 游戏结束—— 导语 小伙伴们大家好~ 如今的游戏可谓是层出不穷,NBA 2K系列啊,FIFA系列啊更是经典中的经典,不过小编发现,赛车游戏也是深受大家欢迎啊,像跑跑卡丁车.QQ飞车,还有主机游戏极品飞车系列. 咳咳咳......小编那时候主要是最喜欢里面的人物颜值来的! 赛车游戏,通常以款式多样的车型.各式各样的赛道.身临其境的拟真度吸引了众多玩家,而玩家在游戏中需要驾驶各类赛车驰骋在世

  • 基于Python+Pygame实现经典赛车游戏

    目录 导语 一.环境安装 二.代码展示 1.主程序main.py 2.地图设置maps.py 三.效果展示 1.游戏界面 2.游戏运行中 3.15分到手 导语 哈喽!哈喽~我是木木子,很久没给大家更新游戏的类似啦—— 有粉丝投稿,说最近由于受疫情影响封闭在家不能离开小区,前不久刚刚报名的驾照考试只能无线延期,在家里还是挺无聊的,“憋在家里没事干的时候去打打游戏写写代码还挺好的. 于是,小编灵机一动,就有了今天这款简易版本的<赛车计划>也就是咳咳咳....... 通俗点儿就是一款代码写的一款关于

  • Python+Pygame实战之泡泡游戏的实现

    目录 导语 一.环境安装 二.代码展示 三.效果展示 导语 泡泡王国 欢乐多多 咕噜噜,吹泡泡,七彩泡泡满天飘.大的好像彩气球,小的就像紫葡萄. ​当泡泡漫天飞舞时,大朋友.小朋友都会情不自禁地被它吸引.而当珍珠般的泡泡遇上可爱的程序员门时,又会出现什么样的美丽风景呢? 说到4399小游戏,没有人会陌生吧?我小时候经常趁着家长不在家的时候偷偷打开电脑打开小游戏的网页,在电脑桌前一坐就是一下午,真的不能赖我不适可而止,而是这些游戏真的太好 玩了!关于童年的经典游戏and有关泡泡的,之前已经仿写了一

  • python+flask编写一个简单的登录接口

    在学习接口测试的时候往往会因为没有实际操作的接口进行测试而烦恼,这里教大家自己编写两个接口用于学习接口测试 1.编写一个登录的接口 2.在pycharm运行 3.使用apipost进行登录接口测试 输入url和参数值进行访问,访问成功. 4.在pycharm查看是否正常进行访问 5.在编写一个需要登录返回的token直接访问的查询接口 6.运行登录和查询两个接口 7.使用apipost进行登录和查询的接口测试 首先进行登录的接口测试获取返回的token 使用登录返回的token值进行查询的接口测

  • Python+Flask编写一个简单的行人检测API

    目录 前提条件 实验环境 项目结构 主要代码 运行结果 前提条件 1.了解Python语言,并会安装第三方库 2.了解Python Web Flask框架 3.了解PyTorch深度学习框架 实验环境 Python 3.6.2 PyTorch 1.7.1 Flask 1.1.1 Numpy 1.18.5 Opencv 3.4.2 PIL pip3 install pillow 项目结构 相关说明: static:用于存储静态文件,比如css.js和图片等 templates:存放模板文件 upl

  • Python Pygame实战之愤怒的小鸟游戏实现

    目录 前言 一.运行环境 二.代码展示 三.效果展示 1)第一关 2)闯关成功 ​3)其他关卡(随机截图) 4)闯关失败 前言 <愤怒的小鸟>其实活得还不错,尽管我们一直在嘲笑它的IP帝国梦做得太大. 但要知道,把休闲益智游戏的生意做到这个份上的,恐怕也就独此一家了.尤其还是这样的一款古早.过时.难让人相信还能翻出什么花样的游戏.继前两期的效果来看,大家还是依旧挺喜欢这款游戏的啦~嘿!我是栗子,今天终于迎来了最终版本啦~ 这一期给大家写完<愤怒的小鸟最终版>三期完结撒花! 后续再想

  • Python+Pygame实现趣味足球游戏

    目录 导语 一.环境安装 二.代码展示 三.效果展示 1)加载界面 2)开始游戏界面 3)开始游戏 ​4)游戏运行 导语 ​足球运动有着“世界第一运动”的美称,还是全球最具影响力的体育运动项目之一. ​小小的足球在滚动中能释放满满的能量和快乐. 足球游戏不仅可以锻炼大家的协调和反应能力,还能提高身体素质哦~ 喜欢足球的小可爱,这一期可以大饱眼福啦 小编准备出一期关于足球的编程代码给大家哦~还有那些喜欢看足球赛的可以自己上jio啦​ ​​温馨提示: 足球游戏时,一定要注意.场地要安全,服装要选好.

随机推荐