cjjJasonchen 发表于 2023-7-20 14:19:13

【pygame】 模拟车辆移动--预告 2023/7/22

本帖最后由 cjjJasonchen 于 2023-7-22 13:09 编辑

【pygame】模拟车辆移动



家人们,上回不是说要跟新拖动和连带运动吗。。。
enmm这回先更新连带运动的---------------运动!


要先把前置科技点起来不是吗?{:10_279:}

话不多说先放出来给鱼油们看看效果,表示一下我这些时间没有偷懒



只用了pygame一个库

当然啦,这只是一个半成品,代码先给大火看了,但是后续肯定还要升级的,

想要学的先自己研究吧~ {:10_304:}

教程会在以后这个玩意升级好之后更新~

所以大家给评论鼓励我一下就可以啦!评分就等下次吧!{:10_312:}

#这次加了一个挂档和更平滑更难的转向

嗨嗨嗨嗨 代码来喽!




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

class Car(pygame.sprite.Sprite):
    def __init__(self):
      super().__init__()
      self.width,self.height = 50,100
      self.image = pygame.Surface().convert_alpha()
      self.o = self.image # 备份原图
      self.orect = self.o.get_rect()
      self.rect = self.orect

      # 设定初始位置
      self.orect.centerx = 400
      self.orect.centery = 300
      
      # 透明背景和主体
      pygame.draw.rect(self.image,(0,0,0,0),)
      pygame.draw.rect(self.image,(255,255,255),)

      # 画四个轮子
      pygame.draw.rect(self.image,(0,0,0),)
      pygame.draw.rect(self.image,(0,0,0),)
      pygame.draw.rect(self.image,(0,0,0),)
      pygame.draw.rect(self.image,(0,0,0),)
      
      self.all_speed = 0 # 当前帧x速度+y速度
      self.max_speed = 10
      self.speed =
      self.speed_up = 0.2 # 每帧增加速度
      self.angle = 0
      self.turn = 0 # 这帧旋转速度
      self.max_turn = 5 # 最大时速时最大旋转速度
      self.resist = 0.1 # 无操作时每帧减速(阻力)
      self.gears = [(10,1),(20,0.2),(30,0.12)] # 档

      # 按键状态
      self.w = False
      self.s = False
      self.a = False
      self.d = False

      # 车辆状态
      self.forward = False


    def update(self):

      # 保证角度不会超过360
      if self.angle >= 360:
            self.angle -= 360

      # 保证角度不会是负数,为后面的计算提供方便
      if self.angle < 0:
            self.angle = 360+self.angle

      self.radius = math.pi/180*self.angle # 转化为弧度

      # 旋转车
      self.image = pygame.transform.rotate(self.o,self.angle)
      self.rect = self.image.get_rect()
      self.rect.centerx = self.orect.centerx
      self.rect.centery = self.orect.centery

      # 判断油门刹车是否踩下和是否达到最高速度
      if self.w and self.all_speed < self.max_speed:
            self.all_speed += self.speed_up
      elif self.s and self.all_speed > -self.max_speed/2:
            self.all_speed -= self.speed_up

      # 计算移动
      self.speed = -math.sin(self.radius)*self.all_speed
      self.speed = -math.cos(self.radius)*self.all_speed
      
      
      if self.all_speed > self.resist+0.01:
            # 转向
            if self.d and self.turn > -self.max_turn:# 转向速度每帧提高0.2
                self.turn -= 0.2
            elif self.a and self.turn < self.max_turn:
                self.turn += 0.2
               
            elif not self.d and self.turn < 0:# 每帧降低0.2
                self.turn += 0.2
                if self.turn > -0.2:
                  self.turn = 0
            elif not self.a and self.turn > 0:
                self.turn -= 0.2
                if self.turn < 0.2:
                  self.turn = 0
            

            #计算角度
            self.angle += self.turn*(self.all_speed/self.max_speed)
            #阻力
            self.all_speed -= self.resist
            

      
      elif self.all_speed < self.resist-0.01:
            # 转向
            if self.d and self.turn > -self.max_turn:
                self.turn -= 0.2

            elif self.a and self.turn < self.max_turn:
                self.turn += 0.2

            elif not self.d and self.turn < 0:
                self.turn += 0.2
                if self.turn > -0.2:
                  self.turn = 0
            elif not self.a and self.turn > 0:
                self.turn -= 0.2
                if self.turn < 0.2:
                  self.turn = 0

            #计算角度
            self.angle += self.turn*(self.all_speed/self.max_speed)
            #阻力
            self.all_speed += self.resist

      

      

class Thing(pygame.sprite.Sprite):
    def __init__(self):
      # 背景
      super().__init__()
      self.width,self.height = randint(10,300),randint(10,300)
      self.image = pygame.Surface().convert_alpha()
      self.rect = self.image.get_rect()
      self.rect.x, self.rect.y = randint(-800,1600),randint(-800,1600)
      pygame.draw.rect(self.image,
                         (randint(100,255),randint(100,255),randint(100,255)),
                         )


if __name__ == "__main__":
    size = width, height = 800,600

    screen = pygame.display.set_mode(size)

    pygame.display.set_caption("漂移")

    clock = pygame.time.Clock()

    delay = 60 # 延时计时器
    time = 0

    # 背景颜色
    bg = (85,85,85)

    # 是否全屏
    fullscreen = False
    screen_change = False

    running = True

    #实例化对象
    sprites = pygame.sprite.Group()
    bgthings = pygame.sprite.Group()
    car = Car()

    # 生成背景
    for i in range(300):
      #sprites.add(Thing())
      bgthings.add(Thing())
      

    sprites.add(car)

    while running:
      clock.tick(60)

      # 检测是否全屏
      if fullscreen and screen_change:
            screen = pygame.display.set_mode(size,FULLSCREEN,HWSURFACE)
            screen_change = False
      elif screen_change:
            screen = pygame.display.set_mode(size)
            screen_change = False

      for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
               
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                  print("左键按下")

            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                  pygame.quit()
                  sys.exit()
                  
                #F11切换全屏
                elif event.key == K_F11:
                  fullscreen = not fullscreen
                  screen_change = True

                elif event.key == K_a:
                  car.a = True

                elif event.key == K_d:
                  car.d = True

                elif event.key == K_w:
                  car.w = True
                  
                elif event.key == K_s:
                  car.s = True

                elif event.key == K_q:
                  car.q = True
                  
                elif event.key == K_e:
                  car.e = True

                elif event.key == K_1:
                  car.max_speed = car.gears
                  car.speed_up = car.gears

                elif event.key == K_2:
                  car.max_speed = car.gears
                  car.speed_up = car.gears

                elif event.key == K_3:
                  car.max_speed = car.gears
                  car.speed_up = car.gears

                  
            elif event.type == KEYUP:
                if event.key == K_a:
                  car.a = False
                  #if car.turn >0:
                        #car.turn = 0

                elif event.key == K_d:
                  car.d = False
                  #if car.turn <0:
                        #car.turn = 0

                elif event.key == K_w:
                  car.w = False

                elif event.key == K_s:
                  car.s = False

                elif event.key == K_q:
                  car.q = False

                elif event.key == K_e:
                  car.e = False

      # 移动镜头
      for bgthing in bgthings:
            bgthing.rect.x -= car.speed
            bgthing.rect.y -= car.speed

      #画背景
      screen.fill(bg)

      #画 xxxx
      sprites.update()
      bgthings.update()
      bgthings.draw(screen)
      sprites.draw(screen)

      # 刷新界面
      pygame.display.update()






拜拜~ 以会再更新!

哦对了 按键是wasd 很简单吧~

加了1、2、3的挂挡,每个档位加速度不一样,转向半径不一样   ------   从高档位突然挂到低挡位会导致转弯半径突然变小bug?

大家先玩玩看吧,有什么意见可以提出来哦~





漂移展示看图:
各位鱼油可以思考一下漂移是如何实现的

歌者文明清理员 发表于 2023-7-20 14:33:56

牛啊

cjjJasonchen 发表于 2023-7-20 14:36:26

歌者文明清理员 发表于 2023-7-20 14:33
牛啊

哇哇哇别急着评分啊,这个帖子以后还会越来越值钱的{:10_302:}

cjjJasonchen 发表于 2023-7-20 14:37:03

歌者文明清理员 发表于 2023-7-20 14:33
牛啊

虽然它现在只值3贡献{:10_329:}

不二如是 发表于 2023-7-20 15:00:06

期待

sfqxx 发表于 2023-7-20 15:41:33

期待

小甲鱼 发表于 2023-7-20 16:02:38

有点意思了,是不是会有避障功能和自动驾驶 {:10_297:}

cjjJasonchen 发表于 2023-7-20 16:56:49

小甲鱼 发表于 2023-7-20 16:02
有点意思了,是不是会有避障功能和自动驾驶

避障功能还有自动驾驶可能对于我这种小白来说有点难,但写个坦克大战的ai或许还是可以的{:10_307:}

偷偷剧透找亮点o这剧透也太直白了吧{:10_328:}



Ewan-Ahiouy 发表于 2023-7-20 17:13:04

这是什么?{:10_257:}

cjjJasonchen 发表于 2023-7-20 17:31:52

本帖最后由 cjjJasonchen 于 2023-7-20 17:50 编辑

Ewan-Ahiouy 发表于 2023-7-20 17:13
这是什么?

你可以假装这个带着四个轮子的长方形是一辆车

{:10_328:}
https://xxx.ilovefishc.com/forum/202307/17/161432nbtt5wj5m5rmetoe.gif

编程追风梦 发表于 2023-7-21 07:02:07

感觉这篇帖子会火,支持楼主!我先蹲个楼

JasonChencCjj 发表于 2023-7-22 12:35:17

上去!{:10_257:}

JasonChencCjj 发表于 2023-7-22 13:06:05

币来!

JasonChencCjj 发表于 2023-7-22 18:23:17

编程追风梦 发表于 2023-7-21 07:02
感觉这篇帖子会火,支持楼主!我先蹲个楼

大佬,我推一下我的帖子,能不能来支持一下
python + easygui 制作四则运算出题器
https://fishc.com.cn/thread-231147-1-1.html (出处: 鱼C论坛)

琅琊王朝 发表于 2023-7-23 20:54:28

{:5_106:}

零之忆 发表于 2023-7-25 12:22:25

{:9_241:}

vcvsk 发表于 2023-8-9 17:47:23

厉害

cjjJasonchen 发表于 2023-8-9 19:34:42

vcvsk 发表于 2023-8-9 17:47
厉害

{:10_297:}

QQQAAAXXX 发表于 2024-1-27 01:07:01

闪光少年 发表于 2024-1-27 09:53:33

牛啊
页: [1] 2
查看完整版本: 【pygame】 模拟车辆移动--预告 2023/7/22