-
Notifications
You must be signed in to change notification settings - Fork 0
/
startingRacing.py
383 lines (304 loc) · 16 KB
/
startingRacing.py
1
import pygame, random, Spell, Object_racing, drawobject, DEFINE, time, addRankingimport history, datetimeSCREEN_WIDTH = 800SCREEN_HEIGHT = 600def kiemtrahieulucbua(car): i = 0 if (len(car.bua) != 0): if ((car.bua)[0] == DEFINE.TANG_TOC): # bua tang toc gom 3 giai doan: tang dan toc do , di chuyen thang deu voi toc do cao , giam dan ve toc do ban dau if ((car.bua)[ 1] >= 250): # neu nhu da het hieu luc cua bua, o day ta cho hieu luc cua bua tang toc la 300 vong lap if (car.speed <= (car.bua)[2]): car.bua = [] return car else: car.speed = car.speed - (car.bua)[2] / 150 elif ((car.bua)[1] >= 150 and (car.bua)[1] < 250): pass else: car.speed = car.speed + (car.bua)[2] / 150 elif ((car.bua)[0] == DEFINE.DUNG_LAI): # bua dunglai gom 3 giai doan: giam dan ve 0, dung yen tai cho, tang toc di tiep if ((car.bua)[1] >= 400): # giai doan 3: tang dan toc do cua xe tro ve toc do ban dau truoc khi gap bua if car.speed >= (car.bua)[2]: car.bua = [] return car else: car.speed = car.speed + (car.bua)[2] / 100 elif ((car.bua)[1] >= 100 and (car.bua)[1] < 400): # giai doan 2: dung yen tai cho car.speed = 0 else: # giai doan 1: giam dan toc do cua xe den khi ve 0 car.speed = car.speed - (car.bua)[2] / 100 # bua[2] la cai van toc truoc khi gap bua elif ((car.bua)[0] == DEFINE.VE_DICH): if (car.bua[1] <=50): car.speed = (car.bua[3] - car.bua[6] - car.width)/50 # car.speed = 0.5 else: # car.bua[0] = 0 car.speed = DEFINE.firstspeed return car elif (car.bua[0] == DEFINE.VE_VACHXUATPHAT): if (car.bua[1] <= 50): car.speed = -(car.bua[6] - car.bua[5] + car.width) / 50 else: car.speed = car.bua[2] elif (car.bua[0] == DEFINE.QUAY_DAU): if (car.bua[1] == 0): car.picture = pygame.transform.rotate(car.picture, 180) car.speed = -car.bua[2] elif (car.bua[1] == 400): car.picture = pygame.transform.rotate(car.picture, 180) car.speed = car.bua[2] # tang cai bua[1] them 1 don vi (car.bua)[1] += 1 # sau 1 vong lap thi tang i i += 1 return cardef accountchange(money, rank): if(rank == 1): return money elif(rank==2): return -money*0.25 elif (rank == 3): return -money * 0.5 elif (rank == 4): return -money * 0.75 elif (rank == 5): return -money return 0def pos_set_object(Main_object, object_to_set): # day la ham dat lai vi tri cua vat bat ky sau moi lan update man hinh, sau do ve vat ra dung ham khac object_to_set.posx += object_to_set.speed - Main_object.speed return object_to_setdef anmung(racer,y_bandau): if (racer.change <= 20 or (racer.change <=90 and racer.change >=50) or (racer.change <=170 and racer.change >=130)or (racer.change <=250 and racer.change >=210) or (racer.change <=310 and racer.change >=290)): racer.posy += 0.5 elif(racer.change >310): racer.posy = y_bandau else: racer.posy -= 0.5 return racerdef racing_running(win,list_settings, maincar ,money,name_player,HistoryList,road_length,map, taikhoan,passSpell_active): retur = -5 # back ground bla bla background_image = pygame.image.load('picture/map/' + str(map+1) + '-background.png').convert_alpha() # convert background cho do nang # foreground khong convert duoc vi no chuyen khoang trang => khong thay duoc foreground_image = pygame.image.load('picture/map/' + str(map+1) + '-foreground.png').convert_alpha() # Load 2 hinh ke nhau de tao cam giac lien tuc backgroundX = 0 backgroundX2 = background_image.get_width() background_speed = 0 # Sound # Select based on map game_music = ["sounds/ultraviolet.mp3", "sounds/ethereal.mp3","sounds/it_makes_me_smile.mp3"] main_win_music = "sounds/retro.mp3" main_lost_music = "sounds/time_for_rag.mp3" pygame.mixer.music.stop() # Load music according to map pygame.mixer.music.load(game_music[map]) pygame.mixer.music.play(-1) win_sound = False # "Play car win or lose music yet?" flag #startlline,goal startline = pygame.image.load("picture/map/start.png") goal = pygame.image.load("picture/map/start.png") # khoi tao thong so cho road y_road_start = 200 road_width = 300 # khoi tao thong so cho vach ve dich, via he x_startline = 150 x_goal = x_startline + road_length # x bat dau cua dich # tao ra mot danh sach cac racer Racers = [] Racers.append(Object_racing.Racer(x_startline - 55, 215, list_settings[0], list_settings[1][0],list_settings[2][0],'0')) Racers.append(Object_racing.Racer(x_startline - 55, 275, list_settings[0], list_settings[1][1],list_settings[2][1],'1')) Racers.append(Object_racing.Racer(x_startline - 55, 335, list_settings[0], list_settings[1][2],list_settings[2][2],'2')) Racers.append(Object_racing.Racer(x_startline - 55, 395, list_settings[0], list_settings[1][3],list_settings[2][3],'3')) Racers.append(Object_racing.Racer(x_startline - 55, 455, list_settings[0], list_settings[1][4],list_settings[2][4],'4')) # thiet dat hinh anh cho cac car, thiet dat cac emojiface cho cac racer for i in range(0, 5): Racers[i].set_picture() a = 1 #dung de dem so vong lap crash = False# dung de biet khi nao thi vong lap dung #tao ra mot danh sach cac bua list_bua = Spell.createListSpell(5, x_startline + 400, x_goal) # them mot bien co mot xe ve dich comotxevedich = False xvd = -1 #khong can quan tam cai nay nhieu lam, cai nay chi phuc vu cho viec kiem tra xe nao ve dich dau tien soxedunglai = 0 x_left = Racers[0].width x_right = 0 buatamthoi = Spell.Spell(DEFINE.VE_DICH,x_goal,3) #bien nay dung cho viec bo qua bua hai dau tien chuaboquabuahaidautien = True # Block nay dung de ve cac lenh start game font = pygame.font.SysFont("comicsansms", 100) ready = font.render("READY", True, (255, 0, 127)); so1 = font.render("1", True, (255, 0, 0)) so2 = font.render("2", True, (255, 128, 0)) so3 = font.render("3", True, (51, 255, 255)) go = font.render("GOO!!!!", True, (255, 51, 255)) chu = [ready,so1,so2,so3,go] for i in range(0,5): # draw screen win.blit(background_image, (backgroundX, 0)) # Ve background 1 win.blit(background_image, (backgroundX2, 0)) # Ve background 2 win.blit(foreground_image, (backgroundX, 0)) win.blit(startline, (x_startline, y_road_start)) for j in range(0,5): Racers[j].drawCar(win) #ve chu win.blit(chu[i], (pygame.display.get_surface().get_width()/2 - chu[i].get_width() // 2, pygame.display.get_surface().get_height()/6 - chu[i].get_height() // 2)) pygame.display.update(pygame.Rect(0, 0, 800, 600)) time.sleep(1) begintime = datetime.datetime.now().now().time().hour*3600+datetime.datetime.now().time().minute*60+datetime.datetime.now().time().second while not crash: b = 0 c = 0 # check event for event in pygame.event.get(): if event.type == pygame.QUIT: crash = True # Draw background: De truoc may cai ham ve via he, duong, xe win.blit(background_image, (backgroundX, 0)) # Ve background 1 win.blit(background_image, (backgroundX2, 0)) # Ve background 2 # goal win.blit(startline,(x_startline,y_road_start)) win.blit(goal, (x_goal, y_road_start)) # drawobject.draw_goal(win, x_goal, y_road_start, road_width) #kiemtra xe ve dich, thiet dat emoji , ve xe, ve bua, kiemtra gap bua for i in range(0, 5): # kiem tra luon co xe nao ve dich khong if(Racers[i].posx >= x_goal+20 and xvd == -1 ): comotxevedich = True xvd = i for j in range(0, 5): #thiet dat cac emoji cho cac xe if j == xvd: Racers[j].emoji = Object_racing.emoji("VEDICH" + str(random.randint(1, 5)) + ".png", 0, 0) else: Racers[j].emoji = Object_racing.emoji("KHONGVEDICH" + str(random.randint(4, 8)) + ".png", 0, 0) Racers[j].emoji.set_emoji_picture() for j in list_bua[i]: #ve ra cac bua j.draw_spell(win, road_width, y_road_start,a%15) #ve ra cac xe # tao hieu ung xe tien vao lo den if (len(list_bua[i])!=0 and ((Racers[i].posx + Racers[i].width) >= list_bua[i][0].posx) and ( list_bua[i][0].type == DEFINE.VE_DICH or list_bua[i][0].type == DEFINE.VE_VACHXUATPHAT ) and Racers[i].posx <= list_bua[i][0].posx): Racers[i].drawCar(win, 0, Racers[i].width - Racers[i].x_right ) Racers[i].x_right += DEFINE.firstspeed elif (len(Racers[i].bua)!=0 and ((Racers[i].bua[0] == DEFINE.VE_DICH and Racers[i].posx < x_goal) or (Racers[i].bua[0] == DEFINE.VE_VACHXUATPHAT and Racers[i].speed<0 ))): # tao mot bua ve dich moi de ve, giai doan nay se khong draw racer if Racers[i].bua[0] == DEFINE.VE_DICH : buatamthoi.posx = x_goal buatamthoi.road = i buatamthoi.draw_spell(win, road_width, y_road_start, a % 15) Racers[i].x_left = Racers[i].width Racers[i].x_right = 0 else: if( len(Racers[i].bua)!=0 and Racers[i].posx>=x_goal-Racers[i].width-30 and Racers[i].x_left>=0 and Racers[i].bua[0] == DEFINE.VE_DICH): # vo cai nay thi chi co the la bua ve dich # tao hieu ung thoat khoi lo den #tao mot bua ve dich moi de ve buatamthoi.posx = x_goal buatamthoi.road = i buatamthoi.draw_spell(win, road_width, y_road_start,a%15) Racers[i].drawCar(win, Racers[i].x_left, Racers[i].width) Racers[i].x_left -= DEFINE.firstspeed else: Racers[i].drawCar(win,0, Racers[i].width) #cai nay la ve xe binh thuong #duyet tung bua cua moi duong trong danh sach bua va kich hoat bua luon for bua in list_bua[i]: if (((Racers[i].posx + Racers[i].width >= bua.posx) and bua.type != DEFINE.VE_DICH and bua.type != DEFINE.VE_VACHXUATPHAT) or ((Racers[i].posx >= bua.posx) and (bua.type == DEFINE.VE_DICH or bua.type == DEFINE.VE_VACHXUATPHAT))): # neu nhu nguoi dung chon bo qua bua hai dau tien thi neu ma gap bua dau tien ta khong can them no vao if (i == maincar and passSpell_active == True and len(Racers[i].bua) == 0 and (bua.type == DEFINE.DUNG_LAI or bua.type == DEFINE.QUAY_DAU or bua.type == DEFINE.VE_VACHXUATPHAT ) and chuaboquabuahaidautien ): chuaboquabuahaidautien = False # khong lam gi het else: Racers[i].bua = [bua.type, 0, DEFINE.firstspeed, x_goal, Racers[maincar].speed, x_startline, Racers[ i].posx] # them vao mot list [loai bua, hieu luc bua(that ra la so vong lap), toc do truoc khi gap bua] #xoa cai bua ma xe moi vua gap trong listbua list_bua[i].remove(bua) if (not(Racers[i].davedich)): Racers[i] = kiemtrahieulucbua(Racers[i]) # Draw foreground: De sau may cai ham ve via he, duong, xe win.blit(foreground_image, (backgroundX, 0)) # Ve foreground 1 win.blit(foreground_image, (backgroundX2, 0)) # Ve foreground 2 # neu nhu vach dich qua duoc 1/3 man hinh hoac la xe chinh chua toi duoc vi tri mot nua man hinh thi se dung lai if (x_goal <= pygame.display.get_surface().get_width() / 3 or Racers[maincar].posx <= pygame.display.get_surface().get_width()/2.5): # khong thay doi vi tri cua cac vat the nhu via he ,... # tuy nhien se tien hanh thay doi vi tri cua cac xe for i in range(0, 5): Racers[i].posx += Racers[i].speed else: # Cu moi vong lap ta cho cac vat the di chuyen lui lai voi toc do bang toc do cua xe chinh x_goal -= Racers[maincar].speed x_startline -= Racers[maincar].speed # dieu chinh toa do cac vat for i in range(0, 5): Racers[i] = pos_set_object(Racers[maincar], Racers[i]) # Cap nhat lai vi tri background, foreground theo xe chinh # (Lam nhu ham pos_set_object) backgroundX += (background_speed - Racers[maincar].speed) backgroundX2 += (background_speed - Racers[maincar].speed) # dieu chinh van toc cac bua for bua in list_bua: for i in bua: list_bua[b][c].posx -= Racers[maincar].speed c += 1 b += 1 c = 0 # den mot luc nao do thi cac xe dung lai for i in range(0,5): if (Racers[i].posx >= x_goal+300 and Racers[i].speed != 0 and Racers[i].davedich == False) : Racers[i].speed = 0 soxedunglai +=1 Racers[i].FinalRank = soxedunglai Racers[i].davedich = True #gia su co mot ve ve dich thi ta da lay duoc xe do va gan vao bien xvd #bay gio se cho xe do nhay mua if(comotxevedich): # 2 dong duoi dung de cho xe co the nhay len nhay xuong Racers[xvd] = anmung(Racers[xvd],Racers[xvd].posy) Racers[xvd].change +=1 if not win_sound: pygame.mixer.music.stop() if xvd == maincar: pygame.mixer.music.load(main_win_music) pygame.mixer.music.play(-1) else: pygame.mixer.music.load(main_lost_music) pygame.mixer.music.play(-1) win_sound = True for i in range(0, 5): # cai nay dung de gan cam xuc cho cac xe Racers[i].emoji.posx = Racers[i].posx Racers[i].emoji.posy = Racers[i].posy + Racers[i].height # ve emoji cho cac xe Racers[i].emoji.draw_emoji(win) # Cai nay de background quay lai khi da chay het if backgroundX < background_image.get_width() * -1: backgroundX = background_image.get_width() if backgroundX < background_image.get_width() * -1: backgroundX2 = background_image.get_width() #kiem tra neu như tat ca cac xe due ve dich if(soxedunglai == 5): retur = addRanking.FinalRankingScreen(win, Racers, 5, maincar, money, taikhoan) timenow = datetime.datetime.now().now().time().hour*3600+datetime.datetime.now().time().minute*60+datetime.datetime.now().time().second HistoryList.add(Racers[maincar].FinalRank, money, accountchange(money, Racers[maincar].FinalRank), road_length, str(timenow-begintime), 0) taikhoan += accountchange(money, Racers[maincar].FinalRank) # ve bang xep hang addRanking.drawTimeRankBoard(win, 500, 20, Racers, 5, maincar, money) pygame.display.update(pygame.Rect(0, 0, 800, 600)) a+=1 if(retur!= -5): break return retur, taikhoan