Python 如何显示在类中创建的矩形形状?
我正在制作一个蛙人游戏,并试图为玩家创建登陆日志。为此,我决定编写一个类,可以为所有创建的日志执行此操作。我很难弄清楚如何显示这些形状,因为我不能使用blit方法。任何帮助都将不胜感激 这是我目前的代码:Python 如何显示在类中创建的矩形形状?,python,pygame,Python,Pygame,我正在制作一个蛙人游戏,并试图为玩家创建登陆日志。为此,我决定编写一个类,可以为所有创建的日志执行此操作。我很难弄清楚如何显示这些形状,因为我不能使用blit方法。任何帮助都将不胜感激 这是我目前的代码: import sys, pygame, random from pygame.locals import * pygame.init() screen_height = 750 screen_width = 750 screen = pygame.display.set_mode((scre
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、随机
从pygame.locals导入*
pygame.init()
屏幕高度=750
屏幕宽度=750
screen=pygame.display.set_模式((屏幕宽度、屏幕高度))
pygame.display.set_标题(“青蛙”)
FPS=200
player=pygame.image.load('frog.bmp')
player\u rect=player.get\u rect()
玩家右左=300+1
player_rect.top=屏幕高度-74
#用于球员移动
向上移动=0
向下移动=0
向左移动=0
右移=0
向上移动=错误
向下移动=错误
向左移动=错误
右移=假
x_日志=[1301601]
y_日志=[14922429937449524599674]
原木宽度=74
原木高度=74
创建的日志=[]
类日志():
定义初始(自、x、y、方向):
self.x=x
self.y=y
方向
self.log=pygame.draw.rect(屏幕,(153102,0),(self.x,self.y,log\u宽度,log\u高度))
def移动日志(自身):
如果self.direction=='right':
self.x+=5
如果self.direction==“left”:
self.x-=5
self.log
def draw_new_logs(self):#为了解决日志中无限繁殖的问题,将if语句放在主游戏循环中,只有在满足要求时才让它运行此方法
如果self.direction=='right':
如果self.log.right>屏幕宽度:
logs_created.append(Log(-73,self.y,Log_宽度,Log_高度))
#如果日志超出边界,则删除日志
如果self.log.left>屏幕宽度:
日志\u已创建。删除(self.log)
如果self.direction==“left”:
如果self.log.left<0:
logs_created.append(Log(749,self.y,Log_宽度,Log_高度))
#如果日志超出边界,则删除日志
如果self.log.right<0:
日志\u已创建。删除(self.log)
对于x英寸x_日志:
对于y/U日志中的y:
如果(y_.index(y)%2)==0:
日志\u created.append(日志(x,y,'left'))
其他:
日志\u created.append(日志(x,y,'right'))
尽管如此:
屏幕填充((0、119、190))
起始面积=pygame.draw.rect(屏幕,(32178170),(0675,屏幕宽度,屏幕高度/10))
完成面积=pygame.draw.rect(屏幕,(32178170),(0,0,屏幕宽度,屏幕高度/10))
screen.blit(player,player_-rect)
fpscall=pygame.time.Clock()
对于创建的日志中的项目:
item.move_log()
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
elif event.type==KEYDOWN:
如果event.key==K_UP:
向上移动=真
elif event.key==K_DOWN:
向下移动=真
elif event.key==K_LEFT:
向左移动=真
elif event.key==K_RIGHT:
右移=真
#运动
如果向上移动==真:
如果player_rect.top>1:
如果向上移动小于75:
玩家右y-=15
向上移动+=15
其他:
向上移动=0
向上移动=错误
其他:
向上移动=错误
向上移动=0
如果向下移动=真:
如果玩家右下1:
如果左/右移动小于75:
玩家右x-=15
左/右移动+=15
打印(播放器右左)
其他:
向左移动=0
向左移动=错误
如果右移=真:
如果player_rect.right没有构造某种可绘制对象(如曲面
),它会在曲面上绘制一个矩形。您必须使用pygame.draw.rect()
而不是pygame.Surface.blit()
,而不是pygame.image.load()
的返回值是一个对象。pygame.Rect
只包含矩形的位置和大小。
与在Log
的构造函数中绘制矩形不同,您必须向类Log
添加一个draw
方法,该方法在屏幕上绘制一个矩形:
class Log():
定义初始(自、x、y、方向):
self.x=x
self.y=y
方向
def移动日志(自身):
如果self.direction=='right':
self.x+=5
如果self.direction==“left”:
self.x-=5
def牵引(自):
pygame.draw.rect(屏幕,(153102,0),(self.x,self.y,log\u宽度,log\u高度))
在主应用程序循环之前,将初始的Log
对象添加到创建的日志列表中:
x_日志=[1301601]
y_日志=[14922429937449524599674]
创建的日志=[]
对于x英寸x_日志:
对于y/U日志中的y:
日志\u created.append(日志(x,y,'right'))
在主应用程序循环中,移动并绘制创建的日志中的所有Log
对象:
为True时:
屏幕填充((0、119、190))
起始面积=pygame.draw.rect(屏幕,(32178170),(0675,屏幕宽度,屏幕高度/10))
完成面积=pygame.draw.rect(屏幕,(32178170),(0,0,屏幕宽度,屏幕高度/10))
screen.blit(player,player_-rect)
#画圆木
对于已创建的登录日志,请执行以下操作:
log.draw()
# [...]
#移动日志
对于已创建的登录日志,请执行以下操作:
log.move()
感谢您的及时回复和帮助