所以图像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发布于 2017-11-03 07:51:34
我只看到了你的代码中的一个图片:
screen.blit(ball, ballrect)如果你想让ball2显示在屏幕上,你也需要对其进行blit。
screen.blit(ball2, ballrect2)此外,由于屏幕宽度是900,并且您有balrect2.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) https://stackoverflow.com/questions/47086016
复制相似问题