有时pygame中平台与玩家之间的冲突不起作用

时间:2019-09-03 11:11:05

标签: python pygame

代码有效,但有时播放器和平台之间的冲突失败。我认为这是由于平台和播放器的闪烁或振动引起的。我想简单地在平台顶部和播放器底部之间发生冲突。(self.player.pos.y < self.lowest.rect.centery)值得怀疑的主要领域。 可以使用py.sprite.spritecollide(self.player.rect.bottom,self.platform.rect.top,False)这样的东西,我知道这会导致错误。

不久前,我的播放器振动太大,因此当我增加重力值然后解决时,有时会导致跳跃失败,但这一次此技巧无效。

def update(self):
    self.all_sprites.update()

    if self.player.vel.y > 0:
        self.hits =py.sprite.spritecollide(self.player, self.platform, False)
        if self.hits:
            self.lowest = self.hits[0]
            for hit in self.hits:

                if hit.rect.bottom > self.lowest.rect.bottom:
                    self.lowest=hit

            if self.player.pos.y < self.lowest.rect.centery:
                if self.player.pos.x < self.lowest.rect.right+10 and\
                self.player.pos.x >self.lowest.rect.left-10:
                    print ("c")
                    self.player.pos.y= self.lowest.rect.top
                    self.player.vel.y=0
                    self.player.acc.y=0
                    self.player.jumping=False

    if self.player.rect.top <= height/4:
        self.player.pos.y += max(abs(self.player.vel.y),2)
        for plat in self.platform :
            plat.rect.y += max(abs(self.player.vel.y),2)
            if plat.rect.top >= height:
                self.score += 5
                plat.kill()
    if self.player.rect.bottom > height:
        for sprite in self.all_sprites:
            sprite.rect.y -= max(self.player.vel.y,10)
            if sprite.rect.bottom < 0:
                sprite.kill()
    if len(self.platform) == 0:
        self.playing = False
    while len(self.platform) < 6 :
        w=random.randrange(50,100)
        p=Platform(random.randrange(0,width-w),random.randrange(-75,-30),w,20)
        self.platform.add(p)
        self.all_sprites.add(p)

完整代码-

import pygame as py
from mainSetting import *

from mainSprites import  *
from images.anim5 import Animation


import random
import mainMusic
import os

class Game():
    def __init__(self):
        py.init()
        py.mixer.init()

        self.screen=py.display.set_mode((width*0,height*0))
        self.info=py.display.Info()
        print(self.info)
        self.bg=py.image.load(os.path.join('C:/Users/juges/Desktop/python/images','bg.jpg')).convert()

        py.display.set_caption("more fun")
        self.clock =py.time.Clock()
        self.running =True
        self.font_name = py.font.match_font('italic')
        self.score =0
        self.load_data()

    def load_data(self):
        self.dir = os.path.dirname(__file__)
        with open(os.path.join(self.dir,HS_FILE),'r') as f:
            try:
                self.highscore = int(f.read())
            except:
                self.highscore = 0

    def new(self):
        self.all_sprites= py.sprite.Group()

        self.platform=py.sprite.Group()
        self.player=Animation()
        self.all_sprites.add(self.player)
        for plat in PLATFORM_LIST:
            p = Platform(*plat)
            self.all_sprites.add(p)
            self.platform.add(p)
        #self.platform.add()
        #self.all_sprites.add(p)

        mainMusic.runP()
        self.run()


    def run(self):
        self.playing = True
        while self.playing:
            self.clock.tick(FPS)
            self.events()
            self.update()
            self.draw()


    def update(self):
        self.all_sprites.update()

        if self.player.vel.y > 0:
            self.hits =py.sprite.spritecollide(self.player, self.platform, False)
            if self.hits:
                self.lowest = self.hits[0]
                for hit in self.hits:

                    if hit.rect.bottom > self.lowest.rect.bottom:
                        self.lowest=hit

                if self.player.pos.y < self.lowest.rect.centery:
                    if self.player.pos.x < self.lowest.rect.right+10 and\
                    self.player.pos.x >self.lowest.rect.left-10:
                        print ("c")
                        self.player.pos.y= self.lowest.rect.top
                        self.player.vel.y=0
                        self.player.acc.y=0
                        self.player.jumping=False

        if self.player.rect.top <= height/4:
            self.player.pos.y += max(abs(self.player.vel.y),2)
            for plat in self.platform :
                plat.rect.y += max(abs(self.player.vel.y),2)
                if plat.rect.top >= height:
                    self.score += 5
                    plat.kill()
        if self.player.rect.bottom > height:
            for sprite in self.all_sprites:
                sprite.rect.y -= max(self.player.vel.y,10)
                if sprite.rect.bottom < 0:
                    sprite.kill()
        if len(self.platform) == 0:
            self.playing = False
        while len(self.platform) < 6 :
            w=random.randrange(50,100)
            p=Platform(random.randrange(0,width-w),random.randrange(-75,-30),w,20)
            self.platform.add(p)
            self.all_sprites.add(p)



    def events(self):
        for event in py.event.get():
            if event.type == py.QUIT:
                if self.playing:
                    self.playing=False
                self.running =False
            if event.type == py.KEYDOWN:

                if event.key == py.K_UP:
                    #self.jump()
                    self.player.jump1(self.player,self.platform)
            if event.type == py.KEYUP:
                if event.key == py.K_UP:
                    self.player.jumpCut()
            mainMusic.whi(event)

    def draw(self):
        #py.display.update()
        self.screen.blit(self.bg,(0,0))
        self.all_sprites.draw(self.screen)
        self.platform.draw(self.screen)
        self.screen.blit(self.player.image,self.player.rect)

        self.draw_text(str(self.score),22,WHITE,width/2,15)
        #mainMusic.whi()
        py.display.flip()

    def show_start_screen(self):

        self.screen.fill(EXTRA)
        self.draw_text("jumpp",48,WHITE,width/2,height/4)
        self.draw_text("arrow to move ,space to jump:",22,WHITE,width/2,height/2)
        self.draw_text("press a akey  to play:",22,WHITE,width/2,height * 3/4)
        self.draw_text("high score"+str(self.highscore),22,WHITE,width/2,15)
        py.display.flip()
        self.wait_for_key()
    def show_go_screen(self):
        if not self.running:
            return
        self.screen.fill(BLACK)

        self.draw_text("GAME OVER",48,WHITE,width/2,height / 4)
        self.draw_text("score:"+str(self.score),22,WHITE,width/2,height/2)
        self.draw_text("press key for play:",22,WHITE,width/2,height*3/4)

        if self.score > self.highscore:
            self.highscore = self.score
            self.draw_text("new score" ,22,WHITE,width/2,height/5)
            with open(os.path.join(self.dir,HS_FILE),'w') as f:
                f.write(str(self.score))
        else:
            self.draw_text("high score"+str(self.highscore),22,WHITE,width/2,height/3)


        py.display.flip()
        self.wait_for_key()
    def draw_text(self,text,size,color,x,y):
        font = py.font.Font(self.font_name,size)
        text_surface = font.render(text,True,color)
        text_rect = text_surface.get_rect()
        text_rect.midtop = (x,y)
        self.screen.blit(text_surface,text_rect)

    def wait_for_key(self):
        waiting =True
        while waiting:

            self.clock.tick(FPS)
            for event in py.event.get():
                if event.type == py.QUIT:
                    waiting = False
                    self.running =False
                if event.type == py.KEYUP:
                    self.score=0
                    waiting = False


g= Game()
g.show_start_screen()
#m=music_load(MUSIC_PATH)
while g.running:

    g.new()
    g.show_go_screen() 

py.quit()

0 个答案:

没有答案