我正在嘗試重新編碼我小時候在一臺非常舊的 PC 上玩的游戲。為此,我可能需要一些謎語或邏輯愛好者的幫助。原理很簡單:
視窗右側是一個“汽車”(五個類似汽車結構的矩形),可以分三行移動,即頂線、中間線和底線。其他汽車從這三行之一的視窗左側駛來,并朝著右側的汽車移動。玩家必須移動右邊的汽車,以免它與來自左邊的汽車發生碰撞。
從左邊來的汽車被保存在一個串列中。如果一輛車移出框架,則會產生另一輛車。所有的汽車都是用一個遍歷汽車陣列的for回圈來模擬的。
但我似乎無法找到一種演算法來在正確的路線上生成新車。有一些限制要確保,汽車有可能通過。
例如,這是不允許的:

為了防止這種情況,我使用了這個條件:
if cars[-1].line != cars[-2].line:
pssble=[carss[-1].line,cars[-2].line]
cars.append(Auto(pssble[random.randint(0,1)])
所以,如果最后兩輛車不在同一行,第三輛車必須在其中一排,否則會發生上圖所示的事情。但可能會發生其他事情。這:

我現在可以繼續解釋任何事情的每一種可能性,但我懷疑它對任何人都有幫助。如果可以,請告訴我,我將描述更多。
雖然我找到了讓汽車不擋路的可能性,但它間接迫使所有汽車排成兩條線,這樣玩家就不必做出任何動作。這也不是我想要的。
綜上所述,游戲要么因為汽車被堵而無法進行,要么很無聊,因為你永遠不必做任何事情。
如果有人對如何解決這個問題有任何想法,或者遇到這個問題的邏輯謎題,我會很高興有一個遮陽篷。我將把整個代碼放在下面。
提前致謝
編碼:
#Setup
import pygame, sys, random, time
from pygame.locals import *
from pygame import mixer
import math
pygame.init()
pygame.font.init()
infobj=pygame.display.Info()
WINDOWWIDTH= 1000#infobj.current_w
WINDOWHEIGHT=400#infobj.current_h
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE=(255, 255, 255)
BROWN=(139, 69, 19)
RED=(255,0,0)
Grey=(179,179,179)
clock=0.06
MOVESPEED=8
carHeight=3 #As in current line of the car
autos=[] #the list of cars
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
auto=pygame.image.load("Auto.png")
auto=pygame.transform.scale(auto,(int(WINDOWWIDTH/5),int(WINDOWHEIGHT/3)))
playerrect=auto.get_rect()
playerrect.bottom=WINDOWHEIGHT/carHeight
playerrect.left=0
global windowSufarce
class Auto:
def __init__(self, line):
self.line=line
self.auto=pygame.image.load("Auto.png")
self.auto=pygame.transform.scale(auto,(int(WINDOWWIDTH/5),int(WINDOWHEIGHT/3)))
self.autorect=auto.get_rect()
self.autorect.bottom=(WINDOWHEIGHT/3)*self.line
self.autorect.left=WINDOWWIDTH
def sim(self):
self.autorect.left=self.autorect.left-MOVESPEED
windowSurface.blit(self.auto,self.autorect)
#makes two cars at the beginning
autos.append(Auto(random.randint(1,3)))
autos.append(Auto(random.randint(1,3)))
while True:
windowSurface.fill(WHITE)
playerrect.bottom=(WINDOWHEIGHT/3)*carHeight
windowSurface.blit(auto, playerrect)
if autos[-1].autorect.right<=WINDOWWIDTH-4 and autos[-1].autorect.right>=WINDOWWIDTH-10:
if something: #THIS IS THE PROBLEM. This is where I need the conditions for the car. All my tries, even consisting of up to 6 if-statements failed.
autos.append(Auto(LINE))
for car in autos:
try:
car.sim()
except:
pass
if playerrect.colliderect(car.autorect):
print("Collision detected. Exiting")
sys.exit()
pygame.display.update()
time.sleep(clock)
#Keyboard stuff
pressed=pygame.key.get_pressed()
if (pressed[pygame.K_w] or pressed[pygame.K_UP]) and carHeight>1:
carHeight=carHeight-1
if (pressed[pygame.K_s] or pressed[pygame.K_DOWN]) and carHeight<3:
carHeight=carHeight 1
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
uj5u.com熱心網友回復:
感謝The_spider和fana的評論,我找到了解決方案。我在問題中解釋的情況仍然存在。該類Auto已被修改,可以傳遞一個附加引數,將 Car 標記為可見或不可見。每四輛產生的汽車被定義為不可見,并加載空白影像,這些汽車的碰撞檢測也被停用。
通過這個解決方案,上圖的情況被上述條件避免了,下圖的情況和所有其他擋車的可能性都被每四個汽車大小的空間是空的,所以玩家有能力在其他汽車周圍移動。為了確保沒有巧合的長長的自由路區域,汽車在玩家線中產生的概率大大增加。
如果有人需要任何進一步的資訊或我的代碼(無論出于何種原因,我不知道),請隨時回復我。
謝謝你的協助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/479300.html
上一篇:將整數N并行劃分為M個部分
