У меня есть игра с 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()