質問編集履歴
3
試したことのソースコードを更新しました。現状はこの状態です。
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,10 +3,12 @@
|
|
3
3
|
|
4
4
|
### 発生している問題・分からないこと
|
5
5
|
接触してもfloorでとまらない。
|
6
|
+
floor_rectに衝突してもそこでピッタリとまらない。30ピクセルくらい下に行ってからとまるので、
|
7
|
+
床からジャンプして着地すると床に身体が半分くらい埋まってしまう。
|
6
8
|
|
7
9
|
### エラーメッセージ
|
8
10
|
```error
|
9
|
-
|
11
|
+
なし。
|
10
12
|
```
|
11
13
|
|
12
14
|
### 該当のソースコード
|
@@ -101,72 +103,79 @@
|
|
101
103
|
import pygame
|
102
104
|
from pygame.locals import *
|
103
105
|
from sys import exit
|
106
|
+
import random
|
104
107
|
|
105
108
|
pygame.init()
|
106
|
-
screen = pygame.display.set_mode((1000,
|
109
|
+
screen = pygame.display.set_mode((1000,1000))
|
107
110
|
pygame.display.set_caption('learning_rect')
|
108
111
|
clock = pygame.time.Clock()
|
109
112
|
|
110
|
-
floor_surf = pygame.Surface((1000,
|
113
|
+
floor_surf = pygame.Surface((1000,64))
|
111
|
-
floor_rect = floor_surf.get_rect(topleft=(0,
|
114
|
+
floor_rect = floor_surf.get_rect(topleft = (0,936))
|
112
115
|
floor_surf.fill('Blue')
|
113
116
|
|
117
|
+
|
114
118
|
mario_x = 100
|
115
|
-
mario_y =
|
119
|
+
mario_y = 872
|
116
|
-
mario_y_speed =
|
120
|
+
mario_y_speed = 1
|
117
121
|
mario_jump = False
|
118
122
|
mario_jump_height = 20
|
119
123
|
gravity = 1
|
120
124
|
|
125
|
+
img_bg = pygame.image.load('bg.png')# 背景
|
126
|
+
|
121
127
|
mario_surface = pygame.image.load('mario1.png').convert_alpha()
|
122
|
-
img_bg = pygame.image.load('bg.png')
|
123
|
-
mario_rect = mario_surface.get_rect(topleft=(mario_x,
|
128
|
+
mario_rect = mario_surface.get_rect(topleft = (mario_x,mario_y))
|
124
129
|
|
125
130
|
while True:
|
126
|
-
|
131
|
+
for event in pygame.event.get():
|
127
|
-
|
132
|
+
if event.type == pygame.QUIT:
|
128
|
-
|
133
|
+
pygame.quit()
|
129
|
-
|
134
|
+
exit()
|
130
135
|
|
131
|
-
pressed_key = pygame.key.get_pressed()
|
132
|
-
if pressed_key[K_RIGHT]:
|
133
|
-
mario_x += 10
|
134
136
|
|
137
|
+
pressed_key = pygame.key.get_pressed()
|
138
|
+
if mario_x <=936:
|
139
|
+
if pressed_key[K_RIGHT]:
|
140
|
+
mario_x += 10
|
141
|
+
|
142
|
+
if mario_x >= 0:
|
135
|
-
|
143
|
+
if pressed_key[K_LEFT]:
|
136
|
-
|
144
|
+
mario_x -= 10
|
137
145
|
|
138
|
-
|
146
|
+
if pressed_key[K_SPACE] and mario_jump ==False:
|
139
|
-
|
147
|
+
mario_jump = True
|
140
|
-
|
148
|
+
mario_jump_height = 20
|
149
|
+
|
150
|
+
if mario_jump:
|
151
|
+
mario_y -= mario_jump_height
|
152
|
+
mario_jump_height -=gravity
|
141
153
|
|
154
|
+
if mario_rect.colliderect(floor_rect):
|
155
|
+
mario_jump = False
|
156
|
+
#mario_y = 820
|
142
|
-
|
157
|
+
print(mario_jump)
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
screen.blit(img_bg,(0,0))
|
143
|
-
|
163
|
+
screen.blit(floor_surf,floor_rect)
|
144
|
-
mario_jump_height -= gravity
|
145
164
|
|
146
|
-
|
165
|
+
screen.blit(mario_surface,mario_rect)
|
147
|
-
mario_jump = False # ジャンプをリセット
|
148
|
-
|
166
|
+
mario_rect.topleft = (mario_x,mario_y)
|
149
167
|
|
150
|
-
screen.blit(img_bg, (0, 0))
|
151
|
-
screen.blit(floor_surf, floor_rect)
|
152
168
|
|
153
|
-
mario_y += mario_y_speed
|
154
|
-
screen.blit(mario_surface, mario_rect)
|
155
169
|
|
156
|
-
|
170
|
+
|
157
|
-
mario_jump = False # ジャンプをリセット
|
158
|
-
mario_y = 936 # マリオの位置をfloorの上に設定
|
159
171
|
|
160
|
-
|
172
|
+
pygame.display.update()
|
173
|
+
clock.tick(60)
|
174
|
+
|
161
175
|
|
162
|
-
if mario_rect.colliderect(floor_rect):
|
163
|
-
mario_y_speed = 0
|
164
176
|
|
165
|
-
pygame.display.update()
|
166
|
-
clock.tick(60)
|
167
177
|
````````````````````````````````````````````````````````````````````````````````````
|
168
|
-
↑
|
178
|
+
↑現状改善がすすんで、ジャンプはできるようになりましたが、なぜか着地位置が30ピクセルくらい元の高さから下になります。
|
169
|
-
嫌です。
|
170
179
|
|
171
180
|
|
172
181
|
|
2
``````でコードを囲いました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -97,6 +97,7 @@
|
|
97
97
|
- [ ] その他
|
98
98
|
|
99
99
|
##### 上記の詳細・結果
|
100
|
+
```````````````````````````````````````````````````
|
100
101
|
import pygame
|
101
102
|
from pygame.locals import *
|
102
103
|
from sys import exit
|
@@ -163,7 +164,7 @@
|
|
163
164
|
|
164
165
|
pygame.display.update()
|
165
166
|
clock.tick(60)
|
166
|
-
|
167
|
+
````````````````````````````````````````````````````````````````````````````````````
|
167
168
|
↑のコードはchatGPTが教えてくれたやり方ですが、marioのy座標を936に限定してしまうので
|
168
169
|
嫌です。
|
169
170
|
|
1
試したコードを追記しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -97,8 +97,78 @@
|
|
97
97
|
- [ ] その他
|
98
98
|
|
99
99
|
##### 上記の詳細・結果
|
100
|
+
import pygame
|
100
|
-
|
101
|
+
from pygame.locals import *
|
102
|
+
from sys import exit
|
101
103
|
|
104
|
+
pygame.init()
|
105
|
+
screen = pygame.display.set_mode((1000, 1000))
|
106
|
+
pygame.display.set_caption('learning_rect')
|
107
|
+
clock = pygame.time.Clock()
|
108
|
+
|
109
|
+
floor_surf = pygame.Surface((1000, 64))
|
110
|
+
floor_rect = floor_surf.get_rect(topleft=(0, 936))
|
111
|
+
floor_surf.fill('Blue')
|
112
|
+
|
113
|
+
mario_x = 100
|
114
|
+
mario_y = 100
|
115
|
+
mario_y_speed = 15
|
116
|
+
mario_jump = False
|
117
|
+
mario_jump_height = 20
|
118
|
+
gravity = 1
|
119
|
+
|
120
|
+
mario_surface = pygame.image.load('mario1.png').convert_alpha()
|
121
|
+
img_bg = pygame.image.load('bg.png')
|
122
|
+
mario_rect = mario_surface.get_rect(topleft=(mario_x, mario_y))
|
123
|
+
|
124
|
+
while True:
|
125
|
+
for event in pygame.event.get():
|
126
|
+
if event.type == pygame.QUIT:
|
127
|
+
pygame.quit()
|
128
|
+
exit()
|
129
|
+
|
130
|
+
pressed_key = pygame.key.get_pressed()
|
131
|
+
if pressed_key[K_RIGHT]:
|
132
|
+
mario_x += 10
|
133
|
+
|
134
|
+
if pressed_key[K_LEFT]:
|
135
|
+
mario_x -= 10
|
136
|
+
|
137
|
+
if pressed_key[K_SPACE] and not mario_jump and mario_rect.colliderect(floor_rect):
|
138
|
+
mario_jump = True
|
139
|
+
mario_jump_height = 20 # ジャンプの高さをリセット
|
140
|
+
|
141
|
+
if mario_jump:
|
142
|
+
mario_y -= mario_jump_height
|
143
|
+
mario_jump_height -= gravity
|
144
|
+
|
145
|
+
if mario_y >= 936: # floorに着地したら
|
146
|
+
mario_jump = False # ジャンプをリセット
|
147
|
+
mario_y = 936 # マリオの位置をfloorの上に設定
|
148
|
+
|
149
|
+
screen.blit(img_bg, (0, 0))
|
150
|
+
screen.blit(floor_surf, floor_rect)
|
151
|
+
|
152
|
+
mario_y += mario_y_speed
|
153
|
+
screen.blit(mario_surface, mario_rect)
|
154
|
+
|
155
|
+
if mario_y >= 936: # floorに着地したら
|
156
|
+
mario_jump = False # ジャンプをリセット
|
157
|
+
mario_y = 936 # マリオの位置をfloorの上に設定
|
158
|
+
|
159
|
+
mario_rect.topleft = (mario_x, mario_y)
|
160
|
+
|
161
|
+
if mario_rect.colliderect(floor_rect):
|
162
|
+
mario_y_speed = 0
|
163
|
+
|
164
|
+
pygame.display.update()
|
165
|
+
clock.tick(60)
|
166
|
+
|
167
|
+
↑のコードはchatGPTが教えてくれたやり方ですが、marioのy座標を936に限定してしまうので
|
168
|
+
嫌です。
|
169
|
+
|
170
|
+
|
171
|
+
|
102
172
|
### 補足
|
103
173
|
python に慣れるために、やりたいことから無理やりプログラムを作ってるため、かなり変なコードなのだとおもいます。すみません。chatGPTにも質問したりしつつ、ここまでたどりつきました。rectの使い方に慣れる為に作ってるのですが、なんか上手くいきません。
|
104
174
|
|