在pygame中使用图像作为对象,但它不会出现在游戏框架中

时间:2017-05-19 17:34:01

标签: python pygame python-3.6

我试图使用的图像是drakeImg,它应该从游戏框架的顶部掉落,游戏工作正常,但我看不到drakeImg从屏幕顶部掉落,我只能看到boatImg和背景,但躲闪计数和崩溃()仍然有效。这是我的代码:

import pygame
import time
import random

pygame.init()

display_width = 800
display_height = 700

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (3,30,104)

boat_width = 110
drake_width = 110

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Gotta Go Fast')
clock = pygame.time.Clock()

drakeImg = pygame.image.load('drake.png')
drakeImg = pygame.transform.scale(drakeImg, (100, 100))

boatImg = pygame.image.load('boat.png')
boatImg = pygame.transform.scale(boatImg, (100, 150))

bg = pygame.image.load("bg.png")

def drakes_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Drakes Dodged: " + str(count), True, green)
    gameDisplay.blit(text,(0,0))

def drake(drakex, drakey):
    gameDisplay.blit(drakeImg, (drakex, drakey))


def boat(x,y):
    gameDisplay.blit(boatImg, (x,y))

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()


def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/3))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()

def crash():
    message_display('You Lost!')

def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.75)

    drake_startx = random.randrange(0, display_width)
    drake_starty = -550
    drake_speed = 4
    drake_height = 100

    drakesDodged = 0

    gameExit = False

    key_right = False
    key_left = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    key_left = True
                if event.key == pygame.K_RIGHT:
                    key_right = True
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    key_left = False
                if event.key == pygame.K_RIGHT:
                    key_right = False


        gameDisplay.blit(bg, (0, 0))

        drake_starty += drake_speed
        boat(x,y)
        drakes_dodged(drakesDodged)

        if key_left == True and x > 0:
            x += -5
        if key_right == True and x < (display_width - boat_width):
            x += 5

        if drake_starty > display_height:
            drake_starty = 0 - drake_height
            drake_startx = random.randrange(0,display_width)
            drakesDodged += 1
            if drakesDodged % 10 == 0:
                drake_speed += 2




        if y < drake_starty + drake_height:
            if x + boat_width > drake_startx and x < drake_startx + drake_width:
                crash()


        pygame.display.update()
        clock.tick(60)

game_loop()
pygame.quit()
quit()

0 个答案:

没有答案