python でキャラクターをジャンプして着地させたいのですが、上手くいきません。

実現したいこと

ジャンプしてfloorに接触(colliderect)したらとまる。

発生している問題・分からないこと

接触してもfloorでとまらない。
floor_rectに衝突してもそこでピッタリとまらない。30ピクセルくらい下に行ってからとまるので、
床からジャンプして着地すると床に身体が半分くらい埋まってしまう。

エラーメッセージ

error

1なし。

該当のソースコード

import pygame from pygame.locals import * from sys import exit import random pygame.init() screen = pygame.display.set_mode((1000,1000)) pygame.display.set_caption('learning_rect') clock = pygame.time.Clock() floor_surf = pygame.Surface((1000,64)) floor_rect = floor_surf.get_rect(topleft = (0,936)) floor_surf.fill('Blue') mario_x = 100 mario_y = 700 mario_y_speed = 1 mario_jump = False mario_jump_height = 20 gravity = 1 img_bg = pygame.image.load('bg.png')# 背景 mario_surface = pygame.image.load('mario1.png').convert_alpha() mario_rect = mario_surface.get_rect(topleft = (mario_x,mario_y)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() pressed_key = pygame.key.get_pressed() if pressed_key[K_RIGHT]: mario_x += 10 if pressed_key[K_LEFT]: mario_x -= 10 if pressed_key[K_SPACE]: print('jump') mario_jump = True mario_y -= mario_jump_height mario_jump_height -=gravity if mario_rect.colliderect(floor_rect): print('collision') if mario_rect.colliderect(floor_rect): #mario_jump = False jump_height = 20 print(mario_jump) screen.blit(img_bg,(0,0)) screen.blit(floor_surf,floor_rect) mario_y += mario_y_speed screen.blit(mario_surface,mario_rect) if mario_y >= 1100: mario_y = 0 mario_rect.topleft = (mario_x,mario_y) if mario_rect.colliderect(floor_rect): mario_y_speed = 0 pygame.display.update() clock.tick(60)

試したこと・調べたこと

上記の詳細・結果
import pygame from pygame.locals import * from sys import exit import random pygame.init() screen = pygame.display.set_mode((1000,1000)) pygame.display.set_caption('learning_rect') clock = pygame.time.Clock() floor_surf = pygame.Surface((1000,64)) floor_rect = floor_surf.get_rect(topleft = (0,936)) floor_surf.fill('Blue') mario_x = 100 mario_y = 872 mario_y_speed = 1 mario_jump = False mario_jump_height = 20 gravity = 1 img_bg = pygame.image.load('bg.png')# 背景 mario_surface = pygame.image.load('mario1.png').convert_alpha() mario_rect = mario_surface.get_rect(topleft = (mario_x,mario_y)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() pressed_key = pygame.key.get_pressed() if mario_x <=936: if pressed_key[K_RIGHT]: mario_x += 10 if mario_x >= 0: if pressed_key[K_LEFT]: mario_x -= 10 if pressed_key[K_SPACE] and mario_jump ==False: mario_jump = True mario_jump_height = 20 if mario_jump: mario_y -= mario_jump_height mario_jump_height -=gravity if mario_rect.colliderect(floor_rect): mario_jump = False #mario_y = 820 print(mario_jump) screen.blit(img_bg,(0,0)) screen.blit(floor_surf,floor_rect) screen.blit(mario_surface,mario_rect) mario_rect.topleft = (mario_x,mario_y) pygame.display.update() clock.tick(60)

↑現状改善がすすんで、ジャンプはできるようになりましたが、なぜか着地位置が30ピクセルくらい元の高さから下になります。

補足

python に慣れるために、やりたいことから無理やりプログラムを作ってるため、かなり変なコードなのだとおもいます。すみません。chatGPTにも質問したりしつつ、ここまでたどりつきました。rectの使い方に慣れる為に作ってるのですが、なんか上手くいきません。

mario の画像は64×64です。

宜しくお願いします。

コメントを投稿

0 コメント