gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:31:27 26 4
gpt4 key购买 nike

我正在开发一款 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com