Python 当我的player类与我的platform类发生冲突时,我如何制作和结束屏幕?
所以我一直在尝试制作一个结束画面,但我知道怎么做,我看过一些教程,但没有一个对我来说真正有意义。我也尝试过使用我的启动屏幕,但把它调高一点,放在不同的位置,但它不起作用。我正在尝试使它是当我的球员与我的一个平台碰撞的结束屏幕显示,但我不知道如何使结束屏幕Python 当我的player类与我的platform类发生冲突时,我如何制作和结束屏幕?,python,python-3.x,pygame,Python,Python 3.x,Pygame,所以我一直在尝试制作一个结束画面,但我知道怎么做,我看过一些教程,但没有一个对我来说真正有意义。我也尝试过使用我的启动屏幕,但把它调高一点,放在不同的位置,但它不起作用。我正在尝试使它是当我的球员与我的一个平台碰撞的结束屏幕显示,但我不知道如何使结束屏幕 I do not know what to really put here but this is my start screen code ############################################## #ST
I do not know what to really put here but this is my start screen code
##############################################
#START MENUE
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
#print(click)
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(window, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(window, ic,(x,y,w,h))
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
window.blit(textSurf, textRect)
def quitgame():
pygame.quit()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
window.fill((255,255,255))
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects("Jump", largeText)
TextRect.center = ((500/2),(500/2))
window.blit(TextSurf, TextRect)
button("GO!",100,350,100,50,green,darkgreen,main_loop)
button("Quit",300,350,100,50,orange,darkred,quitgame)
pygame.display.update()
clock.tick(15)
############################################
我的完整代码
导入pygame
导入时间
随机输入
pygame.init()
#这是尖叫高度
window=pygame.display.set_模式((500500))
#知道我们把尖叫的名字
pygame.display.set_标题(“Noobs第一场游戏”)
bg=pygame.image.load(“New.png”)
gg=pygame.image.load(“lol.png”)
Rule2=pygame.image.load(“one1.png”)
Rule1=pygame.image.load(“two2.png”)
Rule3=pygame.image.load(“three3.png”)
Rule4=pygame.image.load(“four4.png”)
Rule5=pygame.image.load(“five5.png”)
Rule6=pygame.image.load(“six6.png”)
#球员级别
职业球员:
定义初始值(自、x、y、宽度、高度、颜色):
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.color=颜色
自身速度=5
self.isJump=False
self.JumpCount=10
self.fall=0
self.rect=pygame.rect(x,y,高度,宽度)
def牵引(自):
self.rect.topleft=(self.x,self.y)
pygame.draw.rect(窗口,self.color,self.rect)
课程平台:
定义初始值(自、x、y、宽度、高度、颜色):
self.x=x
self.y=y
自我高度=高度
self.width=宽度
self.color=颜色
self.rect=pygame.rect(x,y,高度,宽度)
self.ss1=pygame.image.load(“Rock.png”)
self.ss1=pygame.transform.scale(self.ss1,(self.ss1.get_width()//2,self.ss1.get_height()//2))
self.hitbox=(self.x+20,self.y,58,60)
def牵引(自):
self.rect.topleft=(self.x,self.y)
player_rect=self.ss1.get_rect(center=self.rect.center)
player_rect.centerx+=70
玩家右中锋+=88
window.blit(self.ss1,player_rect)
self.hitbox=(self.x+20,self.y,58,60)
类别项目:
定义初始值(自、x、y、宽度、高度、颜色):
self.x=x
self.y=y
自我高度=高度
self.width=宽度
self.color=颜色
self.rect=pygame.rect(x,y,高度,宽度)
self.ss1=pygame.image.load(“H6.png”)
self.ss1=pygame.transform.scale(self.ss1,(self.ss1.get_width()//2,self.ss1.get_height()//2))
def牵引(自):
self.rect.topleft=(self.x,self.y)
blit(self.ss1,self.rect)
类墙:
定义初始值(自、x、y、宽度、高度、颜色):
self.x=x
self.y=y
自我高度=高度
self.width=宽度
self.color=颜色
self.rect=pygame.rect(x,y,高度,宽度)
def牵引(自):
self.rect.topleft=(self.x,self.y)
pygame.draw.rect(窗口,self.color,self.rect)
#平局球员
白色=(255255)
player1=播放器(255400,30,30,白色)
darkred=(200,0,0)
暗绿色=(0200,0)
黑色=(0,0,0)
项目1=项目(200,-500,50,50,白色)
项目2=项目(250250,50,50,白色)
项目3=项目(330,-1030,50,50,白色)
项目4=项目(400,-2290,50,50,白色)
项目5=项目(300,-1600,50,50,白色)
项目6=项目(200130,50,50,白色)
项目7=项目(230,-1830,50,50,白色)
项目8=项目(300,0,50,50,白色)
项目9=项目(330,-1440,50,50,白色)
项目10=项目(20,-160,50,50,白色)
项目=[项目1、项目2、项目3、项目4、项目5、项目6、项目7、项目8、项目9、项目10]
绿色=(0255,0)
橙色=(255,0,0)
平台1=平台(100300,10,60,橙色)
平台2=平台(5200,10,60,橙色)
平台3=平台(5400,10,60,橙色)
平台4=平台(100100,10,60,橙色)
平台5=平台(5,-50,10,60,橙色)
平台6=平台(100,-200,10,60,橙色)
平台7=平台(5400,60600,橙色)
平台8=平台(300,-2430,10,60,橙色)
平台9=平台(350,-340,10,60,橙色)
平台10=平台(100,-470,10,60,橙色)
平台13=平台(330,-600,10,60,橙色)
平台14=平台(100,-790,10,60,橙色)
平台15=平台(330,-990,10,60,橙色)
平台16=平台(70,-1130,10,60,橙色)
平台17=平台(200,-1340,10,60,橙色)
平台18=平台(400,-1500,10,60,橙色)
平台19=平台(300,-1700,10,60,橙色)
平台20=平台(100,-1950,10,60,橙色)
平台21=平台(350,-2140,10,60,橙色)
平台22=平台(100,-2270,10,60,橙色)
平台23=平台(0,-2500,60500,绿色)
#墙
平台11=墙壁(485,-960010000,10,橙色)
平台12=墙壁(0,-960010000,10,橙色)
墙1=墙(0400,60500,橙色)
墙2=墙(0,-2500,60500,绿色)
墙3=墙(485,-960010000,10,橙色)
墙4=墙(0,-960010000,10,橙色)
墙=[wall1,wall2]
站台=(站台1、站台2、站台3、站台4、站台5、站台6、站台7、站台8、站台9、站台10、站台13、站台14、站台15、站台16、站台17、站台18、站台19、站台20、站台21、站台22、站台23)
fps=(60)
clock=pygame.time.clock()
##############################################
#开始菜单
def text_对象(文本、字体):
textSurface=font.render(文本,真,黑色)
返回textSurface,textSurface.get_rect()
def按钮(消息、x、y、w、h、ic、ac、操作=无):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
#打印(单击)
如果x+w>鼠标[0]>x和y+h>鼠标[1]>y:
pygame.draw.rect(窗口,ac,(x,y,w,h))
如果单击[0]==1并执行操作!=无:
行动()
其他:
pygame.draw.rect(窗口,ic,(x,y,w,h))
smallText=pygame.font.SysFont(“comicsansms”,20)
textSurf,textRect=text\u对象(msg,smallText)
textRect.center=((x+(w/2)),(y+(h/2)))
blit(textSurf,textRect)
def quitgame():
pygame.quit()
def game_intro():
简介=正确
而简介:
用于pygame.e中的事件
my Full code
import pygame
import time
import random
pygame.init()
#this is screem height
window = pygame.display.set_mode((500,500))
#know we put screem name
pygame.display.set_caption("Noobs first Game")
bg = pygame.image.load("New.png")
gg = pygame.image.load("lol.png")
Rule2 = pygame.image.load("one1.png")
Rule1 = pygame.image.load("two2.png")
Rule3 = pygame.image.load("three3.png")
Rule4 = pygame.image.load("four4.png")
Rule5 = pygame.image.load("five5.png")
Rule6 = pygame.image.load("six6.png")
#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 = 5
self.isJump = False
self.JumpCount = 10
self.fall = 0
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
class platform:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
self.ss1 = pygame.image.load("Rock.png")
self.ss1 = pygame.transform.scale(self.ss1,(self.ss1.get_width()//2,self.ss1.get_height()//2))
self.hitbox = (self.x + 20 , self.y,58,60)
def draw(self):
self.rect.topleft=(self.x,self.y)
player_rect = self.ss1.get_rect(center = self.rect.center)
player_rect.centerx += 70
player_rect.centery += 88
window.blit(self.ss1,player_rect)
self.hitbox = (self.x + 20 , self.y,58,60)
class item:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
self.ss1 = pygame.image.load("H6.png")
self.ss1 = pygame.transform.scale(self.ss1,(self.ss1.get_width()//2,self.ss1.get_height()//2))
def draw(self):
self.rect.topleft = (self.x,self.y)
window.blit(self.ss1,self.rect)
class wall:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
#draw player
white = (255,255,255)
player1 = player(255,400,30,30,white)
darkred = (200,0,0)
darkgreen = (0,200,0)
black = (0,0,0)
item1 = item(200,-500,50,50,white)
item2 = item(250,250,50,50,white)
item3 = item(330,-1030,50,50,white)
item4 = item(400,-2290,50,50,white)
item5 = item(300,-1600,50,50,white)
item6 = item(200,130,50,50,white)
item7 = item(230,-1830,50,50,white)
item8 = item(300,0,50,50,white)
item9 = item(330,-1440,50,50,white)
item10 = item(20,-160,50,50,white)
items = [item1,item2,item3,item4,item5,item6,item7,item8,item9,item10]
green = (0,255,0)
orange = (255,0,0)
platform1 = platform(100,300,10,60,orange)
platform2 = platform(5,200,10,60,orange)
platform3 = platform(5,400,10,60,orange)
platform4 = platform(100,100,10,60,orange)
platform5 = platform(5,-50,10,60,orange)
platform6 = platform(100,-200,10,60,orange)
platform7 = platform(5,400,60,600,orange)
platform8 = platform(300,-2430,10,60,orange)
platform9 = platform (350,-340,10,60,orange)
platform10 = platform (100,-470,10,60,orange)
platform13 = platform(330,-600,10,60,orange)
platform14 = platform(100,-790,10,60,orange)
platform15 = platform(330,-990,10,60,orange)
platform16 = platform(70,-1130,10,60,orange)
platform17 = platform(200,-1340,10,60,orange)
platform18 = platform(400,-1500,10,60,orange)
platform19 = platform(300,-1700,10,60,orange)
platform20 = platform(100,-1950,10,60,orange)
platform21 = platform (350,-2140,10,60,orange)
platform22 = platform (100,-2270,10,60,orange)
platform23 = platform(0,-2500,60,500,green)
#walls
platform11 = wall (485,-9600,10000,10,orange)
platform12 = wall (0,-9600,10000,10,orange)
wall1 = wall (0,400,60,500,orange)
wall2 = wall (0,-2500,60,500,green)
wall3 = wall (485,-9600,10000,10,orange)
wall4 = wall (0,-9600,10000,10,orange)
walls = [wall1,wall2]
platforms = (platform1,platform2,platform3,platform4,platform5,platform6,platform7,platform8,platform9,platform10,platform13,platform14,platform15,platform16,platform17,platform18,platform19,platform20,platform21,platform22,platform23)
fps = (60)
clock = pygame.time.Clock()
##############################################
#START MENUE
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
#print(click)
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(window, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(window, ic,(x,y,w,h))
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
window.blit(textSurf, textRect)
def quitgame():
pygame.quit()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
window.fill((255,255,255))
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects("Jump", largeText)
TextRect.center = ((500/2),(500/2))
window.blit(TextSurf, TextRect)
button("GO!",100,350,100,50,green,darkgreen,main_loop)
button("Quit",300,350,100,50,orange,darkred,quitgame)
pygame.display.update()
clock.tick(15)
############################################
def main_loop():
#window
def redrawwindow():
window.fill((0,0,0))
window.blit(bg,(0,0))
#draw plyer
player1.draw()
platform11.draw()
platform12.draw()
wall3.draw()
wall4.draw()
for platform in platforms:
platform.draw()
for wall in walls:
wall.draw()
# the score draw it on the screen
window.blit(text,textRect)
for item in items:
item.draw()
font = pygame.font.Font("freesansbold.ttf",30)
score = 0
text = font.render("Coins = "+str(score),True,(255,255,255))
textRect = text.get_rect()
textRect.center = ((100,40))
run = True
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#walls
if player1.rect.colliderect(platform11) and player1.rect.colliderect(platform12):
player1.x = 40
# this makes you scroll up
if player1.y < 250:
player1.y += 1
#platforms
for platform in platforms:
platform.y += player1.speed
for item in items:
item.y += player1.speed
for wall in walls:
wall.y += player1.speed
#walls
platform11.y += player1.speed
platform12.y += player1.speed
# this makes you scroll down
if player1.y > 450:
player1.y -= player1.fall
for platform in platforms:
platform.y -= player1.fall
platform11.y -= player1.fall
platform12.y -= player1.fall
for item in items:
item.y -= player1.fall
for wall in walls:
wall.y -= player1.fall
# coin collisions
for item in items:
for one in range(len(items)-1,-1,-1):
if player1.rect.colliderect(items[one].rect):
del items[one]
score += 1
text = font.render("Coins = "+str(score),True,(255,255,255))
textRect.center = ((100,40))
keys = pygame.key.get_pressed()
if keys[pygame.K_a]and player1.x > player1.speed:
player1.x -= player1.speed
if keys[pygame.K_d]and player1.x <500 - player1.height - player1.speed:
player1.x += player1.speed
if keys[pygame.K_w]and player1.y > player1.speed:
player1.y -= player1.speed
if keys[pygame.K_s]and player1.y <500 - player1.width - player1.speed:
player1.y += player1.speed
if not player1.isJump:
player1.y += player1.fall
player1.fall += 1
player1.isJump = False
collide = False
# this part lets you jump on platform
for platform in platforms:
if player1.rect.colliderect(platform.rect):
collide = True
player1.isJump = False
player1.y = platform.rect.top - player1.height + 1
if player1.rect.right > platform.rect.left and player1.rect.left < platform.rect.left - player1.width:
player1.x = platform.rect.left - player1.width
if player1.rect.left < platform.rect.right and player1.rect.right > platform.rect.right + player1.width:
player1.x = platform.rect.right
if player1.rect.bottom >= 500:
collide = True
player1.isJump = False
player1.JumpCount = 10
player1.y = 500 - player1.height
if collide:
if keys[pygame.K_SPACE]:
player1.isJump = True
player1.fall = 0
else:
if player1.JumpCount >= 0:
player1.y -= (player1.JumpCount*abs(player1.JumpCount))*0.3
player1.JumpCount -= 1
else:
player1.JumpCount = 10
player1.isJump = False
redrawwindow()
if player1.rect.colliderect(platform6):
window.blit(Rule3,(-150,-80))
if player1.rect.colliderect(platform2.rect):
window.blit(Rule1,(-30,0))
if player1.rect.colliderect(platform23.rect):
window.blit(gg,(100,100))
if player1.rect.colliderect(platform7.rect):
window.blit(Rule2,(-70,0))
if player1.rect.colliderect(platform1.rect):
window.blit(Rule4,(-40,100))
if player1.rect.colliderect(platform14.rect):
window.blit(Rule5,(-50,0))
if player1.rect.colliderect(platform17.rect):
window.blit(Rule6,(-50,0))
pygame.display.update()
pygame.quit()
game_intro()
main_loop()
def redrawwindow():
# basically what you have now...
def draw_end_screen():
# make this new function such that it shows your end-screen
run=True
end_condition=False # a new boolean variable to keep track if collision (or something else happened)
while run:
# basically what you have now....
# check for collision (in pseudo-code) check for game-ending events
if player1.collides_with_death...:
end_condition=True
if player1.falls_into_pit()...:
end_condition=True
# use the boolean variable to decide what to do next, and in next loop
if end_condition:
draw_end_screen()
else:
redrawwindow()
pygame.display.update()