我怎么可能超级跳跃'以更好的方式实施?

时间:2016-09-02 06:53:11

标签: pygame sprite

我需要让我的精灵超级跳跃'如果空格键被按下。在这些情况下,y速度将设置为-12.0而不是常规跳跃中的-8.0(空格键的点击)。

我使用' pygame.key.set_repeat'编写的代码。有效,但我知道必须有一个比我所得到的方法更好的方法吗?对于初学者,我有两个嵌套的' if'陈述:/

特定代码块:

# player jump
player_jumping = False
jump_vel = 0.0
player_start_y = player.Y

# set key repeat
pygame.key.set_repeat(50, 0)

# high jump
space_held = 0
super_jump = False

# event loop
while True:
    ticks = pygame.time.get_ticks() # n.o ms since pygame.init() was called
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_SPACE and space_held <2:
                space_held += 1
                if not player_jumping:
                    player_jumping = True
                    jump_vel = -8.0
            elif event.key == K_SPACE and space_held == 2:
                if player_jumping and not super_jump:
                    jump_vel = -12.0
                    super_jump = True
        if event.type == KEYUP:
            if event.key == K_SPACE:
                space_held = 0
                super_jump = True

# is player jumping?
    if player_jumping:
        player.Y += jump_vel
        jump_vel += 0.5
        if player.Y > player_start_y:
            player_jumping = False
            player.Y = player_start_y
            jump_vel = 0.0
            super_jump = False
            space_held = 0

完整的工作代码:

import math
import time
import sys
import random
import pygame
from pygame.locals import *

class MySprite(pygame.sprite.Sprite):
    """My sprite"""
    def __init__(self, target):
        super(MySprite, self).__init__()
        self.frame = 0
        self.old_frame = -1
        self.master_image = None
        self.frame_width = 1
        self.frame_height = 1
        self.first_frame = 0
        self.last_frame = 0
        self.columns = 1
        self.last_time = 0

    # X property
    def _getx(self): return self.rect.x
    def _setx(self, value): self.rect.x = value
    X = property(_getx, _setx)        

    # Y property
    def _gety(self): return self.rect.y
    def _sety(self, value): self.rect.y = value
    Y = property(_gety, _sety)

    # position property
    def _getpos(self): return self.rect.topleft
    def _setpos(self, pos): self.rect.topleft = pos
    position = property(_getpos, _setpos)

    def load(self, filename, width, height, columns):
        self.master_image = pygame.image.load(filename).convert_alpha()
        self.frame_width = width
        self.frame_height = height
        self.columns = columns
        self.rect = Rect(0,0, width, height)
        # try to auto-calc frames
        rect = self.master_image.get_rect()
        self.last_frame = (rect.height//height) * (rect.width/width) -1


    def update(self, current_time, rate = 30):
        if current_time > self.last_time + rate:
            self.frame += 1
            if self.frame > self.last_frame:
                self.frame = self.first_frame
            self.last_time = current_time

        # build current frame only if changed
        if self.frame != self.old_frame:
            frame_x = (self.frame % self.columns) * self.frame_width
            frame_y = (self.frame // self.columns) * self.frame_height
            rect = Rect(frame_x, frame_y, self.frame_width, self.frame_height)
            self.image = self.master_image.subsurface(rect)
            self.old_frame = self.frame

    def __str__(self):
        rep = str(self.frame) + ", " + str(self.first_frame) + ", " +\
              str(self.last_frame) + ", " + str(self.frame_width) +\
              ", " + str(self.frame_height) + ", " + str(self.columns) +\
              ", " + str(self.rect)
        return rep

def print_text(font, x, y, text, color = (255,255,255)):
    textimage = font.render(text, True, color)
    DISPLAYSURF.blit(textimage, (x,y))

def reset_arrow():
    y = random.randint(250, 350)
    arrow.position = (800, y)


pygame.init()

DISPLAYSURF = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Escape the dragon")
font = pygame.font.SysFont(None, 18)
font1 = pygame.font.SysFont("trebuchet ms", 40)
fps = 30
fpsclock = pygame.time.Clock()

# load bitmaps
background  = pygame.image.load("background.png").convert()

# create a sprite group
group = pygame.sprite.Group()

# create dragon sprite
dragon = MySprite(DISPLAYSURF)
dragon.load("dragon.png", 260, 150, 3)
dragon.position = (100, 230)
group.add(dragon)

# create player sprite
player = MySprite(DISPLAYSURF)
player.load("caveman.png", 50, 64, 8)
player.first_frame = 1
player.last_frame = 7
player.position = 400, 303
group.add(player)

# create arrow sprite
arrow = MySprite(DISPLAYSURF)
arrow.load("flame.png", 40, 16, 1)
arrow.position = (800,320)
group.add(arrow)

arrow_vel = 8.0
game_over = False
you_win = False
player_jumping = False
jump_vel = 0.0
player_start_y = player.Y
score = 0

# set key repeat
pygame.key.set_repeat(50, 0)

# high jump
space_held = 0
super_jump = False

# event loop
while True:
    ticks = pygame.time.get_ticks() # n.o ms since pygame.init() was called
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_SPACE and space_held <2:
                space_held += 1
                if not player_jumping:
                    player_jumping = True
                    jump_vel = -8.0
            elif event.key == K_SPACE and space_held == 2:
                if player_jumping and not super_jump:
                    jump_vel = -12.0
                    super_jump = True
        if event.type == KEYUP:
            if event.key == K_SPACE:
                space_held = 0
                super_jump = True

    # keyboard polling
    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        pygame.quit()
        sys.exit()
##    if keys[K_SPACE]:
##        if not player_jumping:
##            
##            player_jumping  = True
##            jump_vel = -8.0

    # update arrow
    if not game_over:
        arrow.X -= arrow_vel
        if arrow.X < -40:
            reset_arrow()

    # did arrow hit player?
    if pygame.sprite.collide_rect(arrow, player):
        player.X -=10
        reset_arrow()

    # did arrow hit dragon?
    if pygame.sprite.collide_rect(arrow, dragon):
        dragon.X -= 10
        reset_arrow()
        score += 10

    # did dragon eat player?
    if pygame.sprite.collide_rect(player, dragon):
        game_over = True

    # dragon defeated?
    if dragon.X < -100   :
        game_over = True
        you_win = True

    # is player jumping?
    if player_jumping:
        player.Y += jump_vel
        jump_vel += 0.5
        if player.Y > player_start_y:
            player_jumping = False
            player.Y = player_start_y
            jump_vel = 0.0
            super_jump = False
            space_held = 0

    # draw background
    DISPLAYSURF.blit(background, (0,0))

    # update sprites
    if not game_over:
        group.update(ticks, 50)

    # draw sprites
    group.draw(DISPLAYSURF)

    print_text(font1, 325, 0, "Score: " + str(score))
    print_text(font, 350, 560, "Press space to jump!")
    if game_over:
        score = 0
        if you_win:
            print_text(font, 330, 130, "YOU BEAT THE DRAGON!")
        else:
            print_text(font, 330, 130, "THE DRAGON ATE YOU!!!")



    pygame.display.update()
    fpsclock.tick(fps)

0 个答案:

没有答案