繁体   English   中英

图像之间的Y值是否意外变化?

[英]Y Value Between Images Changes Unexpectedly?

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

MEGA下载图像和SFX: 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()

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

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

该测试为7 10,因为该测试是在Y位置> 700(窗口高度)时进行的。 因此,您也可以在此运动中一臂之力。

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

在另外35个步骤中, boat-X2Y2触底,返回-450。 此时, boat-XY已前进350步(从-100开始)到位置250,使距离为700

依此类推。 随着窗口大小的改变,这种效果也会改变。

我认为这是一个很有趣的效果。 当然,这不是您想要的,但是它在船上融合了一些“发条随机性”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM