基于Python+Pygame實現(xiàn)經(jīng)典賽車游戲
導(dǎo)語
哈嘍!哈嘍~我是木木子,很久沒給大家更新游戲的類似啦——
有粉絲投稿,說最近由于受疫情影響封閉在家不能離開小區(qū),前不久剛剛報名的駕照考試只能無線延期,在家里還是挺無聊的,“憋在家里沒事干的時候去打打游戲?qū)憣懘a還挺好的。
于是,小編靈機一動,就有了今天這款簡易版本的《賽車計劃》也就是咳咳咳.......
通俗點兒就是一款代碼寫的一款關(guān)于汽車的小游戲,由于小編的思路跟編程的學(xué)習(xí)跟不上?。?/p>
SO,我盡力了......只能展示出這樣的一款賽車游戲~有大神的話可以優(yōu)化啦~
一、環(huán)境安裝
1.各種素材(圖片)

2.運行環(huán)境
小編使用的環(huán)境:Python3、Pycharm社區(qū)版、Pygame模塊部分自帶。
模塊安裝:pip install -i https://pypi.douban.com/simple/ pygame
二、代碼展示
這款游戲代碼特別多啦,小編這里就主要展示一下主程序運行的代碼,全部的就找我文末拿哈~
1.主程序main.py
import os, sys, pygame, random, array, gamemode
import direction, bounds, timeout, menu
from pygame.locals import *
#Import game modules.
from loader import load_image
import player, maps, traffic, camera, tracks
TRAFFIC_COUNT = 45
CENTER_W = -1
CENTER_H = -1
#Main function.
def main():
#initialize objects.
clock = pygame.time.Clock()
running = True
font = pygame.font.Font(None, 24)
car = player.Player()
cam = camera.Camera()
target = gamemode.Finish()
bound_alert = bounds.Alert()
time_alert = timeout.Alert()
info = menu.Alert()
pointer = direction.Tracker(int(CENTER_W * 2), int(CENTER_H * 2))
#create sprite groups.
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 = pygame.sprite.Group()
bound_alert_s = pygame.sprite.Group()
menu_alert_s = pygame.sprite.Group()
#generate tiles
for tile_num in range (0, len(maps.map_tile)):
maps.map_files.append(load_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.地圖設(shè)置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退出游戲。跟著紅色箭頭運行會出現(xiàn)獎杯。一個獎杯15
分,在規(guī)定時間拿到的獎杯越多數(shù)越高啦~
1.游戲界面

2.游戲運行中

3.15分到手

到此這篇關(guān)于基于Python+Pygame實現(xiàn)經(jīng)典賽車游戲的文章就介紹到這了,更多相關(guān)Python Pygame賽車游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決jupyter notebook import error但是命令提示符import正常的問題
這篇文章主要介紹了解決jupyter notebook import error但是命令提示符import正常的問題,具有很好的參考2020-04-04
selenium+python 去除啟動的黑色cmd窗口方法
今天小編就為大家分享一篇selenium+python 去除啟動的黑色cmd窗口方法。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python學(xué)習(xí)筆記之open()函數(shù)打開文件路徑報錯問題
這篇文章主要介紹了Python學(xué)習(xí)筆記之open()函數(shù)打開文件路徑報錯問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
Python中tkinter+MySQL實現(xiàn)增刪改查
這篇文章主要介紹了Python中tkinter+MySQL實現(xiàn)增刪改查,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
python GUI庫圖形界面開發(fā)之PyQt5計數(shù)器控件QSpinBox詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5計數(shù)器控件QSpinBox詳細使用方法與實例,需要的朋友可以參考下2020-02-02

