Untitled
unknown
plain_text
2 years ago
5.5 kB
4
Indexable
Never
import pygame import random from pygame.locals import * pygame.init() width = 800 height = 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Eggs") egg_counts = [0 for i in range(0, 16)] font = pygame.font.Font('Oswald-VariableFont_wght.ttf', 32) class Player: def __init__(self, number): self.number = number self.text_img = font.render('0', False, (255, 0, 0)) self.text = self.text_img.get_rect() if number % 2 == 0: self.image = pygame.image.load("player2.png") self.text.x = 100 self.text.y = 550 else: self.image = pygame.image.load("player1.png") self.text.x = 600 self.text.y = 550 self.rectangle = self.image.get_rect() self.rectangle.x = random.randint(0, width - 150) self.rectangle.y = random.randint(0, height - 150) self.points = 0 def draw(self, screen): screen.blit(self.image, self.rectangle) screen.blit(self.text_img, self.text) def move(self, x, y): self.rectangle.x += x self.rectangle.y += y def checkCollisions(self, eggs): for egg in eggs: if self.rectangle.colliderect(egg.rectangle) and self.number % 2 == egg.number % 2: self.points += 1 if self.points >= 2: global gameover gameover = True self.text_img = font.render(str(self.points), False, (255, 0, 0)) eggs.remove(egg) if egg.number % 2 == 0: r = random.choice([2]) eggs.append(Egg()) else: r = random.choice([1]) eggs.append(Egg()) class Egg: def __init__(self): self.number = random.randint(0, 16) if self.number == 1: self.image = pygame.image.load("jajko1.png") elif self.number == 2: self.image = pygame.image.load("jajko2.png") elif self.number == 3: self.image = pygame.image.load("jajko3.png") elif self.number == 4: self.image = pygame.image.load("jajko4.png") elif self.number == 5: self.image = pygame.image.load("jajko5.png") elif self.number == 6: self.image = pygame.image.load("jajko6.png") elif self.number == 7: self.image = pygame.image.load("jajko7.png") elif self.number == 8: self.image = pygame.image.load("jajko8.png") elif self.number == 9: self.image = pygame.image.load("jajko9.png") elif self.number == 10: self.image = pygame.image.load("jajko10.png") elif self.number == 11: self.image = pygame.image.load("jajko11.png") elif self.number == 12: self.image = pygame.image.load("jajko12.png") elif self.number == 13: self.image = pygame.image.load("jajko13.png") elif self.number == 14: self.image = pygame.image.load("jajko14.png") elif self.number == 15: self.image = pygame.image.load("jajko15.png") else: self.image = pygame.image.load("jajko16.png") self.rectangle = self.image.get_rect() self.rectangle.x = random.randint(0, width-50) self.rectangle.y = random.randint(0, height-50) def jajkos(self): if self.number %2 ==0: self.image = pygame.image.load("jajko1.png") else: self.image= pygame.image.load("jajko2.png") def draw(self, screen): screen.blit(self.image, self.rectangle) def jajkos(self): if self.number % 2 == 0: self.image = pygame.image.load("jajko1.png") else: self.image = pygame.image.load("jajko2.png") def draw(self, screen): screen.blit(self.image, self.rectangle) eggs = [Egg(), Egg(), Egg(), Egg(), Egg(), Egg(), Egg(), Egg(), Egg(), Egg(), Egg(), Egg(), Egg(), Egg(), Egg(), Egg()] players = [Player(1), Player(2)] points= [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] gameover = False running = True while running == True: screen.fill((30, 100, 66)) # RGB for event in pygame.event.get(): if event.type == QUIT: running = False if gameover == False: if event.type == KEYDOWN: if event.key == K_a: players[0].move(-10, 0) if event.key == K_d: players[0].move(10, 0) if event.key == K_w: players[0].move(0, -10) if event.key == K_s: players[0].move(0, 10) if event.key == K_LEFT: players[1].move(-10, 0) if event.key == K_RIGHT: players[1].move(10, 0) if event.key == K_UP: players[1].move(0, -10) if event.key == K_DOWN: players[1].move(0, 10) for player in players: player.checkCollisions(eggs) player.draw(screen) for egg in eggs: egg.draw(screen) if gameover : for i in range(0,16): text_img2 = font.render(str(i) + "-" + str(points[i]), False, (0,0,50)) text2 = text_img2.get_rect() text2.x = 10 text2.y = 40*i screen.blit(text_img2,text2) pygame.display.update() pygame.quit()