精灵图片矩形移动得太​​快

时间:2020-11-11 04:42:15

标签: python pygame

所以我正在尝试使它的水像熔岩一样以1的速度上升,但是每次我的屏幕上升时:

    # Mkaing screen go up
    if playerman.y < 250:
        playerman.y += 9
        for Platform in platforms:
            Platform.y += 9
        for Platform2 in Platforms:
            Platform2.y += 9
        for Dirt in dirts:
            Dirt.y += 9

水精灵会变快。 https://gyazo.com/1bdd7b513a06f6a80832e565255e9fb0如何做到,即使屏幕移动,水精灵也将保持相同的速度。无论我继续走多快,水都会停留在同一地点并不断上升

我的完整代码

import pygame
import random
pygame.init()

window = pygame.display.set_mode((500,500))
pygame.display.set_caption(("Noobs First Game"))

# Player class
class Player:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 6
        self.fall = 0
        self.idle = [pygame.image.load("Player_idle1.png"),
                     pygame.image.load("Player_idle2.png"),
                     pygame.image.load("Player_idle3.png"),
                     pygame.image.load("Player_idle4.png"),
                     pygame.image.load("Player_idle5.png"),
                     pygame.image.load("Player_idle6.png"),
                     pygame.image.load("Player_idle7.png"),
                     pygame.image.load("Player_idle8.png"),
                     pygame.image.load("Player_idle9.png"),
                     pygame.image.load("Player_idle10.png")]
        
        self.idlel = [pygame.image.load("Player_lidle1.png"),
                     pygame.image.load("Player_lidle2.png"),
                     pygame.image.load("Player_lidle3.png"),
                     pygame.image.load("Player_lidle4.png"),
                     pygame.image.load("Player_lidle5.png"),
                     pygame.image.load("Player_lidle6.png"),
                     pygame.image.load("Player_lidle7.png"),
                     pygame.image.load("Player_lidle8.png"),
                     pygame.image.load("Player_lidle9.png"),
                     pygame.image.load("Player_lidle10.png")]

        self.run = [pygame.image.load("Player_run1.png"),
                    pygame.image.load("Player_run2.png"),
                    pygame.image.load("Player_run3.png"),
                    pygame.image.load("Player_run4.png"),
                    pygame.image.load("Player_run5.png"),
                    pygame.image.load("Player_run6.png"),
                    pygame.image.load("Player_run7.png"),
                    pygame.image.load("Player_run8.png")]
        
        self.jump = [pygame.image.load("Player_Jump.png")]

        self.lrun = [pygame.image.load("Player_lrun1.png"),
                    pygame.image.load("Player_lrun2.png"),
                    pygame.image.load("Player_lrun3.png"),
                    pygame.image.load("Player_lrun4.png"),
                    pygame.image.load("Player_lrun5.png"),
                    pygame.image.load("Player_lrun6.png"),
                    pygame.image.load("Player_lrun7.png"),
                    pygame.image.load("Player_lrun8.png")]

        self.ljump = [pygame.image.load("Player_lJump.png")]

        self.direction = "run"
        self.direction = "jump"
        self.direction = "lrun"
        self.direction = "ljump"
        self.direction = "idle"
        self.direction = "idlel"
        self.run = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.run]
        self.jump = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.jump]
        self.lrun = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.lrun]
        self.ljump = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.ljump]
        self.idle = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.idle]
        self.idlel = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.idlel]

        self.isJump = False
        self.JumpCount = 10
        self.rect = pygame.Rect(x,y,width,height)
        self.next_frame_time = 0
        self.fps = 10
        self.clock = pygame.time.Clock()
        self.anim_index = 0
        self.jump_height = 2
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        if self.direction == "run":
            image_list = self.run
        if self.direction == "jump":
            image_list = self.jump
        if self.direction == "lrun":
            image_list = self.lrun
        if self.direction == "ljump":
            image_list = self.ljump
        if self.direction == "idle":
            image_list = self.idle
        if self.direction == "idlel":
            image_list = self.idlel
            

        # Is it time to show next frame?
        time_now = pygame.time.get_ticks()
        if (time_now > self.next_frame_time):
            # seconds till next frame
            inter_time_delay = 1000 // self.fps
            self.next_frame_time = time_now + inter_time_delay
            # switch to next frame
            self.anim_index += 1
            if self.anim_index >= len(image_list):
                self.anim_index = 0

        if self.anim_index >= len(image_list):
            self.anim_index = 0
        player_image = image_list[self.anim_index]

        pygame.draw.rect( window, self.color, self.get_rect(), 2 )
        player_image = image_list[self.anim_index]

        player_rect = player_image.get_rect(center = self.get_rect().center)
        player_rect.centerx += 3
        player_rect.centery -= 17
        window.blit(player_image, player_rect)

# Platform class
class Platform:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.log = pygame.image.load("log.png")
        self.rect = pygame.Rect(x,y,width,height)
        self.log = pygame.transform.scale(self.log,(self.log.get_width()//4,self.log.get_height()//6))
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        platform_rect = self.log.get_rect(center = self.rect.center)
        platform_rect.centerx += 60
        platform_rect.centery -= 2
        window.blit(self.log,platform_rect)


# Platform2 class
class Platform2:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.log = pygame.image.load("Grass_1.png")
        self.rect = pygame.Rect(x,y,width,height)
        self.log = pygame.transform.scale(self.log,(self.log.get_width()-90,self.log.get_height()-91))
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        platform_rect = self.log.get_rect(center = self.rect.center)
        platform_rect.centerx += 2
        platform_rect.centery += 0.2
        window.blit(self.log,platform_rect)

class Dirt:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.dirt = pygame.image.load("Dirt_1.png")
        self.dirt = pygame.transform.scale(self.dirt,(self.dirt.get_width()-90,self.dirt.get_height()-91))
        self.rect = pygame.Rect(x,y,width,height)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        dirt_rect = self.dirt.get_rect(center = self.rect.center)
        dirt_rect.centerx += 2
        dirt_rect.centery += 0.2
        window.blit(self.dirt,dirt_rect)

class Water:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 1
        self.water = pygame.image.load("water_1.png")
        self.water = pygame.transform.scale(self.water,(self.water.get_width()*8,self.water.get_height()*6))
        self.rect = pygame.Rect(x,y,width,height)
    def draw(self):
        self.rect.topleft = (self.x,self.y)

        water_rect = self.water.get_rect(center = self.rect.center)
        water_rect.centerx += 25
        water_rect.centery += 80
        window.blit(self.water,water_rect)

# displaying Color
white = (255,255,255)

# Drawing stuff

# Drawing player
playerman = Player(255,440,40,40,white)
# Drawing Platform
platform1 = Platform(0,388,130,30,white)
# Drawing Platform2
Platform1 = Platform2(7000,470,1,1,white)
# Drawing Dirt
dirt1 = Dirt(7000,255,35,35.1,white)
# Water
water1 = Water(0,800,500,300,white)
# List

# Putting Platform in a list
platforms = [platform1]
# Putting Platform2 in a list
Platforms = [Platform1]
# Putting Dirt in a list
dirts = [dirt1]

platformGroup = pygame.sprite.Group
Level = [
"  l",
"l",
"",
"  l",
"  ",
"l",
"   ",
"  l",
"   ",
"",
"",
"",
"11111111111111111111",
"22222222222222222222",]
for iy, row in enumerate(Level):
    for ix, col in enumerate(row):
        if col == "1":
            new_platform = Platform2(ix*35, iy*36.4, 35,35.1,(255, 255, 255))
            Platforms.append(new_platform)
for iy, row in enumerate(Level):
    for ix, col in enumerate(row):
        if col == "2":
            new_dirt = Dirt(ix*35, iy*36.4, 35,35.1,(255, 255, 255))
            dirts.append(new_dirt)
for iy, row in enumerate(Level):
    for ix, col in enumerate(row):
        if col == "l":
            new_platform = Platform(ix*110, iy*60, 130,30,(255, 255, 255))
            platforms.append(new_platform)
    


# redrawing window
def redrawwindow():
    window.fill((0,0,0))
    
    # bliting a counter the game
    window.blit(text,textRect)
    # showing player on the screen
    playerman.draw()

    # Drawing Platform
    for Platform in platforms:
        Platform.draw()
    # Drawing Platform2
    for Platform2 in Platforms:
        Platform2.draw()
    # Drawing Dirt
    for Dirt in dirts:
        Dirt.draw()
        # Drawing Water
        water1.draw()
        

# The conter and how its going look like
font = pygame.font.Font("freesansbold.ttf",30)
score = 0
text = font.render(" = "+str(score),True,(255,255,255))
textRect = text.get_rect()
textRect.center = ((150,40))


fps = 30
clock = pygame.time.Clock()

x = 10
y = 10

x_change = 0
y_change = 0

old_x = x
old_y = y

timer = 0
Stimer =  0
# Space down = False
spcdown = False
run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    water1.y -= 1

    for Platform in platforms:
        if Platform.y > 510:
            Platform.x = random.randint(0, 500)
            Platform.y = random.randint(-150, -100)
            
    # Mkaing screen go up
    if playerman.y < 250:
        playerman.y += 9
        for Platform in platforms:
            Platform.y += 9
        for Platform2 in Platforms:
            Platform2.y += 9
        for Dirt in dirts:
            Dirt.y += 9
        water1.y += 1

            
    # Marking screen go down
    if playerman.y > 410:
        playerman.y -= playerman.fall
        for Platform in platforms:
            Platform.y -= playerman.fall
        for Platform2 in Platforms:
            Platform2.y -= playerman.fall
        for Dirt in dirts:
            Dirt.y -= playerman.fall
            



        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_d:
                x_change = -7
            if event.key == pygame.K_a:
                x_change = 7

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_d or event.key == pygame.K_a:
                x_change = 0

            x += x_change
            if x > 500 - playerman.width or x < 0:
                x = old_x


  

            
        

    # If keys get pressed
    keys = pygame.key.get_pressed()
    px,py = playerman.x,playerman.y




    # Adding one to score every time player jumps
    if not keys[pygame.K_SPACE]:
        spcdown = False  # space released
    
    if keys[pygame.K_SPACE]:
        if not spcdown:
            score += 1  # if space pressed first time
        spcdown = True  # space key is pressed
        text = font.render(" = "+str(score),True,(255,255,255))
        textRect.center = ((150,40))
        


    # Player movment
    if keys[pygame.K_a] and px > playerman.speed:
        px -= playerman.speed
        playerman.direction = "lrun"
    elif keys[pygame.K_d] and px < 500 - playerman.width - playerman.speed:
        px += playerman.speed
        playerman.direction = "run"
    else:
        if playerman.direction == "run":
            playerman.direction = "idle"
        else:
            if playerman.direction == "lrun":
                playerman.direction = "idlel"
    

    if keys[pygame.K_w] and py > playerman.speed:
        py -= playerman.speed

    if keys[pygame.K_s] and py < 500 - playerman.height - playerman.speed:
        py += playerman.speed

    # animation for player jump
    if playerman.direction == "run":
        if keys[pygame.K_SPACE]: 
            playerman.direction = "jump"
    else:
        if playerman.direction == "lrun":
            if keys[pygame.K_SPACE]:
                playerman.direction = "ljump"


    platform_rect_list =[p.rect for p in platforms]
    player_rect = playerman.get_rect()
    playerman.rect.topleft = (px,py)


    playerman.y = py
    if player_rect.collidelist(platform_rect_list) < 0:
        playerman.x = px


    

        
    # About isJump
    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False

                    

            # this part lets you jump on platform only the top 
        collide = False
        for Platform in platforms:
            if playerman.get_rect().colliderect(Platform.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform.rect.top - playerman.height
                if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
                    playerman.x = Platform.rect.left - playerman.width
                if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
                    playerman.x = Platform.rect.right

        
                # Lets Player jump on top of second Platform
        for Platform2 in Platforms:
            if playerman.get_rect().colliderect(Platform2.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform2.rect.top - playerman.height
                if playerman.rect.right > Platform2.rect.left and playerman.rect.left < Platform2.rect.left - playerman.width:
                    playerman.x = Platform2.rect.left - playerman.width
                if playerman.rect.left < Platform2.rect.right and playerman.rect.right > Platform2.rect.right + playerman.width:
                    playerman.x = Platform2.rect.right

                           
                # colliding with floor      
            if playerman.rect.bottom >= 500:
                collide = True
                playerman.isJump = False
                playerman.Jumpcount = 10
                playerman.y = 500 - playerman.height

            # Jumping
        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
            playerman.fall = 0

    # Jump Count

    else:
        if playerman.JumpCount >= 0:
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.5
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10

        
            
    redrawwindow()
    pygame.display.update()
quit_game

3 个答案:

答案 0 :(得分:1)

您必须像平台和任何其他物体一样移动水。如果遵循我的建议,您将不会遇到这些麻烦:Enemy not staying at original spot

现在,您必须以“困难”方式进行操作。水位上升的速度受以下因素控制:

while run:
   # [...]

   water1.y -= 1  # <--- Controls the rising water

如果要提高速度,则需要更改递减程度。

但是,每次播放器垂直移动时,场景中的所有对象都必须移动。每次平台由于玩家的移动而垂直移动时,水也必须移动。

run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    water1.y -= 1                             # <--- Controls the rising water

    for Platform in platforms:
        if Platform.y > 510:
            Platform.x = random.randint(0, 500)
            Platform.y = random.randint(-150, -100)

    # Mkaing screen go up
    if playerman.y < 250:
        playerman.y += 9
        for Platform in platforms:
            Platform.y += 9
        for Platform2 in Platforms:
            Platform2.y += 9
        for Dirt in dirts:
            Dirt.y += 9
        water1.y += 9                         # <--- shift vertically

    # Marking screen go down
    if playerman.y > 410:
        playerman.y -= playerman.fall
        for Platform in platforms:
            Platform.y -= playerman.fall
        for Platform2 in Platforms:
            Platform2.y -= playerman.fall
        for Dirt in dirts:
            Dirt.y -= playerman.fall
        water1.y -= playerman.fall            # <--- shift vertically

答案 1 :(得分:0)

我认为您可以简单地将添加到坐标的值更改为较低的值以更改速度。我还建议将值存储在变量中,然后在add语句中使用该变量,这样,您只需在一个位置进行编辑,即可在此处检查代码:

if playerman.y < 250:
        delta = 6
        playerman.y += delta
        for Platform in platforms:
            Platform.y += delta
        for Platform2 in Platforms:
            Platform2.y += delta
        for Dirt in dirts:
            Dirt.y += delta

答案 2 :(得分:0)

尝试增加玩家跳起时水向下移动的量