Beispiel #1
0
 def __init__(self):
     self._running = True
     self._display_surf = None
     self._image_surf = None
     self.player = Player(1)
     self.apple = Apple()
     self.game = Game()
Beispiel #2
0
 def __init__(self, mx, my):
     self.map = [[define.elem["case"]] * my for _ in range(mx)]
     self.mx = mx
     self.my = my
     self.head = [self.mx / 2, self.my / 2]
     self.body = Body()
     self.apple = Apple(self.mx, self.my)
     self.cherry = Cherry(self.mx, self.my)
Beispiel #3
0
def gameNilsas():
    other1 = tk.Toplevel()
    other1.title("The iGame")
    otherlabel = tk.Label(other1,
                          text='Entering The apple game',
                          relief=tk.RIDGE)
    otherlabel.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.YES)
    Apple.start()
Beispiel #4
0
 def __init__(self, bg):
     self.bg = bg
     self.screen = pygame.display.set_mode([Vars.screen_size, Vars.screen_size])
     self.snake_class = Snake.Snake(self.screen)
     self.apple_class = Apple.AppleClass(self.screen, self.snake_class.head)
     self.start = False
     self.score = 0
Beispiel #5
0
def game_loop():
    snake = Settings.Menu.Snake.Snake()
    apple = Apple.Apple()

    while True:
        for event in Settings.pygame.event.get():
            if event.type == Settings.pygame.QUIT:
                Settings.Functions.my_quit()
            elif event.type == Settings.pygame.KEYDOWN:
                if event.key == Settings.pygame.K_UP:
                    snake.point(Settings.UP)
                elif event.key == Settings.pygame.K_DOWN:
                    snake.point(Settings.DOWN)
                elif event.key == Settings.pygame.K_LEFT:
                    snake.point(Settings.LEFT)
                elif event.key == Settings.pygame.K_RIGHT:
                    snake.point(Settings.RIGHT)
                elif event.key == Settings.pygame.K_SPACE:
                    snake.pause()
                    Settings.Menu.pause(snake)

        Settings.surface.fill(Settings.black)
        snake.move()
        Settings.Functions.check_eat(snake, apple)
        snake.draw(Settings.surface)
        apple.draw(Settings.surface)
        Settings.screen.blit(Settings.surface, (0, 0))
        Settings.pygame.display.flip()
        Settings.pygame.display.update()
        Settings.fpsClock.tick(Settings.fps + snake.length / 3)
Beispiel #6
0
def respawnApple(apples, index, sx, sy):
    radius = math.sqrt(
        (Constants.SCREEN_WIDTH / 2 * Constants.SCREEN_WIDTH / 2 +
         Constants.SCREEN_HEIGHT / 2 * Constants.SCREEN_HEIGHT / 2)) / 2
    angle = 999
    while (angle > radius):
        angle = random.uniform(0, 800) * math.pi * 2
        x = Constants.SCREEN_WIDTH / 2 + radius * math.cos(angle)
        y = Constants.SCREEN_HEIGHT / 2 + radius * math.sin(angle)
        if (x == sx and y == sy):
            continue

    c = random.randint(1, 7)
    newApple = FastnerApple(Apple(x, y, 1, Constants.APPLE_SIZE))
    if (c == 1):
        newApple = FastnerApple(Apple(x, y, 1, Constants.APPLE_SIZE))
    elif (c == 2):
        newApple = KillerApple(Apple(x, y, 1, Constants.APPLE_SIZE))
        Constants.type = type(newApple)
        a = pygame.time.get_ticks()
        Constants.sTime = a

    elif (c == 3):
        newApple = OnePointApple(Apple(x, y, 1, Constants.APPLE_SIZE))
    elif (c == 4):
        newApple = SilverLowerApple(Apple(x, y, 1, Constants.APPLE_SIZE))
    elif (c == 5):
        newApple = SlowerApple(Apple(x, y, 1, Constants.APPLE_SIZE))
    elif (c == 6):
        newApple = ThreePointApple(Apple(x, y, 1, Constants.APPLE_SIZE))

    apples[index] = newApple
Beispiel #7
0
class Board():
    def __init__(self, mx, my):
        self.map = [[define.elem["case"]] * my for _ in range(mx)]
        self.mx = mx
        self.my = my
        self.head = [self.mx / 2, self.my / 2]
        self.body = Body()
        self.apple = Apple(self.mx, self.my)
        self.cherry = Cherry(self.mx, self.my)

    def init_board(self):
        self.body.init_body(self.head)
        self.apple.set_apple(self.map)
        self.cherry.set_cherry(self.map)
        self.update_board()

    def update_board(self):
        for x in range(self.mx):
            for y in range(self.my):
                self.map[x][y] = define.elem["head"] if [x, y] == self.head \
                    else define.elem["body"] if self.body.is_body([x, y]) \
                    else define.elem["apple"] if self.apple.is_apple([x, y]) \
                    else define.elem["cherry"] if self.cherry.is_cherry_const([x, y]) \
                    else define.elem["case"]

    def get_map(self):
        return self.map

    def get_head(self):
        return self.head

    def set_head(self, x, y):
        self.head = [x, y]

    def is_wall(self, p):
        return p[0] < 0 or p[0] > self.mx or p[1] < 0 or p[1] > self.my

    def deb(self):
        for x in range(self.mx):
            for y in range(self.my):
                print('{} '.format(self.map[x][y]), end="")
            print()
Beispiel #8
0
    def __init__(self,
                 index,
                 canvas,
                 screen,
                 step=20,
                 showGrid=True,
                 log=True):
        self.clock = pygame.time.Clock()

        self.canvas = canvas
        self.screen = screen
        self.showGrid = showGrid
        self.step = step
        self.index = index
        self.log = log
        self.grid = [i // step for i in canvas[2:]]

        self.printMessage('Initialize grid:', self.grid)

        self.snake = Snake(index, [i // 2 for i in self.grid], step, self.grid,
                           3, log)
        self.apple = Apple(index, self.grid, step, log)
Beispiel #9
0
 def create_apple_list(self):
     for line in self.csv_list:
         apple_data = self.line_to_apple(line)
         self.apple_list.append(
             Apple.Apple(
                 float(apple_data[0]),
                 apple_data[1],
                 float(apple_data[2]),
                 float(apple_data[3]),
                 float(apple_data[4]),
                 float(apple_data[5]),
             ))
     sys.stdout.write(str(self.csv_list))
     sys.stdout.flush()
Beispiel #10
0
def respawnApples(apples, quantity, sx, sy):
    counter = 0
    del apples[:]
    radius = math.sqrt(
        (Constants.SCREEN_WIDTH / 2 * Constants.SCREEN_WIDTH / 2 +
         Constants.SCREEN_HEIGHT / 2 * Constants.SCREEN_HEIGHT / 2)) / 2
    angle = 999
    while (counter < quantity):
        while (angle > radius):
            angle = random.uniform(0, 800) * math.pi * 2
            x = Constants.SCREEN_WIDTH / 2 + radius * math.cos(angle)
            y = Constants.SCREEN_HEIGHT / 2 + radius * math.sin(angle)
            if ((x - Constants.APPLE_SIZE == sx
                 or x + Constants.APPLE_SIZE == sx) and
                (y - Constants.APPLE_SIZE == sy
                 or y + Constants.APPLE_SIZE == sy) or radius - angle <= 10):
                continue
        apples.append(FastnerApple(Apple(x, y, 1, Constants.APPLE_SIZE)))
        angle = 999
        counter += 1
def gameNilsas():
    other1 = tk.Toplevel()
    other1.title("The iGame")
    otherlabel = tk.Label(other1, text='Entering The apple game', relief = tk.RIDGE)
    otherlabel.pack(side=tk.TOP, fill = tk.BOTH, expand = tk.YES)
    Apple.start()
Beispiel #12
0
def main():
    score = 0

    # wall initialization
    Walls = [
        Wall(0, 0, 15),
        Wall(0, 15, 15),
        Wall(0, 30, 15),
        Wall(0, 45, 15),
        Wall(0, 60, 15),
        Wall(0, 75, 15),
        Wall(0, 90, 15),
        Wall(0, 105, 15),
        Wall(0, 120, 15),
        Wall(0, 135, 15),
        Wall(0, 150, 15),
        Wall(0, 165, 15),
        Wall(0, 300, 15),
        Wall(0, 315, 15),
        Wall(0, 330, 15),
        Wall(0, 345, 15),
        Wall(0, 360, 15),
        Wall(0, 375, 15),
        Wall(0, 390, 15),
        Wall(0, 405, 15),
        Wall(0, 420, 15),
        Wall(0, 435, 15),
        Wall(0, 450, 15),
        Wall(0, 465, 15),
        Wall(785, 0, 15),
        Wall(785, 15, 15),
        Wall(785, 30, 15),
        Wall(785, 45, 15),
        Wall(785, 60, 15),
        Wall(785, 75, 15),
        Wall(785, 90, 15),
        Wall(785, 105, 15),
        Wall(785, 120, 15),
        Wall(785, 135, 15),
        Wall(785, 150, 15),
        Wall(785, 165, 15),
        Wall(785, 300, 15),
        Wall(785, 315, 15),
        Wall(785, 330, 15),
        Wall(785, 345, 15),
        Wall(785, 360, 15),
        Wall(785, 375, 15),
        Wall(785, 390, 15),
        Wall(785, 405, 15),
        Wall(785, 420, 15),
        Wall(785, 435, 15),
        Wall(785, 450, 15),
        Wall(785, 465, 15),
        Wall(90, 0, 15),
        Wall(105, 0, 15),
        Wall(120, 0, 15),
        Wall(135, 0, 15),
        Wall(150, 0, 15),
        Wall(165, 0, 15),
        Wall(180, 0, 15),
        Wall(270, 0, 15),
        Wall(285, 0, 15),
        Wall(300, 0, 15),
        Wall(315, 0, 15),
        Wall(330, 0, 15),
        Wall(345, 0, 15),
        Wall(360, 0, 15),
        Wall(450, 0, 15),
        Wall(465, 0, 15),
        Wall(480, 0, 15),
        Wall(495, 0, 15),
        Wall(510, 0, 15),
        Wall(525, 0, 15),
        Wall(540, 0, 15),
        Wall(630, 0, 15),
        Wall(645, 0, 15),
        Wall(660, 0, 15),
        Wall(675, 0, 15),
        Wall(690, 0, 15),
        Wall(705, 0, 15),
        Wall(720, 0, 15),
        Wall(90, 585, 15),
        Wall(105, 585, 15),
        Wall(120, 585, 15),
        Wall(135, 585, 15),
        Wall(150, 585, 15),
        Wall(165, 585, 15),
        Wall(180, 585, 15),
        Wall(270, 585, 15),
        Wall(285, 585, 15),
        Wall(300, 585, 15),
        Wall(315, 585, 15),
        Wall(330, 585, 15),
        Wall(345, 585, 15),
        Wall(360, 585, 15),
        Wall(450, 585, 15),
        Wall(465, 585, 15),
        Wall(480, 585, 15),
        Wall(495, 585, 15),
        Wall(510, 585, 15),
        Wall(525, 585, 15),
        Wall(540, 585, 15),
        Wall(630, 585, 15),
        Wall(645, 585, 15),
        Wall(660, 585, 15),
        Wall(675, 585, 15),
        Wall(690, 585, 15),
        Wall(705, 585, 15),
        Wall(720, 585, 15)
    ]

    error = None
    Resume = None

    # Snake initialization
    mySnake = Snake(Constants.SCREEN_WIDTH / 2, Constants.SCREEN_HEIGHT / 2)
    mySnake.setDirection(Constants.KEY["UP"])
    mySnake.move()
    strb = 3
    start_segments = 3
    while (start_segments > 0):
        mySnake.grow()
        mySnake.move()
        start_segments -= 1

    # Apples initialization
    max_apples = 1
    eaten_apple = False

    apples = [
        FastnerApple(
            Apple(random.randint(60, 300), random.randint(60, 300), 1,
                  Constants.APPLE_SIZE))
    ]
    respawnApples(apples, max_apples, mySnake.x, mySnake.y)
    global startTime, menuTimer
    startTime = time.clock()
    endgame = False

    while not endgame:
        gameClock.tick(Constants.FPS)

        # Input
        KEYPress = getKEY()
        if KEYPress == "exit":
            endgame = True
        elif KEYPress == "menu":
            g = menu.runMenu(screen)

            endgame = g
            if (endgame == 'cagır'):
                main()
            if (endgame == 'save'):
                screen.fill(background_color)
                array = []
                nickname = []
                skr = []
                with open("saveFile.txt", "r") as ins:
                    for line in ins:
                        index = line.find(' ')
                        this = line.find('\r')
                        nickname.append(line[0:index])
                        skr.append(line[index:this])

                drawSave(Nmaxelements(skr, nickname, 10))
            drawScore(score)
            c = mySnake.getHead()

            slen = len(mySnake.stack) / 2 - 1

            main2(c.x, c.y, c.direction, apples, slen, score)
        elif (KEYPress == "yes" or KEYPress == "no" or KEYPress == "save"):
            pass
        # Collision check

        Constants.checkLimits(mySnake)
        if (mySnake.checkCrash() == True):
            endGame(score)

        for myApple in apples:
            if (myApple.state == 1):
                if (Constants.checkCollision(mySnake.getHead(),
                                             Constants.SNAKE_SIZE, myApple,
                                             Constants.APPLE_SIZE) == True):
                    if (type(myApple) == OnePointApple):
                        mySnake.grow()
                        myApple.state = 0
                        score += 5
                    elif (type(myApple) == FastnerApple):
                        Constants.FPS += 1
                    elif (type(myApple) == KillerApple):
                        endGame(score)
                    elif (type(myApple) == SilverLowerApple):
                        mySnake.remove()

                        if ((len(mySnake.stack) - 1) / 2 < strb):
                            score -= 5
                        if ((len(mySnake.stack) - 1) / 2 <= 0):
                            endGame(score)
                    elif (type(myApple) == SlowerApple):
                        Constants.FPS -= 2
                        if (Constants.FPS <= 25):
                            Constants.FPS = 25
                    elif (type(myApple) == ThreePointApple):
                        mySnake.grow()
                        mySnake.grow()
                        mySnake.grow()
                        score += 15
                    eaten_apple = True

        for wal in Walls:
            if (Constants.checkCollision(mySnake.getHead(),
                                         Constants.SNAKE_SIZE, wal,
                                         wal.size) == True):
                endGame(score)

        # Position Update
        if (KEYPress):
            mySnake.setDirection(KEYPress)
        mySnake.move()

        if Constants.type == KillerApple and abs(pygame.time.get_ticks() -
                                                 Constants.sTime) > 5000:
            eaten_apple = False
            Constants.type = None
            respawnApple(apples, 0, mySnake.getHead().x, mySnake.getHead().y)

        # Respawning apples
        if (eaten_apple == True and Constants.type == None):
            eaten_apple = False
            respawnApple(apples, 0, mySnake.getHead().x, mySnake.getHead().y)

        # Drawing
        screen.fill(background_color)
        for wel in Walls:
            wel.draw(screen)

        for myApple in apples:
            if (myApple.state == 1):
                if (type(myApple) == KillerApple):
                    pass
                myApple.draw(screen)

        mySnake.draw(screen)

        drawScore(score)

        gameTime = time.clock() - startTime

        drawGameTime(gameTime - Constants.menuTimes)
        Constants.menuTimes = 0

        pygame.display.flip()
        pygame.display.update()
Beispiel #13
0
import pygame
import pygame.gfxdraw
import Level
import Apple

lv = Level.level()
apple = Apple.apple(lv.x, lv.y)
Beispiel #14
0
class App:
    windowWidth = 800
    windowHeight = 600
    player = 0
    apple = 0

    def __init__(self):
        self._running = True
        self._display_surf = None
        self._image_surf = None
        self.player = Player(1)
        self.apple = Apple()
        self.game = Game()

    def on_init(self):
        pygame.init()
        self._display_surf = pygame.display.set_mode(
            (self.windowWidth, self.windowHeight), pygame.HWSURFACE)
        pygame.display.set_caption('SnakePyGame')
        self._running = True

        self._image_surf = pygame.image.load(
            "../static/green-square.png").convert()
        self._apple_surf = pygame.image.load("../static/logo.png").convert()

    def score(self, input_text, font_size, width, height):
        text = pygame.font.Font('freesansbold.ttf', font_size)
        text_surf = text.render(str(input_text), True, (255, 255, 255))
        #text_rect = text_surf.get_rect().center = (width, height)
        self._display_surf.blit(text_surf, (width, height))  #text_rect)
        pygame.display.update()

    def on_event(self, event):
        if event.type == QUIT:
            self._running = False

    def crash(self):
        self.score("YOU LOSE!", 150, self.windowWidth / 50,
                   self.windowHeight / 2.7)
        self.score("Your Score: " + str(self.player.length - 1), 70,
                   (self.windowWidth / 4.2), (self.windowHeight / 1.5))

    def on_loop(self):
        self.player.update()
        apple_step = 25
        eat_flag = True
        flag = True

        #snake eat apple
        for i in range(0, self.player.length):
            if self.game.is_collision(self.apple.x, self.apple.y,
                                      self.player.x[i], self.player.y[i],
                                      apple_step):
                if i != 0:
                    self.game.apple_collision(self.apple, self.player,
                                              apple_step)
                elif i == 0:
                    self.game.apple_collision(self.apple, self.player,
                                              apple_step)
                    self.player.length = self.player.length + 1

        #find collision
        for i in range(2, self.player.length):
            if self.game.is_collision(self.player.x[0], self.player.y[0],
                                      self.player.x[i], self.player.y[i], 40):
                print("You lose! Collision: ")
                print("x[0] (" + str(self.player.x[0]) + "," +
                      str(self.player.y[0]) + ")")
                print("x[" + str(i) + "] (" + str(self.player.x[i]) + "," +
                      str(self.player.y[i]) + ")")
                self.crash()
                sleep(3)
                exit(0)

        if self.game.out_of_map(self.player.x[0], self.player.y[0],
                                self.windowWidth, self.windowHeight):
            self.crash()
            sleep(3)
            exit(0)

    def on_render(self):
        self._display_surf.fill((0, 0, 0))
        self.player.draw(self._display_surf, self._image_surf)
        self.apple.draw(self._display_surf, self._apple_surf)
        #self.score("Score: " + str(self.player.length - 1), 25, (self.windowWidth / 1.2), (self.windowHeight / 50))
        pygame.display.flip()

    def on_cleanup(self):
        pygame.quit()

    def on_execute(self):
        right_flag = True
        left_flag = True
        up_flag = True
        down_flag = True

        if self.on_init() is False:
            self._running = False

        while (self._running):
            pygame.event.pump()
            keys = pygame.key.get_pressed()

            for event in pygame.event.get():
                self.on_event(event)

            if (keys[K_RIGHT] and right_flag):
                self.player.moveRight()
                left_flag = False
                up_flag = True
                down_flag = True

            if (keys[K_LEFT] and left_flag):
                self.player.moveLeft()
                right_flag = False
                up_flag = True
                down_flag = True

            if (keys[K_UP] and up_flag):
                self.player.moveUp()
                down_flag = False
                left_flag = True
                right_flag = True

            if (keys[K_DOWN] and down_flag):
                self.player.moveDown()
                up_flag = False
                right_flag = True
                left_flag = True

            if (keys[K_ESCAPE]):
                self._running = False

            self.on_loop()
            self.on_render()
            #self.score("Score: " + str(self.player.length - 1), 25, (self.windowWidth / 1.2), (self.windowHeight / 50))
            #pygame.display.update()
            sleep(50.0 / 1000.0)
        self.on_cleanup()
Beispiel #15
0
from Apple import *
# from Apple import Apple
# import Apple

# main

print "Here are your fruits:"
apple = Apple()
apple.printState()

papple = Papple()
papple.print_status()
papple.increment_count()
papple.print_status()
Beispiel #16
0
import Netflix
import YouTube
import GMedia
import Outside
import Mainland
import Apple
import ad_lite

print("去广告工作开始")
os.system('python ./ad.py')
print("去广告精简版工作开始")
os.system('python ./ad_lite.py')
print("CMedia工作开始")
CMedia.mainchange()
print("Microsoft工作开始")
Microsoft.change()
print("Netflix工作开始")
Netflix.change()
print("YouTube工作开始")
YouTube.change()
print("GMedia工作开始")
GMedia.mainchange()
print("Outside工作开始")
Outside.mainchange()
print("Mainland工作开始")
Mainland.mainchange()
print("Apple工作开始")
Apple.mainchange()

print("所有工作都完成")
Beispiel #17
0
class SnakeGame:
    canvas = (0, 0, 500, 500)
    grid = None
    step = 10

    SPEED = 20
    run = True

    def __init__(self,
                 index,
                 canvas,
                 screen,
                 step=20,
                 showGrid=True,
                 log=True):
        self.clock = pygame.time.Clock()

        self.canvas = canvas
        self.screen = screen
        self.showGrid = showGrid
        self.step = step
        self.index = index
        self.log = log
        self.grid = [i // step for i in canvas[2:]]

        self.printMessage('Initialize grid:', self.grid)

        self.snake = Snake(index, [i // 2 for i in self.grid], step, self.grid,
                           3, log)
        self.apple = Apple(index, self.grid, step, log)

    def printMessage(self, *message):
        if self.log:
            print(f'\033[1;33;40mGame[{self.index}]:\033[0m', *message)

    def listenToEvents(self, events):
        step = None

        for event in events:
            if (event.type == pygame.QUIT):
                self.run = False
            elif event.type == pygame.KEYDOWN:

                if event.key == pygame.K_w:
                    self.SPEED += 10
                    self.printMessage('Speed:', self.SPEED)

                elif event.key == pygame.K_s:
                    self.SPEED -= 10 if self.SPEED - 10 > 0 else 0
                    self.printMessage('Speed:', self.SPEED)

    def moveSnake(self, direction):
        self.snake.move(direction, self.apple.pos, self.apple.setPos)

    def drawGrid(self):
        color = (150, ) * 3
        [x, y, w, h] = self.canvas

        if self.showGrid:
            for i in range(self.grid[1]):
                dy = self.step * i

                for j in range(1, self.grid[0]):
                    dx = self.step * j

                    pygame.draw.line(self.screen, color, (x + dx, y),
                                     (x + dx, y + h - 1))
                    pygame.draw.line(self.screen, color, (x, y + dy),
                                     (x + w - 1, y + dy))

        pygame.draw.rect(self.screen, (255, ) * 3, (x, y, w, h), 1)

    def clean(self):
        # Fill canvas with black
        if self.screen:
            pygame.draw.rect(self.screen, (0, ) * 3, self.canvas)

    def draw(self, events, getDirection):
        if not self.run:
            if self.screen:
                pygame.draw.rect(self.screen, (0, ) * 3, self.canvas)
            return

        self.clean()
        self.clock.tick(self.SPEED)

        # Event Handler
        # events = pygame.event.get()
        self.listenToEvents(events)
        self.moveSnake(getDirection(events))

        # Draw
        if self.screen:
            self.drawGrid()
            self.snake.draw(self.screen, self.canvas[:2])
            self.apple.draw(self.screen, self.canvas[:2])

        # Check if it a game over
        if not self.snake.alive:
            self.run = False

        # Flip the display
        if self.screen:
            pygame.display.flip()
Beispiel #18
0
os.environ['SDL_VIDEO_CENTERED'] = '1'

pygame.mixer.pre_init(44100, -16, 1, 512)
pygame.init()
pygame.display.set_caption("Sssssnake")

# 10x10 segments
winwidth = 200
winheight = 240
win = pygame.display.set_mode((winwidth, winheight))

# segment - 20x20px
segsize = 20
snake = Snake.Snake(0, 20, segsize, segsize-2, segsize-2)
apple = Apple.Apple(segsize//2, winwidth, winheight, segsize, snake.segments)

# font
font = pygame.font.SysFont("monospace", 15)

# sounds
eatsound = pygame.mixer.Sound('sounds/eat sound.wav')
losesound = pygame.mixer.Sound('sounds/lose sound.wav')
music = pygame.mixer.music.load('sounds/bg music.mp3')
pygame.mixer.music.play(-1)


def lost():
    pygame.mixer.music.stop()
    losesound.play()
    global win, running, snake, score