【问题标题】:Pygame: Creating a borderPygame:创建边框
【发布时间】:2016-10-07 19:45:36
【问题描述】:

我一直在研究我的学校项目,这是一款带有可移动播放器的 2D 游戏。我希望在屏幕边缘创建一个边框。我已经阅读了其他几篇文章,但似乎无法完全理解它。如果有人可以帮助我,那就太好了。这是我的程序

import pygame
import os

black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)


class Player(object):  
    def __init__(self):
        self.image = pygame.image.load("player1.png")
        self.image2 = pygame.transform.flip(self.image, True, False) 
        self.coffee = pygame.image.load("coffee.png")
        self.computer = pygame.image.load("computer.png")
        self.background = pygame.image.load("background.png")
        self.flipped = False
        self.x = 0
        self.y = 0


    def handle_keys(self):
        """ Movement keys """
        key = pygame.key.get_pressed()
        dist = 5
        if key[pygame.K_DOWN]: 
            self.y += dist 
        elif key[pygame.K_UP]: 
            self.y -= dist 
        if key[pygame.K_RIGHT]: 
            self.x += dist
            self.flipped = False
        elif key[pygame.K_LEFT]:
            self.x -= dist
            self.flipped = True

    def draw(self, surface):
        if self.flipped:
            image = self.image2
        else:
            image = self.image
        surface.blit(self.background,(0,0))
        surface.blit(self.coffee, (725,500))
        surface.blit(self.computer,(15,500))
        surface.blit(image, (self.x, self.y))


pygame.init()

screen = pygame.display.set_mode((800, 600))    #creates the screen

player = Player() 
clock = pygame.time.Clock()

running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()      # quit the screen
            running = False

    player.handle_keys()       # movement keys
    screen.fill((255,255,255)) # fill the screen with white
    player.draw(screen)        # draw the player to the screen
    pygame.display.update()    # update the screen

    clock.tick(60)             # Limits Frames Per Second to 60 or less

【问题讨论】:

    标签: python python-2.7 pygame


    【解决方案1】:

    你可以做的是,你可以为每个宽度和高度添加 20 个像素,并在周围绘制一系列正方形。

    screen = pygame.display.set_mode((800 + 20, 600 + 20))
    
    
    for x in range(0, 810, 10)
        pygame.draw.rect(surface, BLACK, [x, 0, 10, 10])
        pygame.draw.rect(surface, BLACK, [x, 610, 10, 10])
    
    for x in range(0, 610, 10)
        pygame.draw.rect(screen, BLACK, [0, x, 10, 10])
        pygame.draw.rect(screen, BLACK, [810, x, 10, 10])
    

    【讨论】:

    • 为什么不pygame.draw.rect(surface, BLACK, [0, 0, 820, 10]);等?
    • 它似乎不起作用。我应该在源代码的哪个位置添加语法?
    • 好的,我把它放在了 player.draw(screen) 之前的 while 循环中。它完美地绘制了矩形,但它们不充当边界。有什么建议吗?
    • 您必须限制玩家的移动,使其只能在边界内的空间内移动。在过去的 24 小时内,您就同一脚本提出了许多问题。也许最好看一个教程,学习基础知识,然后问你是否不明白。这里有一些很好的教程:Richard Jones presentation on programming games with pygamepygame official tutorials
    【解决方案2】:

    尝试在player.draw(screen) 之前将其添加到您的主要部分:

    pygame.init()
    
    line_width = 10
    width = 800
    height = 600
    #creates the screen
    screen = pygame.display.set_mode((width+line_width, height+line_width),0,32)    
    
    screen.fill(white) # fill the screen with white
    # top line
    pygame.draw.rect(screen, black, [0,0,width,line_width])
    # bottom line
    pygame.draw.rect(screen, black, [0,height,width,line_width])
    # left line
    pygame.draw.rect(screen, black, [0,0,line_width, height])
    # right line
    pygame.draw.rect(screen, black, [width,0,line_width, height+line_width])
    

    【讨论】:

    • 我把它放在那里。程序绘制了边界矩形,但是当我按住箭头键时玩家不会移动,我必须不断点击箭头键才能让玩家移动。边界也不起作用,因为我仍然可以移出屏幕。我也尝试将代码放在循环之外,但这也不起作用。请帮忙。
    • 那么你在谈论一个完全不同的问题。绘图不会限制运动。
    猜你喜欢
    • 2022-10-25
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多