【问题标题】:Y Value Between Images Changes Unexpectedly?图像之间的 Y 值意外变化?
【发布时间】:2019-01-22 22:14:04
【问题描述】:

我有一个有 4 条车道的游戏——船要从上到下成对移动,你的角色(海豚)应该通过按 A 和 D 键来避开它们。我将第一艘船设定在 Y -100,第二艘船在 Y -450。当您运行游戏时,船之间的距离会随着每次再次在顶部重生而发生变化。为什么会这样?以我的理解,两艘船之间的距离应该始终保持在 350。有人可以帮帮我吗?

图像和音效的巨型下载:https://mega.nz/fm/zHohiSJD

import pygame
import random
import os

os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" % (0, 20)  # positions the game tab to the top left portion of the monitor
pygame.init()  # initializes pygame
pygame.display.set_caption("Biomagnification ")
SIZE = W, H = 400, 700  # determining the screen size of the game
screen = pygame.display.set_mode(SIZE)  # creating a display surface
clock = pygame.time.Clock()  # creating a clock

# RGB colours
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BACKGROUND = (94, 194, 222)
STRIPE = (60, 160, 190)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# variables
x1 = 30  # desired X position for the left dolphin/hitbox
x2 = 330  # desired X position for the right dolphin/hitbox
lane1 = 30
lane2 = 130
lane3 = 230
lane4 = 330
y = 530  # desired y value of the hitbox
width, height = (40, 64)  # width and height of the hitbox

toggle1 = 0  # toggle for left dolphin
toggle2 = 0  # toggle for right dolphin

target_x1 = 30
target_x2 = 330
vel_x = 10  # speed at which the dolphins move


def drawScene():
    screen.fill(BACKGROUND)  # fills the background with the colour ((94, 194, 222)
    pygame.draw.polygon(screen, STRIPE,
                        ((200, 700), (300, 700), (400, 600), (400, 500)))  # draws the different coloured stripes
    pygame.draw.polygon(screen, STRIPE, ((0, 700), (100, 700), (400, 400), (400, 300)))
    pygame.draw.polygon(screen, STRIPE, ((0, 500), (0, 600), (400, 200), (400, 100)))
    pygame.draw.polygon(screen, STRIPE, ((0, 300), (0, 400), (400, 0), (300, 0)))
    pygame.draw.polygon(screen, STRIPE, ((0, 100), (0, 200), (200, 0), (100, 0)))
    pygame.draw.line(screen, WHITE, (100, 0), (100, 700), 2)  # draws the white separating lines in the background
    pygame.draw.line(screen, WHITE, (200, 0), (200, 700), 6)
    pygame.draw.line(screen, WHITE, (300, 0), (300, 700), 2)


# dolphin spritesheet
mainsheet = pygame.image.load("mainsheetSmall.png").convert()  # load in the spritesheet
sheetSize = mainsheet.get_size()  # gets the size of the spritesheet
horiz_cells = 36  # number of horizontal frames/cells in the spritesheet
vert_cells = 1  # number of vertical frames/cells in the spritesheet
cell_width = int(sheetSize[0] / horiz_cells)  # determining the width of each cell
cell_height = int(sheetSize[1] / vert_cells)  # determing the height of each cell

cellList = []  # creates a list for all the cells
for vert in range(0, sheetSize[1], cell_height):
    for horz in range(0, sheetSize[0], cell_width):
        surface = pygame.Surface((cell_width, cell_height))
        surface.blit(mainsheet, (0, 0),
                     (horz, vert, cell_width, cell_height))
        colorkey = surface.get_at((0, 0))  # gets the colour at O, 0 (white)
        surface.set_colorkey(colorkey)  # removes all the white from the spritesheet, making the background transparent
        cellList.append(surface)  # appends to the list of cells (cellList)

cellPosition = 0  # determines which frame is playing. Starts at 0

船代码

# boat
boatSpeed = 10
boat = pygame.image.load("boat.png").convert_alpha()
boatX = random.choice([lane1, lane2, lane3, lane4])
if boatX == lane1:
    boatX2 = random.choice([lane3, lane4])
elif boatX == lane2:
    boatX2 = random.choice([lane3, lane4])
else:
    boatX2 = random.choice([lane1, lane2])
boatY = -100

boatX3 = random.choice([lane1, lane2, lane3, lane4])
if boatX3 == lane1:
    boatX4 = random.choice([lane3, lane4])
elif boatX3 == lane2:
    boatX4 = random.choice([lane3, lane4])
else:
    boatX4 = random.choice([lane1, lane2])
boatY2 = -450

.

# main loop
while True:
    clock.tick(60)  # makes the game tick 60 frames per second

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:  # detects if the user presses the ESC key
                pygame.quit()
            elif event.key == pygame.K_a:  # detects if the user presses the A key
                pygame.mixer.music.load('percussiveHit.mp3')
                pygame.mixer.music.play()
                toggle1 += 1  # adds 1 to toggle1
                if toggle1 % 2 == 1:  # if toggle1 mod 2 equals 1
                    target_x1 += 100  # add 100 to the target position
                else:
                    target_x1 -= 100  # if toggle1 mod 2 does not equal 1, subtract 100 from the target position making it the original of 30
            elif event.key == pygame.K_d:  # detects if the user presses the D key
                pygame.mixer.music.load('percussiveHit.mp3')
                pygame.mixer.music.play()
                toggle2 += 1  # adds 1 to toggle2
                if toggle2 % 2 == 1:  # if toggle2 mod 2 equals 1
                    target_x2 -= 100  # subtract 100 the to target position
                else:
                    target_x2 += 100  # if toggle2 mod 2 does not equal 1, add 100 to the target position making it the original of 330
    if x1 < target_x1:
        x1 = min(x1 + vel_x, target_x1)
    else:
        x1 = max(x1 - vel_x, target_x1)

    if x2 < target_x2:
        x2 = min(x2 + vel_x, target_x2)
    else:
        x2 = max(x2 - vel_x, target_x2)

    if cellPosition < len(cellList) - 1:
        cellPosition += 1
    else:
        cellPosition = 0

船的第二位代码

    boatY += boatSpeed
    if boatY > H:
        boatX == random.choice([lane1, lane2, lane3, lane4])
        if boatX == lane1:
            boatX2 == random.choice([lane3, lane4])
        elif boatX == lane2:
            boatX2 == random.choice([lane3, lane4])
        else:
            boatX2 == random.choice([lane1, lane2])
        boatY = -100

    boatY2 += boatSpeed
    if boatY2 > H:
        boatX3 == random.choice([lane1, lane2, lane3, lane4])
        if boatX3 == lane1:
            boatX4 == random.choice([lane3, lane4])
        elif boatX3 == lane2:
            boatX4 == random.choice([lane3, lane4])
        else:
            boatX3 == random.choice([lane3, lane4])
        boatY2 = -450

    # pygame.draw.rect(screen, GREEN, (x1, y, width, height))  # hitbox of left dolphin
    pygame.draw.rect(screen, GREEN, (x2, y, width, height))  # hitbox of right dolphin
    drawScene()  # draws the scene (background, lane-lines, etc)
    # players
    screen.blit(cellList[cellPosition], (x1 + 4, y - 1))  # the left dolphin
    screen.blit(cellList[cellPosition], (x2 + 4, y - 1))  # the right dolphin
    screen.blit(boat, (boatX, boatY))
    screen.blit(boat, (boatX2, boatY))
    screen.blit(boat, (boatX3, boatY2))
    screen.blit(boat, (boatX4, boatY2))
    # screen.blit(boat, (boatX5, boaY3))
    # screen.blit(boat, (boatX6, boatY4))

    pygame.display.update()

【问题讨论】:

  • 您好 Patrick,欢迎来到 Stack Overflow。我认为最好花时间提出Minimal, Complete, and Verifiable example 并发布它而不是所有代码。就您而言,如果您发现创建 MCVE 太难或不可能,您的问题可能更适合专门用于完整代码审查的网站。

标签: python image pygame sprite variable-assignment


【解决方案1】:

boat-XY 从 -100 开始
boat-X2Y2 从 -450 开始
那是 350 的距离。
每一步/循环,船移动 10 (Y += 10)。

经过 80 步(取决于窗口大小,当前为 700),boat-XY 离开屏幕,从位置 710 移动到 -100。

测试是 710,因为测试是在 Y 位置 > 大于 700(窗口高度)时。所以你也会在这个动作中得到一个不一样的结果。

boat-XY 重新启动时,boat-X2Y2 现在位于位置 -450 + 810 → 360。所以现在距离为 460

再经过 35 步,boat-X2Y2 触底,返回 -450。此时,boat-XY 已经前进了 350 步(从 -100)到位置 250,使得距离 700

等等。随着窗口大小的变化,这种效果也会发生变化。

我认为这是一个非常有趣的效果。当然这不是你想要的,但它在船上加入了一些“发条随机性”。

【讨论】:

  • 这绝对不是我想要的,好像你玩的时间够长,4艘船都会围成一堵墙,无法通过。我该如何阻止这种情况发生,并让距离保持在 350?
  • 只需检查一下以确保情况并非如此。如果,移动另一艘船(最好还是在屏幕外)if boatY == boatY2: boatY2 -= random.randint(100,400)。或者添加一个新控件,让海豚可以“跳”船。
  • 就像 if boatY == boatY2: boatY2 -= random.randint(100,400) 的想法。有没有办法让船不在同一个 Y 坐标上?例如,如果boatY2 在距离boatY 200 以内,有没有办法重置它?
  • 类似if abs( boatY - boatY2 ) &gt; 200: boatY2 -= random.randint(200,400) 的东西?这应该将它移开至少 200 距离。
猜你喜欢
  • 1970-01-01
  • 2013-11-03
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多