Я смотрел SentDex, youtuber, о учебнике pygame, который стал "простой" игрой ... Поэтому я решил отремонтировать ее, чтобы сделать свою, но с той же квартирой, что и его. Когда вы играете, яблоки падают с деревьев и вы должны поймать их с тележкой, однако, когда я хочу поймать его, поймайте яблоко, но он не пройдет, когда яблоко попадает в край. Вот код:
import pygame
import time
import randompygame.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()
Я отметил, где проблема. Я не умею программировать, я думаю, что не все могут это сделать, моя логика плоха. Спасибо за помощь!
В вашем состоянии проблема с координатами x. Вы хотите проверить, находитесь ли вы в этой конфигурации:
, поэтому это будет: