J'essaie donc de faire en sorte que mon eau monte comme de la lave à la vitesse de 1 mais à chaque fois que mon écran se lève:
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
Le sprite d'eau deviendra plus rapide. https://gyazo.com/1bdd7b513a06f6a80832e565255e9fb0 Comment faire pour que le sprite d'eau continue à se déplacer à la même vitesse même si l'écran bouge. Peu importe la vitesse à laquelle je continue de monter, l'eau restera simplement au même endroit et continuera à monter
Mon code complet
# 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
3 Réponses :
Je pense que vous pouvez simplement changer la valeur que vous ajoutez à la coordonnée en quelque chose de plus bas pour changer la vitesse. Je recommanderais également de stocker la valeur dans une variable et d'utiliser cette variable dans l'instruction add, de cette façon, vous n'avez qu'à la modifier à un seul endroit, vérifiez le code ici:
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
Je l'ai fait, mais chaque fois que mon eau est dans mon écran, quelle que soit la vitesse à laquelle je bouge, je me rattraperai même si je me déplace plus vite et qu'elle ne bouge qu'à une vitesse de 1.
Je pense que c'est plus un problème de logique de problème qu'un problème avec les valeurs ...
Vous devez déplacer l'eau comme les plates-formes et tout autre objet. Vous n'auriez pas ces problèmes si vous aviez suivi mes suggestions: l' ennemi ne reste pas à l'endroit d'origine .
Maintenant, vous devez le faire de la manière «dure». La vitesse de montée du niveau d'eau est contrôlée par:
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
Si vous souhaitez augmenter la vitesse, vous devez modifier le degré de décrémentation.
Cependant, chaque fois que le joueur se déplace verticalement, tous les objets de la scène doivent être déplacés. Chaque fois que les plates-formes sont déplacées verticalement en raison du mouvement du joueur, l'eau doit également être déplacée.
while run: # [...] water1.y -= 1 # <--- Controls the rising water
essayez d'augmenter la quantité d'eau descend lorsque le joueur saute
Vous devez déplacer l'eau comme les plates-formes et tout autre objet. Vous n'auriez pas ces problèmes si vous aviez suivi mes suggestions: l' ennemi ne reste pas à l'endroit d'origine