python - 如何显示在类中创建的矩形?

标签 python pygame

我正在开发一款 Frog 游戏,并试图为玩家创建登录日志。为此,我决定编写一个可以为所有创建的日志执行此操作的类。我无法弄清楚如何显示这些形状,因为我无法使用 blit 方法。任何帮助将不胜感激。

到目前为止,这是我的代码:

import sys, pygame, random
from pygame.locals import *

pygame.init()
screen_height = 750
screen_width = 750
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Frogger")
FPS = 200

player = pygame.image.load('frog.bmp')
player_rect = player.get_rect()
player_rect.left = 300 + 1
player_rect.top = screen_height - 74

#For player movement
up_movements = 0
down_movements = 0
left_movements = 0 
right_movements = 0
up_movement = False
down_movement = False
left_movement = False
right_movement = False

x_logs = [1, 301, 601]
y_logs = [149, 224, 299, 374, 449, 524, 599, 674]
log_width = 74
log_height = 74
logs_created = []

class Log():

    def __init__(self, x, y, direction):
        self.x = x
        self.y = y
        self.direction = direction
        self.log = pygame.draw.rect(screen, (153, 102, 0),(self.x, self.y, log_width, log_height))

    def move_log(self):
        if self.direction == 'right':
            self.x += 5
        if self.direction == 'left':
            self.x -= 5
        self.log

    def draw_new_logs(self): # To address the issue of infinitely spawning in logs, put the if statements in the main game loop and only have it run this method if it meets the requirements
        if self.direction == 'right':
            if self.log.right > screen_width:
                logs_created.append(Log(-73, self.y, log_width, log_height))
            #Delete log if it exceeds boundary
            if self.log.left > screen_width:
                logs_created.remove(self.log)

        if self.direction == 'left':
            if self.log.left < 0:
                logs_created.append(Log(749, self.y, log_width, log_height))
            #Delete log if it exceeds boundary  
            if self.log.right < 0:
                logs_created.remove(self.log)


for x in x_logs:
    for y in y_logs:
        if (y_logs.index(y) % 2) == 0: 
            logs_created.append(Log(x, y, 'left'))
        else:
            logs_created.append(Log(x, y, 'right')) 

while True:
    screen.fill((0, 119, 190))
    starting_area = pygame.draw.rect(screen, (32, 178, 170), (0, 675, screen_width, screen_height / 10))
    finish_area = pygame.draw.rect(screen, (32, 178, 170), (0,0, screen_width, screen_height / 10))
    screen.blit(player, player_rect)

    FPSCLOCK = pygame.time.Clock()

    for item in logs_created:
        item.move_log()

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == KEYDOWN:
            if event.key == K_UP:
                up_movement = True
            elif event.key == K_DOWN:
                down_movement = True
            elif event.key == K_LEFT:
                left_movement = True
            elif event.key == K_RIGHT:
                right_movement = True

        #Movements
    if up_movement == True:
        if player_rect.top > 1:
            if up_movements < 75:
                player_rect.y -= 15
                up_movements += 15
            else:
                up_movements = 0
                up_movement = False
        else:
            up_movement = False
            up_movements = 0
    if down_movement == True:
        if player_rect.bottom <= screen_height - 1:
            if down_movements < 75:
                player_rect.y += 15
                down_movements += 15
            else:
                down_movements = 0
                down_movement = False
        else:
            down_movement = False
            down_movements = 0

    if left_movement == True:
        if player_rect.left > 1:
            if left_movements < 75:
                player_rect.x -= 15
                left_movements += 15
                print(player_rect.left)
            else:
                left_movements = 0
                left_movement = False
    if right_movement == True:
        if player_rect.right <= screen_width - 1:
            if right_movements < 75:
                player_rect.x += 15
                right_movements += 15
            else:
                right_movements = 0
                right_movement = False

    pygame.display.update()
    FPSCLOCK.tick(FPS)

最佳答案

pygame.draw.rect()不构造某种可绘制对象(如 Surface),它在表面上绘制一个矩形。你必须使用 pygame.draw.rect() 而不是 pygame.Surface.blit(),而不是 pygame.image.load().
pygame.draw.rect()的返回值是 pygame.Rect目的。 pygame.Rect 仅包含矩形的位置和大小。
不是在 Log 的构造函数中绘制一个矩形,而是必须向类 Log 添加一个 draw 方法,它在屏幕:

class Log():

    def __init__(self, x, y, direction):
        self.x = x
        self.y = y
        self.direction = direction

    def move_log(self):
        if self.direction == 'right':
            self.x += 5
        if self.direction == 'left':
            self.x -= 5

    def draw(self):
        pygame.draw.rect(screen, (153, 102, 0),(self.x, self.y, log_width, log_height))

在主应用程序循环之前,将初始 Log 对象添加到列表 logs_created:

x_logs = [1, 301, 601]
y_logs = [149, 224, 299, 374, 449, 524, 599, 674]
logs_created = []

for x in x_logs:
    for y in y_logs:
        logs_created.append(Log(x, y, 'right'))

在主应用程序循环中移动并绘制 logs_created 中的所有 Log 对象:

while True:
    screen.fill((0, 119, 190))
    starting_area = pygame.draw.rect(screen, (32, 178, 170), (0, 675, screen_width, screen_height / 10))
    finish_area = pygame.draw.rect(screen, (32, 178, 170), (0,0, screen_width, screen_height / 10))
    screen.blit(player, player_rect)

    # draw logs
    for log in logs_created:
        log.draw()

    # [...]

    # move logs
    for log in logs_created:
        log.move()

关于python - 如何显示在类中创建的矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61191620/

相关文章:

python - AttributeError 'NoneType' 对象没有属性 'upload_from_filename'

python - 使用 Anaconda 安装 pygame

python - 在运行 python 3.8 的机器上用于 python 3.7 的 distutils 和 pygame

python - 如何使用一个循环添加到多个组

python - PyGame wormy - 线程与坏苹果

Python 请求 : URL with percent character

python - Google Dataflow 流式插入 BigQuery 达到速率限制

python - next 方法对于 python 迭代器来说是引用透明的吗?

python - bin 一列并对 (2,N) 数组的另一列求和

python - 如何在 pygame 中设置鼠标围绕圆的位置