如何解决如何使用随机化器使平台彼此远离
所以我一直在努力使播放器使用原木/平台上升,但是我有问题,我希望使原木/平台与播放机保持良好的距离,我可以跳到它,并使用随机数发生器上升到更高的位置,但对我来说,日志/平台有时会彼此靠近或离玩家跳远。 https://gyazo.com/d69b5e37be51bd1ba7896db6bb5abed5
我的日志/平台变得随机
run = True
while run:
#[....]
for Platform in platforms:
if Platform.y > 510:
Platform.x = random.randint(0,400)
Platform.y = random.randint(-160,-100)
我的完整代码
import pygame
import random
pygame.init()
window = pygame.display.set_mode((700,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_height()//6))for image in self.jump]
self.lrun = [pygame.transform.scale(image,image.get_height()//6))for image in self.lrun]
self.ljump = [pygame.transform.scale(image,image.get_height()//6))for image in self.ljump]
self.idle = [pygame.transform.scale(image,image.get_height()//6))for image in self.idle]
self.idlel = [pygame.transform.scale(image,image.get_height()//6))for image in self.idlel]
self.isJump = False
self.JumpCount = 10
self.rect = pygame.Rect(x,height)
self.next_frame_time = 0
self.fps = 10
self.clock = pygame.time.Clock()
self.anim_index = 0
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,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,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.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,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,(self.log.get_width()-90,self.log.get_height()-91))
def draw(self):
self.rect.topleft = (self.x,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,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,height)
def draw(self):
self.rect.topleft = (self.x,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)
# displaying Color
white = (255,255,255)
# Drawing stuff
# Drawing player
playerman = Player(255,440,40,white)
# Drawing Platform
platform1 = Platform(0,388,130,30,white)
# Drawing Platform2
Platform1 = Platform2(7000,470,1,white)
# Drawing Dirt
dirt1 = Dirt(7000,35,35.1,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"," "," ","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,(255,255))
Platforms.append(new_platform)
for iy,col in enumerate(row):
if col == "2":
new_dirt = Dirt(ix*35,255))
dirts.append(new_dirt)
for iy,col in enumerate(row):
if col == "l":
new_platform = Platform(ix*110,iy*60,255))
platforms.append(new_platform)
# redrawing window
def redrawwindow():
window.fill((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()
# The conter and how its going look like
font = pygame.font.Font("freesansbold.ttf",30)
score = 0
text = font.render(" = "+str(score),True,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()
for Platform in platforms:
if Platform.y > 510:
Platform.x = random.randint(0,-100)
# Mkaing screen go up
if playerman.y < 250:
playerman.y += playerman.speed
for Platform in platforms:
Platform.y += playerman.speed
for Platform2 in Platforms:
Platform2.y += playerman.speed
for Dirt in dirts:
Dirt.y += playerman.speed
# 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 timer > 0:
timer += 1
if timer > 50:
timer = 0
# 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),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 < 700 - 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.3
playerman.JumpCount -= 1
else:
playerman.isJump = False
playerman.JumpCount = 10
redrawwindow()
pygame.display.update()
quit_game
解决方法
将第一个日志记录在精灵可以跳转到的位置上。然后,通过添加/减去以前的日志坐标来重新定位其他所有日志。
您可以使用enumerate()
遍历每个platforms
索引旁边的Platform
列表,以便可以更改每个相应Platform
的位置:>
for i,Platform in enumerate(platforms):
if i:
if not Platform.fixed:
x = platforms[i-1].x + random.randint(-150,150) # Set the x to be the previous log's x minus/added with a random number that will guarantee the player will be able to access
if x < 0:
x = 0
elif x > 500:
x = 500
Platform.x = x
Platform.y = platforms[i-1].y - random.randint(100,140) # Set the y to be the previous log's y minus/added with a random number that will guarantee the player will be able to access
Platform.fixed = True
请注意,您需要将self.fixed = False
添加到__init__
函数中。
完整的固定代码:
import pygame
import random
pygame.init()
window = pygame.display.set_mode((700,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_height()//6))for image in self.jump]
self.lrun = [pygame.transform.scale(image,image.get_height()//6))for image in self.lrun]
self.ljump = [pygame.transform.scale(image,image.get_height()//6))for image in self.ljump]
self.idle = [pygame.transform.scale(image,image.get_height()//6))for image in self.idle]
self.idlel = [pygame.transform.scale(image,image.get_height()//6))for image in self.idlel]
self.isJump = False
self.JumpCount = 10
self.rect = pygame.Rect(x,height)
self.next_frame_time = 0
self.fps = 10
self.clock = pygame.time.Clock()
self.anim_index = 0
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,color):
self.x = x
self.y = y
self.fixed = False
self.width = width
self.height = height
self.color = color
self.log = pygame.image.load("log.png")
self.rect = pygame.Rect(x,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.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,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,(self.log.get_width()-20,self.log.get_height()-20))
def draw(self):
self.rect.topleft = (self.x,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,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()-20,self.dirt.get_height()-20))
self.rect = pygame.Rect(x,height)
def draw(self):
self.rect.topleft = (self.x,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)
# displaying Color
white = (255,255,255)
# Drawing stuff
# Drawing player
playerman = Player(255,440,40,white)
# Drawing Platform
platform1 = Platform(0,388,130,30,white)
# Drawing Platform2
Platform1 = Platform2(7000,470,1,white)
# Drawing Dirt
dirt1 = Dirt(7000,35,35.1,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"," "," ","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,(255,255))
Platforms.append(new_platform)
for iy,col in enumerate(row):
if col == "2":
new_dirt = Dirt(ix*35,255))
dirts.append(new_dirt)
for iy,col in enumerate(row):
if col == "l":
new_platform = Platform(ix*110,iy*60,255))
platforms.append(new_platform)
# redrawing window
def redrawwindow():
window.fill((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()
# The conter and how its going look like
font = pygame.font.Font("freesansbold.ttf",30)
score = 0
text = font.render(" = "+str(score),True,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()
for i,150)
if x < 0:
x = 0
elif x > 500:
x = 500
Platform.x = x
Platform.y = platforms[i-1].y - random.randint(100,140)
Platform.fixed = True
# Mkaing screen go up
if playerman.y < 250:
playerman.y += playerman.speed
for Platform in platforms:
Platform.y += playerman.speed
for Platform2 in Platforms:
Platform2.y += playerman.speed
for Dirt in dirts:
Dirt.y += playerman.speed
# 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 timer > 0:
timer += 1
if timer > 50:
timer = 0
# 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),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 < 700 - 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.3
playerman.JumpCount -= 1
else:
playerman.isJump = False
playerman.JumpCount = 10
redrawwindow()
pygame.display.update()
quit_game
可能不会随意放置它们,但是您可以轻松调整random.randint
部分以适应自己的喜好。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。