会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132433个问题
JAVA 全系列/第八阶段:Linux入门到实战/Maven 17088楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Title</title>
    <style>
        *{
            margin: 0;
        }
        .box{
            width: 1263px;
            height: 750px;
            /*background: red;*/
        }
        .wrap{
            width: 25%;
            height: 375px;
            /*background: blue;*/
            float: left;
            padding: 10px;
            box-sizing: border-box;
            position: relative;
        }
        .items>img{
            width: 100%;
            height: 100%;
        }
        .items-name{
            position: absolute;
            top: 265px;
            left:25px;
        }
        .items-name1{
            left:90px;
        }
        .items-information{
            position: absolute;
            top: 300px;
            left:40px;
        }
        .items-information1{
            left:25px;
        }
        .items-price{
            position: absolute;
            top: 310px;
            left:100px;
        }
        .box-p{
            width: 303px;
            height: 80px;
        }
        .box-p>p:first-of-type{
            color: #333333;
            font-size: 16px;
            margin-top: 1px;
        }
        .box-p>p:nth-child(2){
            font-size: 14px;
            color: #999999;
        }
        .box-p>p:last-of-type{
            font-size: 22px;
            color: #cc0000;
        }
        span{
            color: #999999;
            font-size: 12px;
        }
        .items:hover{
            box-shadow: 0 10px 10px rgba(0,0,0,.18);
            /*transform: scale(1.1);*/
        }
        .circle{
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: linear-gradient(to bottom right,hotpink,pink);
            position: absolute;
            top: 20px;
            left: 20px;
            line-height: 50px;
        }
    /*    移动端*/
        @media screen and (max-width: 768px) {
            .wrap{
                width: 50%;
            }
        }
    /*    Ipad端*/
        @media screen and(max-width: 992px) and (min-width: 768px){
            .wrap{
                width: 33%;
            }
        }
    /*    pc端*/
        @media screen and (min-width: 992px){
            .wrap{
                width: 25%;
            }
        }
    </style>
</head>
<body>
 <div class="box">
     <div class="wrap">
         <div class="items">
             <img src="魅族images/be6fed05-168e-4633-bc1d-6e75705fce07.jpg" alt="">
             <p class="items-name" style="font-size: 21px;font-weight: 700;color: #ffffff">魅族 HD60 头戴式蓝牙耳机</p>
             <p class="items-information" style="color: #ffffff;font-size: 14px">40mm生物振膜 | 触控操作 | 蓝牙5.0</p>
             <p class="items-price" style="font-size: 24px;font-weight: 700;color:gold">¥499</p>
         </div>
     </div>
     <div class="wrap">
         <div class="items">
             <img src="https://openfile.meizu.com/group1/M00/08/5E/Cgbj0V_38NOAGvEXAAN6WGRLi3Y197.png" alt="">
             <div class="box-p" align="center">
                 <p>魅族 POP Pro 主动降噪耳机</p>
                 <p>【限时特惠449元+3期免息】</p>
                 <p>¥449 <span><small><del>¥499</del></small></span></p>
             </div>
         </div>
         <div class="circle" style="color: white;text-align: center">
         <p>免息</p>
     </div>
     </div>
     <div class="wrap">
         <div class="items">
             <img src="https://openfile.meizu.com/group1/M00/08/40/Cgbj0V-JCMSAa5qBAAMth2Y6_gU978.png" alt="">
             <div class="box-p" align="center">
                 <p>魅族 POP2s 真无线耳机</p>
                 <p>【限时特惠199元+3期免息】</p>
                 <p>¥199 <span><small><del>¥299</del></small></span></p>
             </div>
         </div>
         <div class="circle" style="color: white;text-align: center">
             <p>免息</p>
         </div>
     </div>
     <div class="wrap">
         <div class="items">
             <img src="https://openfile.meizu.com/group1/M00/07/2F/Cgbj0Vy_C3GAN_TKAAExaPfTwFc180.png" alt="">
             <div class="box-p" align="center">
                 <p>魅族 HIFI 解码耳放</p>
                 <p>【限时特惠149元】</p>
                 <p>¥149 <span><small><del>¥169</del></small></span></p>
             </div>
         </div>
     </div>
     <div class="wrap">
         <div class="items">
             <img src="https://fms.res.meizu.com/dms/2019/08/29/04811a8d-07fb-4188-a852-1971f442d08f.jpg" alt="">
             <p class="items-name items-name1" style="font-size: 21px;font-weight: 700;color: #ffffff">魅族 EP3C 耳机</p>
             <p class="items-information items-information1" style="color: #ffffff;font-size: 14px">Hi-Res 认证高解析音质 | Type-C数字接口</p>
             <p class="items-price" style="font-size: 24px;font-weight: 700;color:gold">¥
                 129</p>
         </div>
     </div>
     <div class="wrap">
         <div class="items">
             <img src="https://openfile.meizu.com/group1/M00/07/E0/Cgbj0V6z8VSAV6kEAAlCc-omYno614.png" alt="">
             <div class="box-p" align="center">
                 <p>魅族 HD60 降噪耳机</p>
                 <p>【限时特惠799元+12期免息】</p>
                 <p>¥799 <span><small><del>¥1099</del></small></span></p>
             </div>
         </div>
         <div class="circle" style="color: white;text-align: center">
             <p>免息</p>
         </div>
     </div>
     <div class="wrap">
         <div class="items">
             <img src="https://openfile.meizu.com/group1/M00/07/86/Cgbj0V3UocyAR_tJAApbDmCAyEo620.png" alt="">
             <div class="box-p" align="center">
                 <p>魅族 HD60 头戴式蓝牙耳机</p>
                 <p>40mm生物振膜 | 触控操作 | 蓝牙5.0</p>
                 <p>¥449</p>
             </div>
         </div>
     </div>
     <div class="wrap">
         <div class="items">
             <img src="https://openfile.meizu.com/group1/M00/04/17/Cgbj0VrcX6yABHxPAARZwWUAjc4748.png" alt="">
             <div class="box-p" align="center">
                 <p>魅族 HALO 激光蓝牙耳机</p>
                 <p>炫酷夜跑神器 随性张扬</p>
                 <p>¥449 <span><small><del>¥999</del></small></span></p>
             </div>
         </div>
     </div>
 </div>
</body>
</html>

我为什么缩小到移动端的时候,图片并没有变小,感觉所有的格式都乱了呢,还有就是并没有按相应该显示的几个

WEB前端全系列/第一阶段:HTML5+CSS3模块/CSS盒子模型 17091楼
Python 全系列/第七阶段:网页编程基础/html5 17092楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Spring 17093楼
JAVA 全系列/第九阶段:Spring Boot实战/Spring Boot 17094楼
人工智能/第五阶段:机器学习-线性回归/Lasso回归_Ridge回归_多项式回归 17095楼

import pygame as p
screen_widht = 1800
screen_height = 1000
bg_color = p.Color(0,0,0)
text_color = p.Color(255,0,0)
class MainGame():
    window =None
    my_tank=None
    def __init__(self):
        pass
    def startGame(self):
        p.display.init()
        MainGame.window=p.display.set_mode([screen_widht,screen_height]) #设置高度和宽度,必须是一个列表
        MainGame.my_tank=Tank(850,400)

        p.display.set_caption('坦克大战')
        while True:
            MainGame.window.fill(bg_color)
            self.getEvent()
            MainGame.window.blit(self.getTextSuface('敌方坦克剩余数量:6'),(10,10))
            MainGame.my_tank.displayTank() #让坦克进行显示
            p.display.update()
    def endGame(self):
        print('谢谢使用,欢迎下次再来')
        exit()#这个函数的功能就是关闭窗口
    def getTextSuface(self,text):
        p.font.init()
        font = p.font.SysFont('kaiti',18)
        TextSurface=font.render(text,True,text_color)
        return TextSurface
    def getEvent(self):
        # 读取全部事件
        eventList = p.event.get()
        for event in eventList:
            #判断按下的是关闭还是键盘按键
            #当按键按下的是关闭时,退出
            if event.type == p.QUIT:
                self.endGame()
            #当按下的是键盘按键时
            if event.type == p.KEYDOWN:
                
                if event.key == p.K_LEFT:
                    #让坦克切换方向
                    MainGame.my_tank.direction = 'L'
                    #调用方法, 让坦克移动
                    MainGame.my_tank.move()

                    print('按下左键')
                if event.key == p.K_RIGHT:
                    MainGame.my_tank.direction ='R'
                    MainGame.my_tank.move()
                    print('按下右键')
                if event.key == p.K_UP:
                    MainGame.my_tank.direction ='U'
                    MainGame.my_tank.move()
                    print('按下上键')
                if event.key == p.K_DOWN:
                    MainGame.my_tank.direction ='D'
                    MainGame.my_tank.move()
                    print('按下下键')
class Tank():
    def __init__(self,left,top):
        self.images={'U':p.image.load('img\Pk01-U.jpg'),
                     'D':p.image.load('img\Pk01-D.jpg'),
                     'L':p.image.load('img\Pk01-L.jpg'),
                     'R':p.image.load('img\Pk01-R.jpg')}
        #方向
        self.direction='U' 
        #根据当前图片的方向获取图片 
        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
    #移动
    def move(self):
        #判断坦克的方向移动
        #如果向左移动
        if self.direction == 'L':
            self.rect.left -= self.speed
        #如果向上移动
        if self.direction == 'U' :
            self.rect.top -= self.speed  
        #如果向下移动 
        if self.direction == 'D':
            self.rect.top += self.speed
        #r如果向右移动
        if self.direction == 'R':
            self.rect.left += self.speed
    def displayTank(self):
        self.image=self.images[self.direction]
        MainGame.window.blit(self.image,self.rect)


    
MainGame().startGame()
老师我在对坦克左右移动时,无法改变方向,只能朝上来左右移动,而当我按下下键时就可以使坦克朝下


Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 17096楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 17097楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 17099楼
JAVA 全系列/第二十七阶段:JVM实战和BATJ大厂面试重难点/MySQL优化(旧) 17100楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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