Python 在pygame中显示新项目时遇到问题

Python 在pygame中显示新项目时遇到问题,python,pygame,Python,Pygame,我一直在pygame中编写一个类似青蛙的游戏。要做到这一点,我需要有日志/项目,玩家可以登陆到屏幕的另一边。我试图使这些日志以75、150或225像素的随机宽度生成。左边的产卵机制一直在工作,但奇怪的是右边正在创建比预期产卵大得多的日志。对于如何确保这些原木以正确的宽度繁殖,任何帮助都将不胜感激 以下是我目前的代码: import sys, pygame, random from pygame.locals import * pygame.init() screen_height = 750

我一直在pygame中编写一个类似青蛙的游戏。要做到这一点,我需要有日志/项目,玩家可以登陆到屏幕的另一边。我试图使这些日志以75、150或225像素的随机宽度生成。左边的产卵机制一直在工作,但奇怪的是右边正在创建比预期产卵大得多的日志。对于如何确保这些原木以正确的宽度繁殖,任何帮助都将不胜感激

以下是我目前的代码:

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

pygame.init()
screen_height = 750
screen_width = 1200
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 + 11
player_rect.top = screen_height - 68

not_in_ocean = False

#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 = [0, 600]
y_logs = [74, 149, 224, 299, 374, 449, 524, 599] 
list_log_width = [1, 2, 3]
log_height = 74
logs_created = []
log_speeds = []
for y in y_logs:
    log_speeds.append(random.randint(1,3))


class Log():

    def __init__(self, x, y, direction, log_width, speed):
        self.direction = direction
        self.drew_new_log = False
        self.log_width = log_width
        self.speed = speed
        self.log = pygame.Rect(x, y, log_width * 75, log_height)
        self.log_length = random.randint(0,2)
        self.y_index = y_logs.index(self.log.y)

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

    def draw_log(self):
        pygame.draw.rect(screen, (153, 102, 0), self.log)


    def delete_log(self, item):
        logs_created.remove(item)

    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' and self.drew_new_log == False:
            if self.log.right  > screen_width:
                logs_created.append(Log((-75 * list_log_width[self.log_length]) + 1, self.log.y, 'right', list_log_width[self.log_length], log_speeds[self.y_index]))
                self.drew_new_log = True
        if self.direction == 'left' and self.drew_new_log == False:
            if self.log.left < 0:
                logs_created.append(Log(screen_width - 1, self.log.y, 'left', list_log_width[self.log_length], log_speeds[self.y_index]))
                self.drew_new_log = True

for x in x_logs:
    for y in y_logs:
        for speed in log_speeds:
            log_length = random.randint(0, 2)
            if (y_logs.index(y) % 2) == 0: 
                logs_created.append(Log(x, y, 'left', list_log_width[log_length], log_speeds[y_logs.index(y)]))#list_log_width[log_length], speed))
            else:
                logs_created.append(Log(x, y, 'right', list_log_width[log_length], log_speeds[y_logs.index(y)])) #list_log_width[log_length], speed)) 


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))
    ocean = pygame.draw.rect(screen, (0, 119, 190), (0, screen_height / 10, screen_width, screen_height *0.8))

    FPSCLOCK = pygame.time.Clock()

    not_in_ocean = False


    for log in logs_created:
        log.draw_log()
        log.move_log()
        log.draw_new_logs()
        print(log.log_width*75)


        if log.direction == 'right':
            if log.log.centerx - ((log.log_width * 75) / 2) > screen_width:
                log.delete_log(log)
        if log.direction == 'left':
            if (log.log.right) < 0:# + (log.log_width * 75)) < 0:
                log.delete_log(log)


        if player_rect.colliderect(log.log):
            not_in_ocean = True
            if log.log_width == 3:

                if abs(player_rect.centerx - log.log.right) > abs(player_rect.centerx - log.log.left) and abs(player_rect.centerx - log.log.left) < abs(player_rect.centerx - log.log.centerx):
                    #Put the player on the left side
                    player_rect.centerx = log.log.left + 37.5

                if abs(player_rect.centerx - log.log.centerx) < abs(player_rect.centerx - log.log.right) and abs(player_rect.centerx - log.log.centerx) < abs(player_rect.centerx - log.log.left):
                    #Put the player in the middle
                    player_rect.centerx = log.log.centerx

                if abs(player_rect.centerx - log.log.right) < abs(player_rect.centerx - log.log.left) and abs(player_rect.centerx - log.log.centerx) > abs(player_rect.centerx - log.log.right):
                    #Put the player on the right side
                    player_rect.centerx = log.log.right - 37.5

            if log.log_width == 2:
                if abs(player_rect.centerx - log.log.right) > abs(player_rect.centerx - log.log.left):
                    #Put the player on the left side
                    player_rect.centerx = log.log.left + 37.5

                if abs(player_rect.centerx - log.log.right) < abs(player_rect.centerx - log.log.left):
                    #Put the player on the right side
                    player_rect.centerx = log.log.right - 37.5

            if log.log_width == 1:
                player_rect.centerx = log.log.centerx

            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    if event.key == K_RIGHT:
                        player_rect.centerx += 75
                    if event.key == K_LEFT:
                        player_rect.x -= 75
                    if event.key == K_UP:
                        player_rect.y -= 75
                    if event.key == K_DOWN:
                        player_rect.y += 75


            if log.direction == 'right':
                player_rect.x += log_speeds[log.y_index]
            if log.direction == 'left':
                player_rect.x -= log_speeds[log.y_index]
        elif starting_area.colliderect(player_rect) or finish_area.colliderect(player_rect):
            not_in_ocean = True

    #Gameover mechanism
    if not_in_ocean == False:
        player_rect.left = 300 + 11
        player_rect.top = screen_height - 68

    screen.blit(player, player_rect)

    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 > 11:
            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 - 11:
            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 > 11:
            if left_movements < 75:
                player_rect.x -= 15
                left_movements += 15
            else:
                left_movements = 0
                left_movement = False
    if right_movement == True:
        if player_rect.right <= screen_width - 11:
            if right_movements < 75:
                player_rect.x += 15
                right_movements += 15
            else:
                right_movements = 0
                right_movement = False

    if player_rect.left < 0 or player_rect.right > screen_width:
        #Gameover
        player_rect.left = 300 + 11
        player_rect.top = screen_height - 68


    pygame.display.update()
    FPSCLOCK.tick(FPS)
导入系统、pygame、随机
从pygame.locals导入*
pygame.init()
屏幕高度=750
屏幕宽度=1200
screen=pygame.display.set_模式((屏幕宽度、屏幕高度))
pygame.display.set_标题(“青蛙”)
FPS=200
player=pygame.image.load('frog.bmp')
player\u rect=player.get\u rect()
玩家右左=300+11
player_rect.top=屏幕高度-68
不在海洋中=错误
#用于球员移动
向上移动=0
向下移动=0
向左移动=0
右移=0
向上移动=错误
向下移动=错误
向左移动=错误
右移=假
x_日志=[0600]
y_日志=[74149224299374449524599]
列表\日志\宽度=[1,2,3]
原木高度=74
创建的日志=[]
日志速度=[]
对于y/U日志中的y:
log_速度。追加(random.randint(1,3))
类日志():
定义初始(自身、x、y、方向、对数宽度、速度):
方向
self.draw\u new\u log=False
self.log\u width=log\u width
自身速度=速度
self.log=pygame.Rect(x,y,对数宽度*75,对数高度)
self.log_length=random.randint(0,2)
self.y_index=y_logs.index(self.log.y)
def移动日志(自身):
如果self.direction=='right':
self.log.x+=self.speed
self.log.right+=self.speed
如果self.direction==“left”:
self.log.x-=self.speed
self.log.right-=self.speed
def绘图日志(自身):
pygame.draw.rect(屏幕,(153102,0),self.log)
def delete_日志(自身,项目):
已创建日志。删除(项)
def draw_new_logs(self):#为了解决日志中无限繁殖的问题,将if语句放在主游戏循环中,只有在满足要求时才让它运行此方法
如果self.direction='right'和self.draw\u new\u log==False:
如果self.log.right>屏幕宽度:
日志创建.append(日志(-75*列表日志宽度[self.Log\u长度])+1,self.Log.y,“右”,列表日志宽度[self.Log\u长度],日志速度[self.y\u索引])
self.draw\u new\u log=True
如果self.direction='left'和self.draw\u new\u log==False:
如果self.log.left<0:
日志\u创建.append(日志(屏幕\u宽度-1,self.Log.y,'左',列表\u日志\u宽度[self.Log\u长度],日志\u速度[self.y\u索引])
self.draw\u new\u log=True
对于x英寸x_日志:
对于y/U日志中的y:
对于对数速度中的速度:
log_length=random.randint(0,2)
如果(y_.index(y)%2)==0:
创建日志。追加(日志(x,y,'左',列表日志宽度[日志长度],日志速度[日志索引(y)])。#列表日志宽度[日志长度],速度))
其他:
创建日志。追加(日志(x,y,'右',列表日志宽度[日志长度],日志速度[日志索引(y)])。#列表日志宽度[日志长度],速度))
尽管如此:
屏幕填充((0、119、190))
起始面积=pygame.draw.rect(屏幕,(32178170),(0675,屏幕宽度,屏幕高度/10))
完成面积=pygame.draw.rect(屏幕,(32178170),(0,0,屏幕宽度,屏幕高度/10))
ocean=pygame.draw.rect(屏幕,(0,119,190),(0,屏幕高度/10,屏幕宽度,屏幕高度*0.8))
fpscall=pygame.time.Clock()
不在海洋中=错误
对于已创建的登录日志,请执行以下操作:
log.draw_log()
log.move_log()
log.draw_new_logs()
打印(对数.对数宽度*75)
如果log.direction=='right':
如果log.log.centerx-((log.log_宽度*75)/2)>屏幕宽度:
日志。删除日志(日志)
如果log.direction==“left”:
如果(log.log.right)<0:#+(log.log_width*75))<0:
日志。删除日志(日志)
如果player_rect.collide rect(log.log):
不在海洋中=正确
如果log.log_width==3:
如果abs(player_rect.centerx-log.log.right)>abs(player_rect.centerx-log.log.left)和abs(player_rect.centerx-log.log.left)abs(player_rect.centerx-log.log.right):
#把球员放在右边
player_rect.centerx=log.log.right-37.5
如果log.log_width==2:
如果abs(player_rect.centerx-log.log.right)>abs(player_rect.centerx-log.log.left):
#把球员放在左边
player_rect.centerx=log.log.left+37.5
如果abs(player_rect.centerx-log.log.right)