import pygame
import random
from objects import Road, Player, Nitro, Tree, Button, \
Obstacle, Coins, Fuel
pygame.init()
SCREEN = WIDTH, HEIGHT = 288, 512
info = pygame.display.Info()
width = info.current_w
height = info.current_h
if width >= height:
win = pygame.display.set_mode(SCREEN, pygame.NOFRAME)
else:
win = pygame.display.set_mode(SCREEN, pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN)
clock = pygame.time.Clock()
FPS = 30
lane_pos = [50, 95, 142, 190]
# COLORS **********************************************************************
WHITE = (255, 255, 255)
BLUE = (30, 144,255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 20)
# FONTS ***********************************************************************
font = pygame.font.SysFont('cursive', 32)
select_car = font.render('Select Car', True, WHITE)
# IMAGES **********************************************************************
bg = pygame.image.load('Assets/bg.png')
home_img = pygame.image.load('Assets/home.png')
play_img = pygame.image.load('Assets/buttons/play.png')
end_img = pygame.image.load('Assets/end.jpg')
end_img = pygame.transform.scale(end_img, (WIDTH, HEIGHT))
game_over_img = pygame.image.load('Assets/game_over.png')
game_over_img = pygame.transform.scale(game_over_img, (220, 220))
coin_img = pygame.image.load('Assets/coins/1.png')
dodge_img = pygame.image.load('Assets/car_dodge.png')
left_arrow = pygame.image.load('Assets/buttons/arrow.png')
right_arrow = pygame.transform.flip(left_arrow, True, False)
home_btn_img = pygame.image.load('Assets/buttons/home.png')
replay_img = pygame.image.load('Assets/buttons/replay.png')
sound_off_img = pygame.image.load("Assets/buttons/soundOff.png")
sound_on_img = pygame.image.load("Assets/buttons/soundOn.png")
cars = []
car_type = 0
for i in range(1, 9):
img = pygame.image.load(f'Assets/cars/{i}.png')
img = pygame.transform.scale(img, (59, 101))
cars.append(img)
nitro_frames = []
nitro_counter = 0
for i in range(6):
img = pygame.image.load(f'Assets/nitro/{i}.gif')
img = pygame.transform.flip(img, False, True)
img = pygame.transform.scale(img, (18, 36))
nitro_frames.append(img)
# FUNCTIONS *******************************************************************
def center(image):
return (WIDTH // 2) - image.get_width() // 2
# BUTTONS *********************************************************************
play_btn = Button(play_img, (100, 34), center(play_img)+10, HEIGHT-80)
la_btn = Button(left_arrow, (32, 42), 40, 180)
ra_btn = Button(right_arrow, (32, 42), WIDTH-60, 180)
home_btn = Button(home_btn_img, (24, 24), WIDTH // 4 - 18, HEIGHT - 80)
replay_btn = Button(replay_img, (36,36), WIDTH // 2 - 18, HEIGHT - 86)
sound_btn = Button(sound_on_img, (24, 24), WIDTH - WIDTH // 4 - 18, HEIGHT - 80)
# SOUNDS **********************************************************************
click_fx = pygame.mixer.Sound('Sounds/click.mp3')
fuel_fx = pygame.mixer.Sound('Sounds/fuel.wav')
start_fx = pygame.mixer.Sound('Sounds/start.mp3')
restart_fx = pygame.mixer.Sound('Sounds/restart.mp3')
coin_fx = pygame.mixer.Sound('Sounds/coin.mp3')
pygame.mixer.music.load('Sounds/mixkit-tech-house-vibes-130.mp3')
pygame.mixer.music.play(loops=-1)
pygame.mixer.music.set_volume(0.6)
# OBJECTS *********************************************************************
road = Road()
nitro = Nitro(WIDTH-80, HEIGHT-80)
p = Player(100, HEIGHT-120, car_type)
tree_group = pygame.sprite.Group()
coin_group = pygame.sprite.Group()
fuel_group = pygame.sprite.Group()
obstacle_group = pygame.sprite.Group()
# VARIABLES *******************************************************************
home_page = True
car_page = False
game_page = False
over_page = False
move_left = False
move_right = False
nitro_on = False
sound_on = True
counter = 0
counter_inc = 1
speed = 3
dodged = 0
coins = 0
cfuel = 100
endx, enddx = 0, 0.5
gameovery = -50
running = True
while running:
win.fill(BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE or event.key == pygame.K_q:
running = False
if event.key == pygame.K_LEFT:
move_left = True
if event.key == pygame.K_RIGHT:
move_right = True
if event.key == pygame.K_n:
nitro_on = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
move_left = False
if event.key == pygame.K_RIGHT:
move_right = False
if event.key == pygame.K_n:
nitro_on = False
speed = 3
counter_inc = 1
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
if nitro.rect.collidepoint((x, y)):
nitro_on = True
else:
if x <= WIDTH // 2:
move_left = True
else:
move_right = True
if event.type == pygame.MOUSEBUTTONUP:
move_left = False
move_right = False
nitro_on = False
speed = 3
counter_inc = 1
if home_page:
win.blit(home_img, (0,0))
counter += 1
if counter % 60 == 0:
home_page = False
car_page = True
if car_page:
win.blit(select_car, (center(select_car), 80))
win.blit(cars[car_type], (WIDTH//2-30, 150))
if la_btn.draw(win):
car_type -= 1
click_fx.play()
if car_type < 0:
car_type = len(cars) - 1
if ra_btn.draw(win):
car_type += 1
click_fx.play()
if car_type >= len(cars):
car_type = 0
if play_btn.draw(win):
car_page = False
game_page = True
start_fx.play()
p = Player(100, HEIGHT-120, car_type)
counter = 0
if over_page:
win.blit(end_img, (endx, 0))
endx += enddx
if endx >= 10 or endx<=-10:
enddx *= -1
win.blit(game_over_img, (center(game_over_img), gameovery))
if gameovery < 16:
gameovery += 1
num_coin_img = font.render(f'{coins}', True, WHITE)
num_dodge_img = font.render(f'{dodged}', True, WHITE)
distance_img = font.render(f'Distance : {counter/1000:.2f} km', True, WHITE)
win.blit(coin_img, (80, 240))
win.blit(dodge_img, (50, 280))
win.blit(num_coin_img, (180, 250))
win.blit(num_dodge_img, (180, 300))
win.blit(distance_img, (center(distance_img), (350)))
if home_btn.draw(win):
over_page = False
home_page = True
coins = 0
dodged = 0
counter = 0
nitro.gas = 0
cfuel = 100
endx, enddx = 0, 0.5
gameovery = -50
if replay_btn.draw(win):
over_page = False
game_page = True
coins = 0
dodged = 0
counter = 0
nitro.gas = 0
cfuel = 100
endx, enddx = 0, 0.5
gameovery = -50
restart_fx.play()
if sound_btn.draw(win):
sound_on = not sound_on
if sound_on:
sound_btn.update_image(sound_on_img)
pygame.mixer.music.play(loops=-1)
else:
sound_btn.update_image(sound_off_img)
pygame.mixer.music.stop()
if game_page:
win.blit(bg, (0,0))
road.update(speed)
road.draw(win)
counter += counter_inc
if counter % 60 == 0:
tree = Tree(random.choice([-5, WIDTH-35]), -20)
tree_group.add(tree)
if counter % 270 == 0:
type = random.choices([1, 2], weights=[6, 4], k=1)[0]
x = random.choice(lane_pos)+10
if type == 1:
count = random.randint(1, 3)
for i in range(count):
coin = Coins(x,-100 - (25 * i))
coin_group.add(coin)
elif type == 2:
fuel = Fuel(x, -100)
fuel_group.add(fuel)
elif counter % 90 == 0:
obs = random.choices([1, 2, 3], weights=[6,2,2], k=1)[0]
obstacle = Obstacle(obs)
obstacle_group.add(obstacle)
if nitro_on and nitro.gas > 0:
x, y = p.rect.centerx - 8, p.rect.bottom - 10
win.blit(nitro_frames[nitro_counter], (x, y))
nitro_counter = (nitro_counter + 1) % len(nitro_frames)
speed = 10
if counter_inc == 1:
counter = 0
counter_inc = 5
if nitro.gas <= 0:
speed = 3
cou
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
demo.zip (51个子文件)
Car Racing 2d
main.py 9KB
objects.py 5KB
app.png 203KB
Assets
road.png 26KB
barrel.png 7KB
car_dodge.png 5KB
coins
3.png 2KB
1.png 2KB
6.png 2KB
5.png 2KB
4.png 1KB
2.png 2KB
game_over.png 94KB
home.png 203KB
cars
3.png 10KB
1.png 10KB
6.png 12KB
5.png 11KB
4.png 12KB
8.png 32KB
7.png 9KB
2.png 9KB
roadblock.png 4KB
bg.png 304KB
nitro.png 3KB
fuel.png 3KB
nitro
0.gif 3KB
5.gif 2KB
2.gif 2KB
3.gif 2KB
1.gif 3KB
4.gif 2KB
horn.png 53KB
buttons
soundOn.png 5KB
arrow.png 689B
home.png 7KB
play.png 7KB
replay.png 8KB
soundOff.png 3KB
trees
3.png 3KB
1.png 3KB
4.png 4KB
2.png 4KB
end.jpg 538KB
Sounds
mixkit-tech-house-vibes-130.mp3 3.12MB
restart.mp3 38KB
fuel.wav 491KB
coin.mp3 24KB
start.mp3 44KB
click.mp3 58KB
__pycache__
objects.cpython-310.pyc 7KB
共 51 条
- 1
资源评论
听风吹等浪起
- 粉丝: 1w+
- 资源: 1189
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功