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([self.width,self.height*1.5]).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),[0,0,self.width,self.height*1.5])
pygame.draw.rect(self.image,(255,255,255),[self.width*0.1,0,self.width*0.8,self.height])
# 画四个轮子
pygame.draw.rect(self.image,(0,0,0),[0,self.height*0.1,10,15])
pygame.draw.rect(self.image,(0,0,0),[self.width-10,self.height*0.1,10,15])
pygame.draw.rect(self.image,(0,0,0),[0,self.height*0.75,10,15])
pygame.draw.rect(self.image,(0,0,0),[self.width-10,self.height*0.75,10,15])
self.all_speed = 0 # 当前帧x速度+y速度
self.max_speed = 10
self.speed = [0,0]
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[0] = -math.sin(self.radius)*self.all_speed
self.speed[1] = -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([self.width,self.height]).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)),
[0,0,self.width,self.height])
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[0][0]
car.speed_up = car.gears[0][1]
elif event.key == K_2:
car.max_speed = car.gears[1][0]
car.speed_up = car.gears[1][1]
elif event.key == K_3:
car.max_speed = car.gears[2][0]
car.speed_up = car.gears[2][1]
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[0]
bgthing.rect.y -= car.speed[1]
#画背景
screen.fill(bg)
#画 xxxx
sprites.update()
bgthings.update()
bgthings.draw(screen)
sprites.draw(screen)
# 刷新界面
pygame.display.update()