如何为此游戏制作算法?

发布于 2025-01-30 06:08:20 字数 3574 浏览 2 评论 0原文

我正在尝试重新编码我小时候在一台非常旧的PC上玩过的游戏。为此,我可能需要一些Riddle或Logic-thumists的帮助。原理非常简单:

在窗口的右侧是“汽车”(五个矩形,类似于汽车的结构),可以将其移动三行,顶线,中间线和底线。其他汽车以这三条线之一的一条从窗户的左侧出现,并且正朝右边的汽车移动。玩家必须在右边移动汽车,以免与左边的汽车相撞。

来自左侧的汽车保存在列表中。如果一辆汽车移出框架,另一辆车将产生。所有汽车均通过一系列汽车的前面迭代进行模拟。

但是我似乎无法找到一种算法来在正确的行中产生新车。有一些限制可以确保汽车有可能通过。

例如,不允许这样做:

为了防止这种情况,我使用了这种情况:

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()

I am trying to re-code a game which I played as a child on a very old PC. For that, I probably need the help of some riddle- or logic-enthusiasts. The principle is very simple:

On the right side of the window is a "car" (five rectangles resembling a car-like structure) which can be moved in three lines, the top line, the middle line and the bottom line. Other cars are coming from the left of the window in one of these three lines, and are moving towards the car on the right. The player has to move the car on the right, so that it doesn't collide with the cars coming from the left.

The cars coming from the left are saved in a list. If one car moves out of frame, another car is spawned. All cars are simulated with a for-loop iterating through the array of cars.

But I can't seem to figure out an algorithm to spawn the new cars in the right line. There are some restrictions to ensure, that the car has the possibility to get through.

This for example is not allowed:
enter image description here

To prevent this, I used this condition:

if cars[-1].line != cars[-2].line:
            pssble=[carss[-1].line,cars[-2].line] 
            cars.append(Auto(pssble[random.randint(0,1)])

So, if the last two cars were NOT in the same line, the third one has to be in one of these rows, otherwise, something like the picture above will happen.
But something else can happen. This:
enter image description here

I could now continue to explain every possibility of anything, but I doubt it would help anybody. If it would, please tell me and I will describe more of them.

Although I found a possibility to make the cars not block the way, it indirectly forced all cars in two lines, so that the player didn't ever had to make any move. That's also not what I want.

So summed up, either the game is impossible because the cars get blocked, or it is boring, because you never have to do anything.

If anybody has an idea on how to solve this, or is into the kind of logic-riddle this question is, I would be happy for an awnser. I will put the whole code below.

Thanks in Advance

The Code:


#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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2025-02-06 06:08:20

多亏了the_spider和fana的评论,我找到了解决方案。我在问题中解释的状况存在。类auto已修改,可以传递一个附加参数,该参数将汽车视为可见或隐形。每四辆汽车产卵的定义是无形的,并且加载了空白图像,这些汽车也会停用碰撞检测。

通过此解决方案,上述情况避免了上图的情况,以及下图和阻止汽车的所有其他可能性,避免了每四个汽车大小的空间是空的,因此玩家具有在其他汽车上移动的能力。为了确保没有巧合的自由途径很长,在玩家线中产卵的可能性大大增加。

如果有人需要进一步的信息或我的代码(无论出于何种原因,我都不知道),请随时回到我身边。

感谢您帮助我!

Thanks to the comments of The_spider and fana, I found a Solution. The condition I explained in my question stays. The class Auto has been modified, an additional parameter can be passed which flags the Car as visible or invisible. Every fourth car spawned is defined as being invisible, and a blank image is loaded, the collision detection is also deactivated for these cars.

Through this solution, the situation of the upper picture gets avoided by the mentioned condition, and the situation in the lower picture and all other possibilities of blocking the car gets avoided by the fact the every fourth car-sized space is empty, so the player has the ability to move around the other cars. To make the sure that there are no long areas of free way by coincidence, the probability of a car spawning in the players line is increased massively.

If anyone needs any further information or my code (for whatever reason, I don't know), feel free to come back at me.

Thanks for helping me out!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文