如何在Pygame中显示两个图像

时间:2017-11-02 23:10:37

标签: python pygame

所以图像1工作得很好,它真的是图像2没有出现在屏幕上。此外,当第一个图像移动并击中第二个时,我希望下面的if条件执行。这涉及到pygame和两个小尺寸的图像。请得到那些。

 import pygame
#Messing with PyGame
import sys, pygame

较小的子标题

pygame.init() #needed to start pygame

size = width, height = 900, 600 #size is a variable that contains two integers
    #and represents the width and height of the game window
speed = [2, 2] #this array represents the speed you move at
vel = [0, 0] #this array represents your current velocity
black = 0, 0, 0

screen = pygame.display.set_mode(size) #sets up the window


ball = pygame.image.load("macbook.jpg")
ball2 = pygame.image.load("home.jpg")

#load the image
ballrect = ball.get_rect() #get the image rectangle object

ballrect2 = ball2.get_rect()

print("Get the Macbook home!")

较小的子标题

ballrect = ballrect.move(100,100) #move the image 100 pixels right and 100 pixels down
    #note: the top left corner of the window is 0,0, and DOWN is POSITIVE Y

ballrect2 = ballrect2.move(900,300)

exiter = True; #variable for exiting game loop

较小的子标题

#main game loop
while exiter:

    #pygame broadcasts events, which should be processed in this loop
    #events are things like keyboard presses and mouse clicks
    #pygame.event.get() returns an array of events and then the python
    for event in pygame.event.get():
        if event.type == pygame.QUIT: exiter = False;
        #We use KEYDOWN and KEYUP events because we want to know when
        #keys are pressed and released
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                vel[1] = -speed[1]
            if event.key == pygame.K_DOWN:
                vel[1] = speed[1]
            if event.key == pygame.K_LEFT:
                vel[0] = -speed[0]
            if event.key == pygame.K_RIGHT:
                vel[0] = speed[0]
        elif event.type == pygame.KEYUP:

            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                vel[1] = 0
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                vel[0] = 0

较小的子标题

    #edges of screen, checks if image is off screen and then doesn't 
    if ballrect.left < 0:
        vel[0] = 0
        ballrect.left = 0
    if ballrect.right > width:
        vel[0] = 0
        ballrect.right = width
    if ballrect.top < 0:
        vel[1] = 0
        ballrect.top = 0
    if ballrect.bottom > height:
        vel[1] = 0
        ballrect.bottom = height
    if (ballrect.top == 300 and ballrect.left == 800 or ballrect.bottom == 300 and ballrect.left == 800):
        print("macbook has reached")
        exiter = False;

    ballrect = ballrect.move(vel)

    #at end of game loop, fill screen with black
    screen.fill(black) #black screen (or copies of image will be made)
    screen.blit(ball, ballrect) #blit bascially means draw
    pygame.display.flip() #flip refers to double-buffering
    #double buffering basically means drawing the whole screen at once
    #rather than drawing it one element at a time
    #you won't see the results of any movement or changes until this
    #flip method is called

1 个答案:

答案 0 :(得分:3)

我只在您的代码中看到其中一个图片:

screen.blit(ball, ballrect)

如果你想让ball2在屏幕上显示,你也需要进行blit。

screen.blit(ball2, ballrect2)

此外,由于屏幕宽度为900且你有ballrect2.move(900,300),你将无法看到它,因为它将离开屏幕。而是做:

ballrect2.move(900 - img_width, 300)

最后,为了使碰撞符合条件,你应该使用不等式,所以边缘不一定是300/800,但可以触发轻微的重叠,如301.另外我认为你的括号的意图应该更多像:

(cond1 and cond2) or (cond3 and cond4) 

(cond1 and cond2 or cond3 and cond4)