python pygame游戏-我的苹果通过购物车

python pygame游戏-我的苹果通过购物车,python,random,pygame,Python,Random,Pygame,我在看SentDex,youtuber,关于pygame教程制作一个“简单”的游戏。。。 所以我决定翻修它,让它成为我自己的,但和他的公寓一样。 当你玩游戏的时候,苹果从树上掉下来,你必须用手推车来接住它们,然而,当我想接住苹果的时候,苹果会跌到谷底,但当苹果撞到边缘时,苹果不会跌到谷底。以下是代码: import pygame import time import random pygame.init() width = 800 height = 600 gameDisplay = pyga

我在看SentDex,youtuber,关于pygame教程制作一个“简单”的游戏。。。 所以我决定翻修它,让它成为我自己的,但和他的公寓一样。 当你玩游戏的时候,苹果从树上掉下来,你必须用手推车来接住它们,然而,当我想接住苹果的时候,苹果会跌到谷底,但当苹果撞到边缘时,苹果不会跌到谷底。以下是代码:

import pygame
import time
import random

pygame.init()
width = 800
height = 600
gameDisplay = pygame.display.set_mode((width,height))
pygame.display.set_caption("Catching apples")
clock = pygame.time.Clock()
apple = pygame.image.load("jabuka.png")

cartImg = pygame.image.load("gajba.png")
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
def apples(x,y):
    gameDisplay.blit(apple,(x,y))
def text_objects(text,font):
    textSurface = font.render(text,True,black)
    return textSurface,textSurface.get_rect()




def message_dispaly(text):
    largeText = pygame.font.Font("freesansbold.ttf",90)
    TextSurf, TextRect = text_objects(text,largeText)
    TextRect.center = ((sirina/2),(visina/2))
    gameDisplay.blit(TextSurf,TextRect)
    pygame.display.update()

    time.sleep(2)

    game_loop()


def n_uhvatili():
    message_dispaly("You did no catch the apple!")

def gajba(x,y):
    gameDisplay.blit(cartImg,(x,y))

def game_loop():
    #About apple.
    width_apple = 50
    height_apple = 82
    apple_startx = random.randrange(0,width-apple_width)
    apple_starty = -600
    apple_speed = 8
    x_cart = (width * 0.45)
    y_cart = (height * 0.8)
    x_change = 0
    cart_width = 193
    car_height = 105

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameEXit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -8
                elif event.key == pygame.K_RIGHT:
                    x_change = 8
                elif event.tyoe == pygame.K_q:
                    pygame.quit()
                    quit()

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



        x_cart += x_change

        gameDisplay.fill(white)
        jabuke(apple_startx,apple_starty)
        apple_starty += apple_speed

        if apple_starty > height:
            n_uhvatili()



        gajba(x_gajbe,y_gajbe)
        if x_cart > width - cart_width:
            x_cart = width - cart_width
        if x_cart < 0:
            x_cart = 0
#Note that problem must be in this statement.
        if y_cart < apple_starty + apple_height:
            print("y crossover")
            if x_cart > apple_startx and x_cart < apple_starty + apple_width or x_cart +cart_width > apple_startx and x_cart + cart_width < apple_startx + apple_height:

                print("You catched an apple")
                apple_startx = random.randrange(0, width - apple_width)
                apple_starty = -50


        pygame.display.update()
        clock.tick(59)
game_loop()
pygame.quit()
quit()
导入pygame
导入时间
随机输入
pygame.init()
宽度=800
高度=600
gameDisplay=pygame.display.set_模式((宽度、高度))
pygame.display.set_标题(“抓苹果”)
clock=pygame.time.clock()
apple=pygame.image.load(“jabuka.png”)
cartImg=pygame.image.load(“gajba.png”)
黑色=(0,0,0)
白色=(255255)
红色=(255,0,0)
def苹果(x,y):
blit(苹果,(x,y))
def text_对象(文本、字体):
textSurface=font.render(文本,真,黑色)
返回textSurface,textSurface.get_rect()
def消息显示(文本):
largeText=pygame.font.font(“freesansbold.ttf”,90)
TextSurf,TextRect=text\u对象(text,largeText)
TextRect.center=((sirina/2),(visina/2))
blit(TextSurf,TextRect)
pygame.display.update()
时间。睡眠(2)
博弈(循环)
定义n_uhdatili():
信息显示(“你没有抓到苹果!”)
def gajba(x,y):
游戏显示.blit(cartImg,(x,y))
def game_loop():
#关于苹果。
宽度=50
高度=82
apple\u startx=random.randrange(0,宽度-apple\u宽度)
苹果星=-600
苹果_速度=8
x_车=(宽度*0.45)
y_车=(高度*0.8)
x_变化=0
大车宽度=193
轿厢高度=105
gameExit=False
不退出游戏时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
gameEXit=True
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
x_变化=-8
elif event.key==pygame.K_RIGHT:
x_变化=8
elif event.tyoe==pygame.K_q:
pygame.quit()
退出
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_左或event.key==pygame.K_右:
x_变化=0
x_购物车+=x_变化
游戏显示。填充(白色)
jabuke(苹果之星,苹果之星)
apple_starty+=apple_速度
如果apple_starty>高度:
n_uhdatili()
gajba(x_gajbe,y_gajbe)
如果x_车>宽度-车宽度:
x_车=宽度-车宽度
如果x_车<0:
x_cart=0
#请注意,此语句中必须存在问题。
如果y_车<苹果星+苹果高:
打印(“y交叉”)
如果x_车>苹果星和x_车<苹果星+苹果星宽度或x_车+车宽度>苹果星和x_车+车宽度<苹果星+苹果星高度:
打印(“你抓到一个苹果”)
apple_startx=random.randrange(0,宽度-apple_宽度)
苹果星=-50
pygame.display.update()
时钟滴答(59)
博弈(循环)
pygame.quit()
退出

我标出了问题所在。我不擅长编程,我认为不是每个人都能做到,我的逻辑不好。谢谢你的帮助

您在x坐标上的情况有问题。您希望测试您是否处于该配置中:

apple_startx apple_startx+apple_width | | ____()__ / ( ( ( ( ( \________/ | | |-----------------------------------| x_cart x_cart+cart_width 苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果苹果 | | ____()__ / ( ( ( ( ( \________/ | | |-----------------------------------| x_车x_车+车宽 因此,这将是:

if apple_startx > x_cart and apple_startx + apple_width < x_cart + cart_width:
如果apple_startx>x_购物车和apple_startx+apple_宽度
如果你想问一些问题,请尽量用英文命名变量和函数,否则很难理解你的代码。好的,我会编辑它,马上就到!翻译好了!哇,比你好多了,你在编程方面很有天赋,保持下去;)@如果损坏了这个问题,请考虑通过点击上面的复选框来标记它。