赛车方程式游戏,Python和Pygame游戏开发指南

  赛车方程式游戏,Python和Pygame游戏开发指南

  本文主要分享一个基于Python和Pygame的赛车游戏。文章中的样例代码讲解的很详细,对我们学习Python很有帮助。如果需要,请参考。

  00-1010简介1。环境安装2。代码显示1。主程序main.py2地图设置maps.py 3。效果展示1。游戏界面2。游戏操作3.15分。

  

目录

  你好!你好~我是木木子。好久没给你更新游戏了。3354

  有粉丝投稿称,最近因为疫情被关在家里,无法离开小区。他们不久前刚报名的驾照考试只能无线延期,在家挺无聊的。“在家没事就玩游戏写代码挺好的。

  于是,边肖灵机一动,就有了今天的简易版《赛车计划》,就是咳咳。

  通俗点就是一个代码写的关于汽车的小游戏,因为边肖的思维跟不上编程的学习!

  所以,我尽力了.我只能展示这样一个赛车游戏~有大神的话可以优化~

  

导语

  1.各种资料(图片)

  2.操作环境

  边肖的环境:Python3,Pycharm社区版,自带Pygame模块部分。

  安装:Pip Install-I https://pypi.douban.com/simple/PyGame

  

一、环境安装

  这个游戏有很多代码。这里的边肖主要展示的是主程序的代码,在我的文末会全部找到~

  

二、代码展示

  导入操作系统,系统,游戏,随机,阵列,游戏模式

  导入方向、界限、超时、菜单

  从pygame.locals导入*

  #导入游戏模块。

  从加载程序导入加载图像

  导入播放器、地图、交通、相机、曲目

  流量计数=45

  CENTER_W=-1

  中心H=-1

  #主函数。

  定义主():

  #初始化对象。

  clock=pygame.time.Clock()

  运行=真

  font=pygame.font.Font(None,24)

  汽车=玩家。玩家()

  cam=相机。照相机()

  目标=游戏模式。完成()

  bound_alert=bounds。警报()

  time_alert=超时。警报()

  信息=菜单。警报()

  指针=方向。跟踪器(int(CENTER_W * 2),int(CENTER_H * 2))

  #创建精灵组。

  map_s=pygame.sprite.Group()

  player_s=pygame.sprite.Group()

  traffic_s=pygame.sprite.Group()

  tracks_s=pygame.sprite.Group()

  target_s=pygame.sprite.Group()

  pointer_s=pygame.sprite.Group()

  timer _ alert _ s=py game . sprite . group()

  bound _ alert _ s=py game . sprite . group()

  menu _ alert _ s=py game . sprite . group()

  #生成切片

  对于范围(0,len(maps.map_tile)):中的tile_num

  map . map _ files . append(loa

  d_image(maps.map_tile[tile_num], False))

   for x in range (0, 10):

   for y in range (0, 10):

   map_s.add(maps.Map(maps.map_1[x][y], x * 1000, y * 1000, maps.map_1_rot[x][y]))

  #load tracks

   tracks.initialize()

  #load finish

   target_s.add(target)

  #load direction

   pointer_s.add(pointer)

  #load alerts

   timer_alert_s.add(time_alert)

   bound_alert_s.add(bound_alert)

   menu_alert_s.add(info)

  #load traffic

   traffic.initialize(CENTER_W, CENTER_H)

   for count in range(0, TRAFFIC_COUNT):

   traffic_s.add(traffic.Traffic())

   player_s.add(car)

   cam.set_pos(car.x, car.y)

   while running:

  #Render loop.

  #Check for menu/reset, (keyup event - trigger ONCE)

   for event in pygame.event.get():

   if event.type == pygame.KEYUP:

   if keys[K_m]:

   if (info.visibility == True):

   info.visibility = False

   else:

   info.visibility = True

   if (keys[K_p]):

   car.reset()

   target.reset()

   if (keys[K_q]):

   pygame.quit()

   sys.exit(0)

   if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:

   running = False

   break

  #Check for key input. (KEYDOWN, trigger often)

   keys = pygame.key.get_pressed()

   if (target.timeleft > 0):

   if keys[K_LEFT]:

   car.steerleft()

   if keys[K_RIGHT]:

   car.steerright()

   if keys[K_UP]:

   car.accelerate()

   else:

   car.soften()

   if keys[K_DOWN]:

   car.deaccelerate()

   cam.set_pos(car.x, car.y)

  #Show text data.

   text_fps = font.render(FPS: + str(int(clock.get_fps())), 1, (224, 16, 16))

   textpos_fps = text_fps.get_rect(centery=25, centerx=60)

   text_score = font.render(Score: + str(target.score), 1, (224, 16, 16))

   textpos_score = text_fps.get_rect(centery=45, centerx=60)

   text_timer = font.render(Timer: + str(int((target.timeleft / 60)/60)) + ":" + str(int((target.timeleft / 60) % 60)), 1, (224, 16, 16))

   textpos_timer = text_fps.get_rect(centery=65, centerx=60)

  #Render Scene.

   screen.blit(background, (0,0))

   #cam.set_pos(car.x, car.y)

   map_s.update(cam.x, cam.y)

   map_s.draw(screen)

  #Conditional renders/effects

   car.grass(screen.get_at(((int(CENTER_W-5), int(CENTER_H-5)))).g)

   if (car.tracks):

   tracks_s.add(tracks.Track(cam.x + CENTER_W, cam.y + CENTER_H, car.dir))

  #Just render..

   tracks_s.update(cam.x, cam.y)

   tracks_s.draw(screen)

   player_s.update(cam.x, cam.y)

   player_s.draw(screen)

   traffic_s.update(cam.x, cam.y)

   traffic_s.draw(screen)

   target_s.update(cam.x, cam.y)

   target_s.draw(screen)

   pointer_s.update(car.x + CENTER_W, car.y + CENTER_H, target.x, target.y)

   pointer_s.draw(screen)

  #Conditional renders.

   if (bounds.breaking(car.x+CENTER_W, car.y+CENTER_H) == True):

   bound_alert_s.update()

   bound_alert_s.draw(screen)

   if (target.timeleft == 0):

   timer_alert_s.draw(screen)

   car.speed = 0

   text_score = font.render(Final Score: + str(target.score), 1, (224, 16, 16))

   textpos_score = text_fps.get_rect(centery=CENTER_H+56, centerx=CENTER_W-20)

   if (info.visibility == True):

   menu_alert_s.draw(screen)

  #Blit Blit..

   screen.blit(text_fps, textpos_fps)

   screen.blit(text_score, textpos_score)

   screen.blit(text_timer, textpos_timer)

   pygame.display.flip()

  #Check collision!!!

   if pygame.sprite.spritecollide(car, traffic_s, False):

   car.impact()

   target.car_crash()

   if pygame.sprite.spritecollide(car, target_s, True):

   target.claim_flag()

   target.generate_finish()

   target_s.add(target)

   clock.tick(64)

  #initialization

  pygame.init()

  screen = pygame.display.set_mode((pygame.display.Info().current_w,

   pygame.display.Info().current_h),

   pygame.FULLSCREEN)

  pygame.display.set_caption(Race of Math.)

  pygame.mouse.set_visible(False)

  font = pygame.font.Font(None, 24)

  CENTER_W = int(pygame.display.Info().current_w /2)

  CENTER_H = int(pygame.display.Info().current_h /2)

  #new background surface

  background = pygame.Surface(screen.get_size())

  background = background.convert_alpha()

  background.fill((26, 26, 26))

  #Enter the mainloop.

  main()

  pygame.quit()

  sys.exit(0)

  

  

2.地图设置maps.py

  

import os, sys, pygame, math

  from pygame.locals import *

  from loader import load_image

  from random import randrange

  #Map filenames.

  map_files = []

  map_tile = [X.png, I.png, L.png, T.png, O.png, null.png]

  #Map to tile.

  crossing = 0

  straight = 1

  turn = 2

  split = 3

  deadend = 4

  null = 5

  #tilemap.

  map_1 = [

   [2,1,3,1,1,3,1,1,1,4],

   [1,5,1,5,4,0,1,2,5,4],

   [1,4,3,1,3,3,1,3,2,1],

   [3,1,3,1,3,5,4,5,1,1],

   [3,2,1,5,1,5,3,1,0,3],

   [1,2,0,1,0,3,0,4,1,1],

   [1,5,1,4,2,1,1,2,3,1],

   [1,2,0,1,3,3,0,0,2,1],

   [1,1,4,2,2,5,1,2,1,3],

   [2,3,1,3,1,1,3,1,1,2]

   ]

  #tilemap rotation, x90ccw

  map_1_rot = [

   [1,1,0,1,1,0,1,1,1,3],

   [0,0,0,0,1,0,1,0,0,0],

   [0,1,2,1,0,2,1,2,0,0],

   [1,1,0,1,3,0,0,0,0,0],

   [1,0,0,0,0,0,1,1,0,3],

   [0,2,0,1,0,0,0,3,0,0],

   [0,0,0,1,3,0,0,1,3,0],

   [0,1,0,1,0,2,0,0,3,0],

   [0,0,2,1,3,0,0,2,1,3],

   [2,2,1,2,1,1,2,1,1,3]

   ]

  class Map(pygame.sprite.Sprite):

   def __init__(self, tile_map, y, x, rot):

   pygame.sprite.Sprite.__init__(self)

   self.image = map_files[tile_map]

   self.rect = self.image.get_rect()

   if rot != 0:

   self.image = pygame.transform.rotate(self.image, rot * 90)

   self.x = x

   self.y = y

  #Realign the map

   def update(self, cam_x, cam_y):

   self.rect.topleft = self.x - cam_x, self.y - cam_y

  

  

三、效果展示

  游戏玩法:M游戏开始——P重来——Q退出游戏。跟着红色箭头运行会出现奖杯。一个奖杯15

  分,在规定时间拿到的奖杯越多数越高啦~

  

  

1.游戏界面

  

  

  

2.游戏运行中

  

  

  

3.15分到手

  

  到此这篇关于基于Python+Pygame实现经典赛车游戏的文章就介绍到这了,更多相关Python Pygame赛车游戏内容请搜索盛行IT软件开发工作室以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT软件开发工作室!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: