wallmart snake

^
 avatar
unknown
python
21 days ago
4.6 kB
8
Indexable
""" 
author: Adiv Goldberg
date:
program:
1)
"""
# import the pygame module
import pygame, random

# will make it easier to use pygame functions
from pygame.draw import line, circle, rect
from random import randint

# initializes the pygame module
pygame.init()
SCREEN_X = 800
SCREEN_Y = 600

# creates a screen variable of size 800 x 600
screen = pygame.display.set_mode([SCREEN_X,SCREEN_Y])

# controls the main game while loop
done = False

# sets the frame rate of the program
clock = pygame.time.Clock()

# colour variables, (R, G, B) from 0-255
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
ORANGE = (255, 127, 0)

#CLASSES
class BALL():
    def __init__(self,x,y,r):
        self.x = x
        self.y = y
        self.r = r
        self.vx = 0
        self.vy = 0
        self.score = 0
        self.timer = 0
        
    def go(self):
        self.draw()
        self.move()
        self.timercount()
        self.print_labels()
        
    def draw(self):
        circle(screen, BLACK, (self.x, self.y), self.r, 0)
        
    def move(self):
        if key[pygame.K_RIGHT]:
            self.vx = 5
            self.vy = 0
        if key[pygame.K_LEFT]:
            self.vx = -5
            self.vy = 0
        if key[pygame.K_DOWN]:
            self.vx = 0
            self.vy = 5
        if key[pygame.K_UP]:
            self.vx = 0
            self.vy = -5

        self.x += self.vx
        self.y += self.vy

        if self.x - self.r > SCREEN_X:
            self.x = -self.r
        elif self.x + self.r < 0:
            self.x = SCREEN_X + self.r

        if self.y - self.r > SCREEN_Y:
            self.y = -self.r
        elif self.y + self.r < 0:
            self.y = SCREEN_Y + self.r
            
    def checkhit(self):
        is_hit = distance(self.x, self.y, hollow.x, hollow.y) <= (self.r + hollow.r)
        if is_hit:
            self.score += 1
            
        return is_hit
    
    def timercount(self):
        self.timer += 1/60
        
    def print_labels(self):
        timerLabel = font1.render(f"timer: {round(self.timer,1)}", 1, (200, 0, 0))
        screen.blit(timerLabel, (10, 10))
        
        score_label = font1.render(f"score: {self.score}", 1, (0, 200, 0))
        screen.blit(score_label, (10, 20 + timerLabel.get_height()))
        
    
class HOLLOW():
    def __init__(self):
        self.r = 0
        self.x = 0
        self.y = 0
        
    def go(self):
        self.draw()
        
    def reset(self):
        self.r = 25
        self.x = random.randint(self.r, SCREEN_X - self.r) 
        self.y = random.randint(self.r, SCREEN_Y - self.r)
        
    def draw(self):
        circle(screen, RED, (int(self.x), int(self.y)), int(self.r), 5)
    
    


# your FUNCTIONS go here
#calculate distance between two points (x1,y1) and (x2,y2)
def distance(x1, y1, x2, y2):
    return ((x1-x2)**2 + (y1-y2)**2)**0.5


# your GLOBAL variables go here
ball = BALL(SCREEN_X*0.5,SCREEN_Y*0.5,25)
hollows = [HOLLOW()]
hollows[0].reset()

font1 = pygame.font.SysFont('None', 50)


# MAIN LOOP
pygame.display.set_caption("lesson:")
while not done:
    # makes the background the colour WHITE
    screen.fill(WHITE)

    xMouse, yMouse = pygame.mouse.get_pos()
    clicks = pygame.mouse.get_pressed()

    key = pygame.key.get_pressed()


    
    ball.go()
    for hollow in hollows:
        hollow.go()
    
    for hollow in hollows:
        if ball.checkhit():
            hollow.__init__()
            
    if all(hollow.r == 0 for hollow in hollows):
        ball.timer = 0
        for hollow in hollows:
            hollow.reset()
            
            
    if ball.score % 5 == 0 and ball.score/5 >= len(hollows):
        hollows.append(HOLLOW())
        



    






    # this code allows you to close the window and end the program
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
            pass
        if event.type == pygame.MOUSEBUTTONDOWN:
            pass
        if event.type == pygame.KEYUP:
            pass
        

    # this line draws everything into the window all at once
    pygame.display.flip()
    # this line limits the frames per second to 60
    clock.tick(60)
    
pygame.quit()
Editor is loading...
Leave a Comment