会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133791个问题
JAVA 全系列/第十三阶段:高性能数据处理、NoSQL、分库分表/Redis 4456楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/MySQL数据库 4457楼

pygame,time
random
_display=pygame.display
COLOR_BLACK=pygame.Color(,,)
COLOR_RED=pygame.Color(,,)
():
    window=TANK_P1=SCREEN_HEIGHT=SCREEN_WIDTH=EnemyTank_list=[]
    EnemyTank_count=():
        ():
        _display.init()
        MainGame.window=_display.set_mode([MainGame.SCREEN_WIDTH,MainGame.SCREEN_HEIGHT])
        MainGame.TANK_P1 = Tank(, )
        .createEnemyTank()
        _display.set_caption()

        :
            time.sleep()
            MainGame.window.fill(COLOR_BLACK)
            .getEvent()
            MainGame.window.blit(.getTextface(%),(,))

            MainGame.TANK_P1.displayTank()

            .displayEnemyTank()
            MainGame.TANK_P1.stop == :
                MainGame.TANK_P1.move()
            _display.update()


    ():
        top = i (MainGame.EnemyTank_count):
            left = random.randint(, )
            speed = random.randint(, )
            eTank = EnemyTank(left, top, speed)
            MainGame.EnemyTank_list.append(eTank)


   ():
        eTank MainGame.EnemyTank_list:
            eTank.displayTank()
            eTank.randMove()

    ():
        ()
        ()

    (,text):
        pygame.font.init()
        font=pygame.font.SysFont(,)
        textSurface=font.render(text,,COLOR_RED)
        textSurface

():
        eventList=pygame.event.get()
        event eventList:
    event.type==pygame.QUIT:
                .endGame()
    event.type==pygame.KEYDOWN:
     event.key==pygame.K_LEFT:
                    ()
                    MainGame.TANK_P1.direction=MainGame.TANK_P1.stop=event.key==pygame.K_RIGHT:
                    ()
                    MainGame.TANK_P1.direction=MainGame.TANK_P1.stop=event.key == pygame.K_UP:
                        ()
                        MainGame.TANK_P1.direction = MainGame.TANK_P1.stop = event.key == pygame.K_DOWN:
                            ()
                            MainGame.TANK_P1.direction = MainGame.TANK_P1.stop = event.key==pygame.K_SPACE:
                    ()


             event.type==pygame.KEYUP:
                    event.key==pygame.K_LEFT event.key==pygame.K_RIGHT \
                        event.key == pygame.K_UP event.key==pygame.K_DOWN:
                     MainGame.TANK_P1.stop=():
    (,left,top):
        .images={
            :pygame.image.load(),
            : pygame.image.load(),
            : pygame.image.load(),
            : pygame.image.load()
        }

        .direction=.image=.images[.direction]
        .rect=.image.get_rect()
        .rect.left=left
        .rect.top=top
        .stop = .speed=():
        .image=.images[.direction]
        MainGame.window.blit(.image,.rect)


():
        .direction==:
            .rect.left>:
                .rect.left-=.speed
        .direction==:
            .rect.left+.rect.width<MainGame.SCREEN_WIDTH:
                .rect.left+=.speed
        .direction==:
            .rect.top > :
                 .rect.top-=.speed
        .direction==:
            .rect.top + .rect.height< MainGame.SCREEN_HEIGHT:
                .rect.top += .speed

    ():
        ():
        ():
        (Tank):
    ():
        ():
        (Tank):

    (,left,top,speed):
        .images={
            :pygame.image.load(),
            :pygame.image.load(),
            :pygame.image.load(),
            :pygame.image.load()
        }
        .direction=.randDirection()
        .image=.images[.direction]
        .rect=.image.get_rect()
        .rect.left=left
        .rect.top=top
        .speed=speed
        .stop=.step=():
            num=random.randint(,)
            num==:
                 num == :
                num == :
                num == :
                ():
        .step<=:
            .direction=.randDirection()
            .step=:
            .move()
            .step-=():
        ():
    ():
        ():
        ():
        ():
        ():
        ():
        ():
    ():
        ():
        ():
    ():
        ():
        ():
    ():
        ():
        __name__==:
    MainGame().startGame()


报错
"C:\Program Files (x86)\Python36-32\python.exe" C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py
pygame 2.0.1 (SDL 2.0.14, Python 3.6.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py", line 284, in <module>
    MainGame().startGame()
  File "C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py", line 28, in startGame
    self.createEnemyTank()
  File "C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py", line 59, in createEnemyTank
    eTank = EnemyTank(left, top, speed)
  File "C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py", line 208, in __init__
    self.image=self.images[self.direction]
KeyError: None

Process finished with exit code 1


Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 4458楼
JAVA 全系列/第一阶段:AI驱动的JAVA编程/面向对象详解和JVM底层内存分析 4459楼
JAVA 全系列/第十九阶段:亿级高并发电商项目/亿级高并发电商项目(旧) 4460楼
Python 全系列/第一阶段:AI驱动的Python编程/控制语句 4461楼
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/GraphQL 4462楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 4463楼
JAVA 全系列/第六阶段:项目管理与SSM框架/SpringMVC 4464楼
JAVA 全系列/第十九阶段:亿级高并发电商项目/亿级高并发电商项目(旧) 4465楼
JAVA 全系列/(旧的隐藏)第二十一阶段:百战商城项目(Spring Cloud最新架构)/百战商城项目 4466楼
Python 全系列/第一阶段:AI驱动的Python编程/编程基本概念 4467楼

package com.example.Jsp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/language.do")
public class LanguageJsp extends HttpServlet {
    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     *
     * 实现一个Servlet执行一个请求转发,基于Jsp页面显示数据内容
     */

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String header = req.getHeader("Accept-Language");

        req.setAttribute("key",header);

        req.getRequestDispatcher("LanguageDisplay.jsp");

    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        String key = (String) request.getAttribute("key");
    %>
    浏览器支持的语言为:<%= key%>
</body>
</html>


老师,我在这里面做了一个尝试,就是在请求转发时,我不重传req和resp对象

image.png



我的理解是,我尝试不传对象,那么在新的jsp页面中,自己本身的request对象就是null,就算显示不出来key数据,也应该显示出来的结果为:

浏览器支持的语言为:null

但是我发现当我输入content-path为:http://localhost:8080/Jsp/language.do时  我的浏览器,显示的页面是空的;但是当我输入:http://localhost:8080/Jsp/LanguageDisplay.jsp时,显示的确实是

浏览器支持的语言为:null

为什么?求解

JAVA 全系列/第六阶段:JavaWeb开发/JSP技术详解(旧) 4468楼

/**
 * 定义馒头类
 */
class ManTou{
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

/**
 * 定义缓冲区
 */
class SyncStack{
    //定义存放馒头的盒子
    private ManTou[] mt = new ManTou[10];

    //定义操作盒子的索引
    private int index;
}

/**
 * 放馒头
 */
    public synchronized void push(ManTou manTou){
        //判断盒子是否满了
        while(this.index == this.mt.length){
            try {
                /**
                 * 语法:wait(),该方法必须要在synchronized块中调用。
                 * wait执行后,线程会将持有的对象锁释放,并进入阻塞状态,
                 * 其他需要该对象锁的线程就可以继续运行了。
                 */
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.mt[this.index] = manTou;
        this.index++;
    }

/**
 *取馒头
 */

    public synchronized ManTou pop(){
        while (this.index ==0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.index--;
        return this.mt[this.index];
    }



/**
 * 定义生产者线程类
 */
class Shengchan extends Thread{
    //需要一个缓冲区放馒头
    private SyncStack ss;
    //把缓冲区对象传递进来,将数据缓冲区传到生产者线程当中
    public  Shengchan(SyncStack ss){
        this.ss = ss;
    }

    @Override
    public void run() {
        for (int i = 0; i <10 ; i++) {
            ManTou mt= new ManTou();
            mt.setId(i);
            System.out.println("生产馒头:" + mt.getId());
            this.ss.push(mt);
        }
    }
}

/**
 * 定义消费者线程类
 */
class XiaoFei extends Thread {
    private SyncStack ss;

    //把缓冲区对象传递进来,将数据缓冲区传到生产者线程当中
    public XiaoFei(SyncStack ss) {
        this.ss = ss;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            ManTou mt = this.ss.pop();
            System.out.println("消费馒头:" + mt.getId());
        }
    }
}



public class TestProduceThread {
    public static void main(String[] args) {
        //创建缓冲区
        SyncStack ss = new SyncStack();
        new Shengchan(ss).start();
        new XiaoFei(ss).start();
     }
}

老师不知道哪里出错了帮忙看一下

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 4470楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637