Мой экран прокрутки не будет прокручивать мой экран - PullRequest
0 голосов
/ 03 мая 2020

Когда мой игрок движется влево, я прокручиваю влево. Если мой игрок движется вправо, я прокручиваю вправо. Однако мой код не работает, и я не знаю, в чем проблема.

# These are my scrolling variables 
LEFT_VIEWPORT_MARGIN = 250
RIGHT_VIEWPORT_MARGIN = 250
BOTTOM_VIEWPORT_MARGIN = 50
TOP_VIEWPORT_MARGIN = 100

view_bottom = 0
view_left = 0

Это моя основная функция, в которой выполняется прокрутка. Он не имеет основных функций, таких как изображения вообще. Это все rect, но оно не позволяет мне прокручиваться, когда я двигаюсь вправо или влево.

 changed = False

 # Scroll left
 left_boundary = view_left + LEFT_VIEWPORT_MARGIN
 if player.left < left_boundary:
     view_left -= left_boundary - player.left
     changed = True

 # Scroll right
 right_boundary =  view_left + 500 - RIGHT_VIEWPORT_MARGIN
 if player.right > right_boundary:
     view_left += player.right - right_boundary
     changed = True

 # Scroll up
 top_boundary = view_bottom + 500 - TOP_VIEWPORT_MARGIN
 if player.top > top_boundary:
      view_bottom += player.top - top_boundary
      changed = True

  # Scroll down
  bottom_boundary = view_bottom + BOTTOM_VIEWPORT_MARGIN
  if player.bottom < bottom_boundary:
      view_bottom -= bottom_boundary - player.bottom
      changed = True

  if changed:
      # Only scroll to integers. Otherwise we end up with pixels that
      # don't line up on the screen
      view_bottom = int(view_bottom)
      view_left = int(view_left)

      # Do the scrolling
      arcade.set_viewport(view_left,
                          500 + view_left,
                          view_bottom,
                          500 + view_bottom)

Вот мой полный код.

import pygame
import arcade
pygame.init()


# WINDOW
window = pygame.display.set_mode((500,500))
pygame.display.set_caption("tjust a beginner trying to learn something")

# Classes for player


# Scrolling varabiales 
LEFT_VIEWPORT_MARGIN = 250
RIGHT_VIEWPORT_MARGIN = 250
BOTTOM_VIEWPORT_MARGIN = 50
TOP_VIEWPORT_MARGIN = 100

view_bottom = 0
view_left = 0
# --------------------

class players(object):
    def __init__(self,x,y,height,width):
        self.x = x
        self.y = y
        self.height = height
        self.width  = width
        self.isJump = False
        self.JumpCount = 10
        self.speed = 5
        self.fall = 0


# enemy class
class enemys(object):
    def __init__(self,cordx,cordy,cordheight,cordwidth):
        self.cordx = cordx
        self.cordy = cordy
        self.cordheight = cordheight
        self.cordwidth = cordwidth

class anotherenemy(object):
    def __init__(self,xs,ys,heights,widths):
        self.xs = xs
        self.ys = ys
        self.heights = heights
        self.widths = widths

class enemore(object):
    def __init__(self,ox,oy,oheights,ohwidths):
        self.ox = ox
        self.oy = oy
        self.oheights = oheights
        self.ohwidths = ohwidths


# FPS
FPS = 60
clock = pygame.time.Clock()


# defIne COLORS
NiceBlue = (6,214,142)
NiceGreen = (6, 214,82)


# class varbls
playerman = players(50,390,50,50)
playerenemy = enemys(150,390,100,10)
soenemy = anotherenemy(280,350,100,10)
enemyOS = enemore(50,300,100,10)

# main Loop
runninggame = True
while runninggame:

    clock.tick(FPS)


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False
    window.fill((0,0,0))
    player = pygame.draw.rect(window, (NiceGreen), (playerman.x,playerman.y,playerman.height,playerman.width))
    enemy = pygame.draw.rect(window, (NiceBlue), (playerenemy.cordx,playerenemy.cordy,playerenemy.cordheight,playerenemy.cordwidth))
    enemy2 = pygame.draw.rect(window, (NiceBlue), (soenemy.xs,soenemy.ys,soenemy.heights,soenemy.widths))
    enemy3 = pygame.draw.rect(window,(NiceBlue), (enemyOS.ox,enemyOS.oy,enemyOS.oheights,enemyOS.ohwidths))

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        playerman.x -= playerman.speed
    if keys[pygame.K_RIGHT]:
        playerman.x += playerman.speed

    if not(playerman.isJump):

        playerman.y += playerman.fall
        playerman.fall += 1
        player.topleft = (playerman.x,playerman.y)
        collide = False
        playerman.isJump = False
        if player.colliderect(enemy):
            collide = True
            playerman.isJump = False
            playerman.y = enemy.top - player.height
            if player.right > enemy.left and player.left < enemy.left - player.width:
                playerman.x = enemy.left - player.width
            if player.left < enemy.right and player.right > enemy.right + player.width:
                playerman.x = enemy.right


        if player.colliderect(enemy2):
            collide = True
            playerman.isJump = False
            playerman.JumpCount = 10
            playerman.y = enemy2.top - player.height
            if player.right > enemy2.left and player.left < enemy2.left - player.width:
                playerman.x = enemy2.left - player.width
            if player.left < enemy2.right and player.right > enemy2.right + player.width:
                playerman.x = enemy2.right

        if player.colliderect(enemy3):
            collide = True
            playerman.isJump = False
            playerman.JumpCount = 10
            playerman.y = enemy3.top - player.height
            if player.right > enemy3.left and player.left < enemy3.left - player.width:
                playerman.x = enemy3.left - player.width
            if player.left < enemy3.right and player.right > enemy3.right + player.width:
                playerman.x = enemy3.right



        if player.bottom >= 500:
            collide = True
            playerman.isJump = False
            playerman.JumpCount = 10
            playerman.y = 500 - player.height

        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
            playerman.fall = 0

    else:
        if playerman.JumpCount > 0:
            playerman.y -= (playerman.JumpCount *abs (playerman.JumpCount)) * 0.5
            playerman.JumpCount -= 1
        else:
            playerman.JumpCount = 10
            playerman.isJump = False



# -------------------------------------------------------------------------
            changed = False

        # Scroll left
        left_boundary = view_left + LEFT_VIEWPORT_MARGIN
        if player.left < left_boundary:
            view_left -= left_boundary - player.left
            changed = True

        # Scroll right
        right_boundary =  view_left + 500 - RIGHT_VIEWPORT_MARGIN
        if player.right > right_boundary:
            view_left += player.right - right_boundary
            changed = True

        # Scroll up
        top_boundary = view_bottom + 500 - TOP_VIEWPORT_MARGIN
        if player.top > top_boundary:
            view_bottom += player.top - top_boundary
            changed = True

        # Scroll down
        bottom_boundary = view_bottom + BOTTOM_VIEWPORT_MARGIN
        if player.bottom < bottom_boundary:
            view_bottom -= bottom_boundary - player.bottom
            changed = True

        if changed:
            # Only scroll to integers. Otherwise we end up with pixels that
            # don't line up on the screen
            view_bottom = int(view_bottom)
            view_left = int(view_left)

            # Do the scrolling
            arcade.set_viewport(view_left,
                                500 + view_left,
                                view_bottom,
                                500 + view_bottom)
# ----------------------------------------------------------------------------------




    pygame.display.update()

pygame.quit()
...