会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132437个问题

老师,直接在A的run方法里创建B的线程对象实现包装,在A类中联合,和课堂讲的区别在哪里呢

class A implements Runnable{
    @Override
    public void run() {
        Thread t3 =new Thread(new B());
        t3.start();
        for(int i=0;i<10;i++){
            if(i==5){
                try {
                    t3.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+":  A"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class B implements Runnable{

    @Override
    public void run() {
        for(int i=0;i<20;i++){
            System.out.println(Thread.currentThread().getName()+"  B"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class TestJoinThread {
    public static void main(String[] args) {
        Thread t1 = new Thread(new A());
        t1.start();

        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+"      "+i);
            //当主线程迭代因子为2时,并入A线程
            if(i==2){
                try {
                    t1.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 10126楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 10127楼

2023-04-13.png2023-04-13.png

WEB前端全系列/第六阶段:音乐社区高级项目模块/音乐社区项目_蓝莓派 10128楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 10130楼

import pygame
import random
import time

screen_width = 700
screen_height = 500
bg_cloro = pygame.Color(0,0,255)
COLOR_RED = pygame.Color(255,0,0)

# 主类
class MainGame:
    window = None
    my_tank = None
    def __init__(self):
        pass
    # 开始游戏
    def startGame(self):
        # 初始化窗口
        pygame.display.init()
        # 创建一个窗口
        MainGame.window = pygame.display.set_mode([screen_width,screen_height])
        # 初始化我方坦克
        MainGame.my_tank = MyTank(340,240)
        # 建立一个标题
        pygame.display.set_caption('坦克大战')
        while True:
            # 给窗口添加颜色
            MainGame.window.fill(bg_cloro)
            # 调用方法
            self.getEvent()
            # 绘制文字
            MainGame.window.blit(self.getTextSuface('敌方坦克剩余数量%d'%5),(10,10))
            # 调用坦克显示的方法
            MainGame.my_tank.displayTank()
            # 更新窗口
            pygame.display.update()
            # 每循环依次休息0.02秒
            time.sleep(0.02)
    # 结束游戏
    def endGame(self):
        print("谢谢使用,欢迎再次使用")
        # 结束整个程序
        exit()

    def getTextSuface(self,text):
        # 初始化字体
        pygame.font.init()
        # 获取字体font对象
        font=pygame.font.SysFont("kaiti",18)
        # 绘制文字信息
        textSurface=font.render(text,True,COLOR_RED)
        return textSurface
    # 获取事件的方法
    def getEvent(self):
        # 将获取的所有事件放到eventList列表里面
        eventList = pygame.event.get()
        for event in eventList:
            if event.type == pygame.QUIT:
                self.endGame()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    MainGame.my_tank.direction='U'
                    MainGame.my_tank.move()
                    print("上")
                elif event.key == pygame.K_DOWN:
                    MainGame.my_tank.direction = 'D'
                    MainGame.my_tank.move()
                    print("下")
                elif event.key == pygame.K_LEFT:
                    MainGame.my_tank.direction = 'L'
                    MainGame.my_tank.move()
                    print("左")
                elif event.key == pygame.K_RIGHT:
                    MainGame.my_tank.direction = 'R'
                    MainGame.my_tank.move()
                    print("右")

# 坦克类
class Tank:
    def __init__(self):
        pass
    # 移动方法
    def move(self):
        # 判断坦克方向的移动
        if self.direction=='U':
            self.rect.top-=self.speed
        elif self.direction=='D':
            self.rect.top+=self.speed
        elif self.direction=='L':
            self.rect.left-=self.speed
        elif self.direction=='R':
            self.rect.left+=self.speed
    # 发射子弹的方法
    def shot(self):
        pass
    # 显示坦克的方法
    def displayTank(self):
        # 获取展示的对象
        #self.image=self.images[self.direction]
        # 调用blit方法展示
        MainGame.window.blit(self.image,self.rect)

# 我方坦克类
class MyTank(Tank):
    def __init__(self,left,top):
        # 保存加载的图片
        self.images={
            'U':pygame.image.load('img/p1tankU.gif'),
            'D':pygame.image.load('img/p1tankD.gif'),
            'L':pygame.image.load('img/p1tankL.gif'),
            'R':pygame.image.load('img/p1tankR.gif'),
        }
        # 方向
        self.direction = 'L'
        #根据当前图片的方向获取图片 surface
        self.image=self.images[self.direction]
        #根据图片获取区域
        self.rect=self.image.get_rect()
        #设置区域的left 和top
        self.rect.left=left
        self.rect.top=top
        # 速度,决定移动的快慢
        self.speed=10

# 敌方坦克类
class Enemy(Tank):
    def __init__(self):
        pass

# 子弹类
class Bullet:
    def __init__(self):
        pass
    # 移动的方法
    def bulletMove(self):
        pass
    # 显示子弹的方法
    def displayBullet(self):
        pass

# 墙壁类
class Wall:
    def __init__(self):
        pass
    # 显示墙壁的方法
    def displayWall(self):
        pass

# 爆炸效果类
class Explode:
    def __init__(self):
        pass
    # 展示爆炸效果的方法
    def displayExplode(self):
        pass

# 音乐类
class Music:
    def __init__(self):
        pass
    # 播放音乐
    def playMusic(self):
        pass

if __name__ == "__main__":
    MainGame().startGame()

我的坦克上下左右移动都没问题,但是不能切换方向(就是不换图片,方向一直朝左),不知道为什么

Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 10132楼

package com.Thread;

/**
 *测试缓冲区的作用
 */
public class TestBufferThread {
    public static void main(String[] args) {
      SyncStack ss=new SyncStack();
      new XiaoFei(ss).start();
        new Product(ss).start();
    }
}
/***
 * 设置一个馒头类
 */
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();
            }
            //唤醒取馒头的线程
            /**
             * 语法:该方法必须要在synchronized块中调用。
             * 该方法会唤醒处于等待状态队列中的一个线程。
             */
            this.notify();
            this.mt[this.index]=mantou;
            this.index++;
        }
    }
    /**
     * 取馒头
     */
    public synchronized Mantou pop(){
        while(this.index == 0){
            try {
                /**
                 * 语法:wait(),该方法必须要在synchronized块中调用。
                 * wait执行后,线程会将持有的对象锁释放,并进入阻塞状态,
                 * 其他需要该对象锁的线程就可以继续运行了。
                 */
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.index--;
        return this.mt[this.index];
    }

}
   /**
 * 定义生产者线程
 */
class  Product extends Thread{
       //传入缓冲区
       private SyncStack ss;
       public Product(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());
        }
    }
}

老师,帮我看看,只启动生产者线程,消费者线程没反应?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 10133楼
JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 10134楼
JAVA 全系列/预科阶段:职业规划/学习方法/程序员的基本素养和职业规划 10136楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 10137楼
JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 10138楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 10139楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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