如何在pygame中制作图像并使它们移动?

时间:2018-07-05 11:36:22

标签: python python-3.x pygame python-requests

我一直在为一个学校项目开发游戏,并且这些块出现在屏幕上,您需要躲避它们。我需要将屏幕下方的空白绘制矩形变成我拥有的汽车的图片。 到目前为止,这是我的代码:

import pygame
import time
import random

pygame.init()

skate_sound = pygame.mixer.Sound("skateboard.wav")
pygame.mixer.music.load("menu_audio.wav")
ouch = pygame.mixer.Sound("ouch.wav")



display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
cyan = (71, 148, 192)
green = (0, 255, 0)
dark_red = (200, 0 ,0)
dark_green = (0, 180 ,0)
grass_green = (39, 131, 51)
man_width = 35

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Skate or Die')
clock = pygame.time.Clock()

manImg = pygame.image.load('skater1.png')





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



def objects(objectx, objecty, objectw, objecth, color):
    pygame.draw.rect(gameDisplay, color, [objectx, objecty, objectw, objecth])

#^^NEED TO CHANGE TO AN IMAGE CALLED TAXI.PNG CAN SOMEONE PLEASE FIX FOR ME    

def man(x,y):
    gameDisplay.blit(manImg,(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/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()


def crash():
    pygame.mixer.Sound.play(ouch)
    pygame.mixer.Sound.stop(skate_sound)
    message_display("You Died!")

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(gameDisplay, ic,(x,y,w,h))
            if click[0] == 1 and action != None:
                if action == "play":
                    game_loop()
                elif action == "quit":
                    pygame.quit()
                    quit()

        else:
            pygame.draw.rect(gameDisplay, ac,(x,y,w,h))

        smallText = pygame.font.Font("freesansbold.ttf",20)
        textSurf, textRect = text_objects(msg, smallText)
        textRect.center = ( (x+(w/2)), (y+(h/2)) )
        gameDisplay.blit(textSurf, textRect)

def game_intro():
    pygame.mixer.music.play(-1)

    intro = True

    while intro:
        for event in pygame.event.get():
            #print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)
        largeText = pygame.font.Font('freesansbold.ttf',115)
        TextSurf, TextRect = text_objects("Skate or Die!", largeText)
        TextRect.center = ((display_width/2),(display_height/2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Start",150,450,100,50,dark_green,green,"play")
        button("Quit",550,450,100,50,dark_red,red,"quit")



        #pygame.draw.rect(gameDisplay, red,(550,450,100,50))

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



def game_loop():
    pygame.mixer.Sound.stop(ouch)
    pygame.mixer.music.stop()
    pygame.mixer.Sound.play(skate_sound)

    x = (display_width * 0.45)
    y = (display_height * 0.75)

    x_change = 0


    object_startx = random.randrange(0, display_width)
    object_starty = -600
    object_speed = 8
    object_width = 100
    object_height = 80 

    dodged = 0 


    gameExit = 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:
                    x_change = -5
                if event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        x += x_change

        #gameDisplay.fill(white)
        bg = pygame.image.load("bg.png")
        gameDisplay.blit(bg, (0, 0))
        man(x,y)

        # objects(objectx, objecty, objectw, objecth, color)
        objects(object_startx, object_starty, object_width, object_height, cyan)
        object_starty += object_speed
        man(x,y)
        objects_dodged(dodged)



        if x > display_width - man_width or x < 0:
            crash()
        if object_starty > display_height:
            object_starty = 0 - object_height
            object_startx = random.randrange(0, display_width)
            dodged += 1
            object_speed += 0.7
            object_width += (dodged * 1)



        if y < object_starty+object_height:
            print('y crossover')

            if x > object_startx and x < object_startx + object_width or x+man_width > object_startx and x + man_width < object_startx+object_width:
                print('x crossover')
                crash()




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


game_intro()
game_loop()
pygame.quit()
quit()

如果有人可以修复它,那么它将显示taxi.png而不是矩形作为对象,将不胜感激。

1 个答案:

答案 0 :(得分:3)

要绘制汽车的图片而不是矩形,只需执行与此人相同的操作即可。

manImg = pygame.image.load('skater1.png')行下,放置以下行:carImg= pygame.image.load('taxi.png')这将加载汽车的图片,并将其称为carImg

现在,您需要将该图片绘制到屏幕上。替换对象功能:

def objects(objectx, objecty, objectw, objecth, color):
    pygame.draw.rect(gameDisplay, color, [objectx, objecty, objectw, objecth])

具有绘制汽车的功能。

def drawCar(x,y):
    pygameDisplay.blit(carImg,(x,y))

请注意它与man函数非常相似,唯一的区别是绘制的图像。

您需要做的最后一件事实际上是调用该drawCar函数。替换此行 objects(object_startx, object_starty, object_width, object_height, cyan)

与此一起:drawCar(object_startx,object_starty)

我建议您自己进行这些更改,这将帮助您学习。但是如果您着急的话,完成所有这些更改的最终代码应该是:

import pygame
import time
import random

pygame.init()

skate_sound = pygame.mixer.Sound("skateboard.wav")
pygame.mixer.music.load("menu_audio.wav")
ouch = pygame.mixer.Sound("ouch.wav")



display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
cyan = (71, 148, 192)
green = (0, 255, 0)
dark_red = (200, 0 ,0)
dark_green = (0, 180 ,0)
grass_green = (39, 131, 51)
man_width = 35

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Skate or Die')
clock = pygame.time.Clock()

manImg = pygame.image.load('skater1.png')
carImg= pygame.image.load('taxi.png')





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



def drawCar(x,y):
    pygameDisplay.blit(carImg,(x,y))

def man(x,y):
    gameDisplay.blit(manImg,(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/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()


def crash():
    pygame.mixer.Sound.play(ouch)
    pygame.mixer.Sound.stop(skate_sound)
    message_display("You Died!")

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(gameDisplay, ic,(x,y,w,h))
            if click[0] == 1 and action != None:
                if action == "play":
                    game_loop()
                elif action == "quit":
                    pygame.quit()
                    quit()

        else:
            pygame.draw.rect(gameDisplay, ac,(x,y,w,h))

        smallText = pygame.font.Font("freesansbold.ttf",20)
        textSurf, textRect = text_objects(msg, smallText)
        textRect.center = ( (x+(w/2)), (y+(h/2)) )
        gameDisplay.blit(textSurf, textRect)

def game_intro():
    pygame.mixer.music.play(-1)

    intro = True

    while intro:
        for event in pygame.event.get():
            #print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)
        largeText = pygame.font.Font('freesansbold.ttf',115)
        TextSurf, TextRect = text_objects("Skate or Die!", largeText)
        TextRect.center = ((display_width/2),(display_height/2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Start",150,450,100,50,dark_green,green,"play")
        button("Quit",550,450,100,50,dark_red,red,"quit")



        #pygame.draw.rect(gameDisplay, red,(550,450,100,50))

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



def game_loop():
    pygame.mixer.Sound.stop(ouch)
    pygame.mixer.music.stop()
    pygame.mixer.Sound.play(skate_sound)

    x = (display_width * 0.45)
    y = (display_height * 0.75)

    x_change = 0


    object_startx = random.randrange(0, display_width)
    object_starty = -600
    object_speed = 8
    object_width = 100
    object_height = 80 

    dodged = 0 


    gameExit = 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:
                    x_change = -5
                if event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        x += x_change

        #gameDisplay.fill(white)
        bg = pygame.image.load("bg.png")
        gameDisplay.blit(bg, (0, 0))
        man(x,y)

        # objects(objectx, objecty, objectw, objecth, color)
        drawCar(object_startx,object_starty)
        object_starty += object_speed
        man(x,y)
        objects_dodged(dodged)

在线学习教程时,请勿仅复制并粘贴代码。确保您了解正在发生的事情,以便将来可以自己做。