撞墙问题

问题描述 投票:0回答:1

我的设计是 0 和 1 的数组,1 是墙壁,0 是地板,但是对于我的一生,我不知道我做错了什么来正确地让它像我所做的一切一样工作只是打破它

import random
import pygame
from pygame.locals import *
pygame.init()

#constants
display_info = pygame.display.Info()
x = display_info.current_w
y = display_info.current_h
WIDTH = x
HEIGHT = y
TILE_SIZE = 20
ROWS = HEIGHT // TILE_SIZE
COLS = WIDTH // TILE_SIZE
pygame.display.set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0)
window = pygame.display.set_mode((WIDTH, HEIGHT))

# Initializing the clock to control frame rate
clock = pygame.time.Clock()

# creating a variable to check the direction
# of movement
direction = True

#player sprite
image = pygame.image.load(r'PidleF1.png')
image = pygame.transform.scale(image, (64, 64))

# initial cords.
# two variables i.e. x and y.
ix = x//2
iy = y//2

# velocity of player's movement
velocity = 12

# Define colors
BLACK = (0, 0, 0)
GREY = (127, 127, 127)

#dungeon grid
dungeon = [[1 for y in range(ROWS)] for x in range(COLS)]

# Generate rooms
for i in range(75):
    x = random.randint(1, COLS - 10)
    y = random.randint(1, ROWS - 10)
    w = random.randint(5, 10)
    h = random.randint(5, 10)
    for dx in range(w):
        for dy in range(h):
            dungeon[x + dx][y + dy] = 0

# Connect the rooms
# TODO: Find a pathfinding algorithm to connect rooms?

# Render dungeon
sprites = pygame.sprite.Group()
for x in range(COLS):
    for y in range(ROWS):
        if dungeon[x][y] == 1:
            sprite = pygame.sprite.Sprite()
            sprite.image = pygame.Surface((TILE_SIZE, TILE_SIZE))
            sprite.image.fill(BLACK)
            sprite.rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
            sprites.add(sprite)
        else:
            sprite = pygame.sprite.Sprite()
            sprite.image = pygame.Surface((TILE_SIZE, TILE_SIZE))
            sprite.image.fill(GREY)
            sprite.rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
            sprites.add(sprite)
run = True
while run:
    # Set frame rate
    clock.tick(60)

    # Display the player sprite coordinates
    # Flipping the player sprite if player
    # changes the direction
    if direction:
        window.blit(image, (ix, iy))
    if not direction:
        window.blit(pygame.transform.flip(image, True, False), (ix, iy))

    # iterate over the list of Event objects
    # that was returned by pygame.event.get() method.
    for event in pygame.event.get():

        # Closing the window and program if the
        # type of the event is QUIT
        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            quit()

        # Changing the value of the
        # direction variable
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                direction = True
            elif event.key == pygame.K_LEFT:
                direction = False

    # Storing the key pressed 
    key_pressed_is = pygame.key.get_pressed()

    # Changing the coordinates
    # of the player
    if key_pressed_is[K_LEFT]:
        ix -= 5
        print('left')
    if key_pressed_is[K_RIGHT]:
        ix += 5
        print('right')
    if key_pressed_is[K_UP]:
        iy -= 5
        print('up')
    if key_pressed_is[K_DOWN]:
        iy += 5
    print('down')
    # Draws the surface object to the screen.
    pygame.display.update()

    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    # Update the screen
    sprites.draw(window)
    pygame.display.flip()

我已经尝试过我在网上找到的不同的东西,但大多数人看不到我正在尝试做的事情

python pygame collision-detection
1个回答
-1
投票

我对您的代码进行了一些优化,可能会有帮助。例如,您的代码可能会中断,因为它包含一个必要的循环,也因为它呈现了一个无效的精灵。

尝试下面的解决方案。

import random
import pygame
from pygame.locals import *

pygame.init()

#constants
display_info = pygame.display.Info()
x = display_info.current_w
y = display_info.current_h
WIDTH = x
HEIGHT = y
TILE_SIZE = 20
ROWS = HEIGHT // TILE_SIZE
COLS = WIDTH // TILE_SIZE

pygame.display.set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0)
window = pygame.display.set_mode((WIDTH, HEIGHT))

# Initializing the clock to control frame rate
clock = pygame.time.Clock()

# creating a variable to check the direction
# of movement
direction = True

#player sprite
image = pygame.image.load(r'PidleF1.png')
image = pygame.transform.scale(image, (64, 64))

# initial cords.
# two variables i.e. x and y.
ix = x//2
iy = y//2

# velocity of player's movement
velocity = 12

# Define colors
BLACK = (0, 0, 0)
GREY = (127, 127, 127)

#dungeon grid
dungeon = [[1 for y in range(ROWS)] for x in range(COLS)]

# Generate rooms
for i in range(75):
    x = random.randint(1, COLS - 10)
    y = random.randint(1, ROWS - 10)
    w = random.randint(5, 10)
    h = random.randint(5, 10)
    for dx in range(w):
        for dy in range(h):
            dungeon[x + dx][y + dy] = 0

# Connect the rooms
# TODO: Find a pathfinding algorithm to connect rooms?

# Render dungeon
sprites = pygame.sprite.Group()
for x in range(COLS):
    for y in range(ROWS):
        if dungeon[x][y] == 1:
            sprite = pygame.sprite.Sprite()
            sprite.image = pygame.Surface((TILE_SIZE, TILE_SIZE))
            sprite.image.fill(BLACK)
            sprite.rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
            sprites.add(sprite)
        else:
            sprite = pygame.sprite.Sprite()
            sprite.image = pygame.Surface((TILE_SIZE, TILE_SIZE))
            sprite.image.fill(GREY)
            sprite.rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
            sprites.add(sprite)

# game loop
run = True
while run:
    # Set frame rate
    clock.tick(60)

    # iterate over the list of Event objects
    # that was returned by pygame.event.get() method.
    for event in pygame.event.get():
        # Closing the window and program if the
        # type of the event is QUIT
        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            quit()

        # Changing the value of the
        # direction variable
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                direction = True
            elif event.key == pygame.K_LEFT:
                direction = False
© www.soinside.com 2019 - 2024. All rights reserved.