为什么我的球员跳出角落?

时间:2014-08-09 07:55:02

标签: python pygame

很抱歉,如果这是一个重复的问题,我也想提前为我的代码的可怕结构道歉。我只是为了一个爱好编程,并开始这个程序只是为了在我正在研究的另一个游戏中使用的随机地图。这很可怕我知道我没有课,甚至没有一个功能。我真的只是在探索如何制作一张随机地图,它变成了如何滚动这张地图,我如何让它适合屏幕,然后当我碰到两侧时该怎么办...我真的被困在在我把它变成我真正想要的是角落之前。我可以四处移动我的播放器但是如果我向左侧移动然后向上移动到一个角落,我最终会回到屏幕中间,而不是在屏幕的中央顶部滚动。我想提前感谢您的帮助,这里是代码。

import random
import pygame

#Make the random map------------------------------------------------------------------------------------------
#Set constants for the height and width of the tiles on the map
TILE_H = 60
TILE_W = 60

tree_tile = pygame.image.load('forrest.jpg')
tree_tile = pygame.transform.scale(tree_tile,(TILE_W,TILE_H))
grass_tile = pygame.image.load('grass.jpg')
grass_tile = pygame.transform.scale(grass_tile,(TILE_W,TILE_H))
#Create surface tiles to populate the map
#grass_tile = pygame.Surface((TILE_W,TILE_H))
water_tile = pygame.Surface((TILE_W,TILE_H))
#tree_tile = pygame.Surface((TILE_W,TILE_H))
sand_tile = pygame.Surface((TILE_W,TILE_H))
hill_tile = pygame.Surface((TILE_W,TILE_H))

#Pick colors for the tiles
#grass_tile.fill((0,255,0))
water_tile.fill((0,0,255))
#tree_tile.fill((30,180,0))
sand_tile.fill((255,255,10))
hill_tile.fill((150,80,10))


#Randomly pick the order of the tiles in rows, and columns. Then store in the list tile_list.
tile_list = []
def map_row():
    row = []
    for i in range(0,256):
        x = random.randint(1,100)
        if 0 < x < 11:
            tile = water_tile
        elif 10 < x < 21:
            tile = hill_tile
        elif 20 < x < 36:
            tile = sand_tile
        elif 35 < x < 61:
            tile = tree_tile
        elif 60 < x < 101:
            tile = grass_tile
        else:
            print('out of range')

        row.append(tile)

    tile_list.append(row)

def map_column():
    for i in range(0,256):
        map_row()

map_column()
#End of random map maker-------------------------------------------------------------------------------------

#Initiate pygame, set up the screen and clock for FPS
pygame.init()
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
clock = pygame.time.Clock()

#Set up constants for screen height and width
SCREEN_H = screen.get_height()
SCREEN_W = screen.get_width()

#Make a surface for the whole map and blit the tiles to it
map_surface = pygame.Surface(( len(tile_list[0])*TILE_W, len(tile_list)*TILE_H))

for y,row in enumerate(tile_list):
    for x,tile_surface in enumerate(row):
        map_surface.blit(tile_surface,(x*TILE_W,y*TILE_H))

#find a random starting position for the map on the screen then blit the screen to that position
map_posX = random.randrange((-(TILE_W * len(tile_list))+SCREEN_W),0,TILE_W)
map_posY = random.randrange((-(TILE_H * len(tile_list))+SCREEN_H),0,TILE_H)
screen.blit(map_surface,(map_posX,map_posY))
pygame.display.update()

#Make a player surface the same size of the screen
player = pygame.Surface((TILE_H,TILE_W))
player.fill((0,0,0))

edge_t = False
edge_b = False
edge_l = False
edge_r = False
#Main game loop------------------------------------------------------------------------------------------------
while True:

     # get the rect for the map and screen for use in calculations   
    map_rect = map_surface.get_rect()
    screen_rect = screen.get_rect()
    if edge_t == False and edge_b == False and edge_l == False and edge_r == False:
        player_rect = player.get_rect()


    #If they close the window quit out of pygame
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    #Assign stuff to keys, like if up is pressed move up   
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        #Move the map up if not at top and player not at the bottom
        if map_posY < -TILE_H and edge_b == False:
            map_posY += TILE_H
            screen.blit(map_surface,(map_posX,map_posY))
            edge_t = False
        elif map_rect.contains(player_rect):
            #Figure out where the player is at when they hit the top
            #Unless they are already on an edge
            if edge_t == False and edge_b == False and edge_l == False and edge_r == False:
                player_rect.bottom = (adjust_x+SCREEN_H/2)+1
                player_rect.left = (adjust_y+SCREEN_W/2)-1
                screen.blit(map_surface,(map_posX,map_posY))
                player_tile_list_posY -= 1
                edge_t = True #Toggle edge_t

            #Move the player while not centered
            elif edge_t  or edge_b  or edge_l  or edge_r :
                #Don't let it move off the map
                if player_rect.top > TILE_H:
                    player_rect = player_rect.move(0,-TILE_H)
                    screen.blit(map_surface,(map_posX,map_posY))
                    player_tile_list_posY -= 1

                #If leaving bottom edge turn edge_b off
                if edge_b  and player_rect.bottom < ((SCREEN_H/2)+TILE_H):
                    edge_b = False


    elif keys[pygame.K_DOWN]:
        if map_posY > (-(TILE_H * len(tile_list))+SCREEN_H+TILE_H) and edge_t == False:
            map_posY -= TILE_H
            screen.blit(map_surface,(map_posX,map_posY))
            edge_b = False

        elif map_rect.contains(player_rect):
            #Figure out where the player is at when they hit the bottom
            #Unless they are already on an edge
            if edge_b == False and edge_t == False and edge_l == False and edge_r == False:
                player_rect.bottom = (adjust_x+SCREEN_H/2)+1
                player_rect.left = (adjust_y+SCREEN_W/2)-1
                screen.blit(map_surface,(map_posX,map_posY))
                player_tile_list_posY += 1
                edge_b = True #Toggle edge_d

            #Move the player while not centered
            elif edge_b  or edge_t  or edge_l  or edge_r :
                if player_rect.bottom <= SCREEN_H-TILE_H:
                    player_rect = player_rect.move(0,TILE_H)
                    screen.blit(map_surface,(map_posX,map_posY))
                    player_tile_list_posY += 1
            #If leaving top edge turn edge_t off
            if edge_t  and player_rect.bottom >= SCREEN_H/2:
                    edge_t = False


    elif keys[pygame.K_LEFT]:
        if map_posX < -TILE_W and edge_r == False:
            map_posX += TILE_W
            screen.blit(map_surface,(map_posX,map_posY))
            edge_l = False
        elif map_rect.contains(player_rect):
            #Figure out where the player is at when they hit the left
            #Unless they are already on an edge
            if edge_t == False and edge_b == False and edge_l == False and edge_r == False:
                player_rect.top = (adjust_x+SCREEN_H/2)+1
                player_rect.left = (adjust_y+SCREEN_W/2)-1
                screen.blit(map_surface,(map_posX,map_posY))
                player_tile_list_posY -= 1
                edge_l = True #Toggle edge_l

            #Move the player while not centered
            elif edge_l  or edge_r  or edge_b  or edge_t :
                #Don't let it move off the map
                if player_rect.left > TILE_W:
                    player_rect = player_rect.move(-TILE_W,0)
                    screen.blit(map_surface,(map_posX,map_posY))
                    player_tile_list_posX -= 1

                #If leaving right edge turn edge_r off
                if edge_r  and player_rect.right < ((SCREEN_W/2)+TILE_W):
                    edge_r = False

    elif keys[pygame.K_RIGHT]:
        if map_posX > (-(TILE_W * len(tile_list[0]))+SCREEN_W+TILE_W) and edge_l == False:
            map_posX -= TILE_W
            screen.blit(map_surface,(map_posX,map_posY))
            edge_r = False
        elif map_rect.contains(player_rect):
            #Figure out where the player is at when they hit the right
            #Unless they are already on an edge
            if edge_t == False and edge_b == False and edge_l == False and edge_r == False:
                player_rect.top = (adjust_x+SCREEN_H/2)+1
                player_rect.left = (adjust_y+SCREEN_W/2)-1
                screen.blit(map_surface,(map_posX,map_posY))
                player_tile_list_posX -= 1
                edge_r = True #Toggle edge_r

            #Move the player while not centered
            elif edge_l  or edge_r  or edge_b  or edge_t :
                #Don't let it move off the map
                if player_rect.right <= SCREEN_W - TILE_W:
                    player_rect = player_rect.move(TILE_W,0)
                    screen.blit(map_surface,(map_posX,map_posY))
                    player_tile_list_posX -= 1

                #If leaving left edge turn edge_l off
                if edge_l  and player_rect.left > ((SCREEN_W/2)+TILE_W):
                    edge_l = False

    elif keys[pygame.K_ESCAPE]:
        pygame.quit()
    elif keys[pygame.K_RETURN]:
        print(edge_t)
        print(edge_l)

        if tile_list[player_tile_list_posY][player_tile_list_posX] == water_tile:
            player.fill((100,0,0))
        if tile_list[player_tile_list_posY][player_tile_list_posX] == grass_tile:
            print('Grass')
        if tile_list[player_tile_list_posY][player_tile_list_posX] == hill_tile:
            print('Hill')
        if tile_list[player_tile_list_posY][player_tile_list_posX] == sand_tile:
            print('Sand')
        if tile_list[player_tile_list_posY][player_tile_list_posX] == tree_tile:
            print('Tree')

    #Blit the player to the center of the screen and alligned with the tiles
    #If your not at the edge of the map
    if edge_t == False and edge_b == False and edge_l == False and edge_r == False:
        adjust_x = ((SCREEN_W/2)%TILE_W)-TILE_W
        adjust_y = ((SCREEN_H/2)%TILE_H)-TILE_H

        #Figure out what kind of tile they are standing on
        player_tile_list_posX = int(-(map_posX-((SCREEN_W+adjust_x)/2))/TILE_W)
        player_tile_list_posY = int(-(map_posY-((SCREEN_H+adjust_y)/2))/TILE_H)

        screen.blit(player, (adjust_x+SCREEN_W/2,adjust_y+SCREEN_H/2))


    if edge_t  or edge_b or edge_l  or edge_r :
        screen.blit(player,player_rect)

    #Update the display and check that the program is running at 20 FPS
    pygame.display.update()
    clock.tick(20)

1 个答案:

答案 0 :(得分:0)

再次抱歉这个程序写得多乱,我更多地玩pygame试图学习如何使用它比什么都重要。在过去的几天里,我一直很忙,一看到这个,答案就让我脸红了。我故意这样做,所以如果玩家已经在顶部或底部,玩家将不会重新进入,但这是我更改了edge_x的布尔值的相同代码块。因此,我所要做的就是返回并添加一个if语句,以确保它已经变为true,如果它已经位于不同的边缘。