Untitled
unknown
plain_text
a year ago
3.0 kB
3
Indexable
import pygame import sys import random import time WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) FOX_SPEED = 1 pygame.init() game_window = pygame.display.set_mode((800, 600)) class Fox(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.image.load('fox.png') self.rect = pygame.Rect(100, 100, self.image.get_width(), self.image.get_height()) def move(self): keys = pygame.key.get_pressed() if keys[pygame.K_RIGHT] and self.rect.x <= 800: self.rect.x += FOX_SPEED if keys[pygame.K_LEFT] and self.rect.x >= 0: self.rect.x -= FOX_SPEED if keys[pygame.K_UP] and self.rect.y >= 0: self.rect.y -= FOX_SPEED if keys[pygame.K_DOWN] and self.rect.y <= 600: self.rect.y += FOX_SPEED #TRY WITH NORMAL INDENTS???? class Fruit(pygame.sprite.Sprite): def __init__(self, fruit_type): super().__init__() fruit_x = random.randint(1, 30) * 10 fruit_y = random.randint(1, 30) * 10 #TRY (1,20)??? self.image = pygame.image.load('apple.png') self.fruit_type = "Apple" self.rect = pygame.Rect(fruit_x, fruit_y, self.image.get_width(), self.image.get_height()) self.value = 1 #?????????? def detect_collision(self): global score; if pygame.sprite.collide_rect(fox, fruit): # be sure to define fox and fruit somewhere score += self.value return True return False class Trap(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.image.load('trap.png') trap_x = random.randint(1, 30) * 10 trap_y = random.randint(1, 30) * 10 #TRY (1,20)??? self.rect = pygame.Rect(trap_x, trap_y, self.image.get_width(), self.image.get_height()) def detect_collision(self): if pygame.sprite.collide_rect(fox, trap): return True return False ##what does this do?????????? score = 0 fox = Fox() fruit = Fruit("Apple") trap = Trap() while True: game_window.fill(WHITE) game_window.blit(fox.image, fox.rect) game_window.blit(fruit.image, fruit.rect) game_window.blit(trap.image, trap.rect) score_font = pygame.font.SysFont('times new roman', 20) score_surface = score_font.render('Score : ' + str(score), True, BLACK) game_window.blit(score_surface, (200, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() fox.move() fruit_catch = fruit.detect_collision() if fruit_catch == True: fruit=Fruit("Apple") trapped = trap.detect_collision() if trapped == True: game_window.fill(BLACK) over_font = pygame.font.SysFont('times new roman', 50) game_over_surface = over_font.render('Your Score is : ' + str(score), True, RED) game_window.blit(game_over_surface, (150, 300)) #D isplay the text for final score pygame.display.update() time.sleep(2) # Wait for 2 seconds pygame.quit() # Quit Pygame sys.exit() # Exit the program pygame.display.update()
Editor is loading...
Leave a Comment