2017-04-24 7 views
0

현재 프로젝트 용 게임을 만들어야하는 A 레벨 컴퓨터 과학 과정을 수강하고 있습니다. 이 코드는 아마도 효율적이지는 않지만 내 작업을 유지하려고 노력 중이며 두 비트가 손에 들고 싶습니다.스크롤링 제한

이 코드는 현재 소개 화면과 게임은 왼쪽, 오른쪽으로 스크롤하고 플레이어가 화면에서 떨어지면 생명력을 제거합니다. 모든 생명이 사라지면 화면에 게임이 표시됩니다

플레이어가 왼쪽으로 이동하면 특정 지점에서 스크롤을 멈추고 중지하도록 스크롤하는 화면에 제한을 설정하는 방법이 있는지 궁금합니다. 플레이어가 떨어지는 것을 막기 위해 화면 가장자리에있는 플레이어. 내가 가장자리로 맨 아래 플랫폼의 왼쪽을 설정하려면이 바로 가기로 플레이어를 강제

더 이상 당신이 필요로하는 정보가있는 경우 지연에게

을 줄일 수있는 방법이 있다면 또 다른 작은 하나입니다 어떤 제안이 나는이 문제가 자신을 내가 간단히 내가 무슨 짓을했는지 설명 할 것 완료하기 위해 관리

import pygame as pg 
import time 
import random 


pg.init()#initiates pygame 

display_height = 690#Creates width and height of screen 
display_width = 1024 

#Colours 
white = (255,255,255) 
black = (0,0,0) 
red = (255,0,0) 
green = (0,255,0) 
blue = (0,0,255) 
grass = (24,85,36) 
yellow = (255,255,0) 
lightGrey = (184,184,184) 
grey = (73,71,65) 

Robot_height = 99#Height of robot 
Robot_width = 112#Width of robot 
Bullet_Fired = False 
PowerUp_Active = False 
Robot_acc = 0.3 #Robot Acceleration 
vec = pg.math.Vector2 

gameDisplay = pg.display.set_mode((display_width,display_height)) #Sets display properties of window 
pg.display.set_caption ("Game") #Title on window 
clock = pg.time.Clock() 
robotImg = pg.image.load("robot1.png") #Loads robots image 
lifeImg = pg.image.load("Life.png")#Loads image from folder 
lifeImg = pg.transform.scale(lifeImg, (80, 80))#Sets dimensions of image 
backgroundImg = pg.image.load("Background.png")#Loads background image 
backgroundImg = pg.transform.scale(backgroundImg, (display_width, display_height))#Sets dimensions of background to fit the screen 
inBackgroundImg = pg.image.load("IntroBackground1.png")#Loads intro background 
controlsImg = pg.image.load("controls.png")#Loads controls screen background 
controlsImg = pg.transform.scale(controlsImg, (display_width, display_height))#Sets dimensions to fit screen 
largeText = pg.font.Font("Font.ttf",77)#Large text set 
smallText = pg.font.Font("Font.ttf",32)#Small text set 

#Class for platforms 
class Platform(pg.sprite.Sprite): 
    def __init__(self, x,y,w,h): 
     pg.sprite.Sprite.__init__(self) 
     self.image = pg.Surface((w,h))#sets width and height of platform 
     self.image.fill(grass)#Fills rectangle with blue 
     self.rect = self.image.get_rect()#Rectangle set 
     self.rect.x = x#x position 
     self.rect.y = y#y position 

#List of platforms 
PLATFORM_LIST = [[0,display_height - 40,display_width,40], 
       [display_width + 300,display_height - 40,display_width,40], 
       [display_width /2 - 50,display_height * 3/4,100,20], 
       [display_width -10,display_height * 3/4,100,20]] 
#Platform group 
platforms = pg.sprite.Group() 

#Checks through "PLATFORM_LIST" and adds all the platforms the the grounp "platforms" 
for plat in PLATFORM_LIST: 
    p = Platform(*plat) 
    platforms.add(p) 

#Draws platforms to the screen 
def draw(): 
    for plat in platforms: 
     pg.draw.rect(gameDisplay, grass, plat) 

#Class for robot 
class RobotClass(pg.sprite.Sprite): 
    def __init__(self): 
     pg.sprite.Sprite.__init__(self) 
     self.image = pg.Surface((Robot_width,Robot_height))#Height and width of the robot 
     self.rect = self.image.get_rect()#Gets rectangle of robot 
     self.rect.center = (display_width/2, display_height/2)#Location of center of robot 
     self.RobotPos = vec(display_width/2, display_height/2)#Position of robot as vector 
     self.bottom = (0,0)#Bottom of robot 
     self.vel = vec(0, 0)#Robots velocity 
     self.acc = vec(0, 0.3)#Robots Acceleration 

startX = display_width/2 
startY = display_height/2 

#Creates Robot 
Robot = RobotClass() 

#Jump function 
def jump(): 
    #Checks pixel below robot to see if there is a collision 
    Robot.rect.x = Robot.rect.x +1 
    hits = pg.sprite.spritecollide(Robot , platforms, False) 
    Robot.rect.x = Robot.rect.x -1 
    if hits: 
     #Gives robot velocity of 5 upwards 
     Robot.vel.y = -10 

#Settings for text 
def text_objects(text, font): 
    textSurface = font.render(text, True, white) 
    return textSurface, textSurface.get_rect() 

#Displays lives of Robot 
def display_lives(lives): 
    global lifeImg 
    TextSurf, TextRect = text_objects("Lives:", smallText)#Sets text and font 
    TextRect.topleft = (1,16)#Places text in top left of screen 
    gameDisplay.blit(TextSurf, TextRect) #displays text 
    if lives == 3:#Displays 3 spanners 
     gameDisplay.blit(lifeImg,(110,0)) 
     gameDisplay.blit(lifeImg,(160,0)) 
     gameDisplay.blit(lifeImg,(210,0)) 
    if lives == 2:#Displays 2 spanners 
     gameDisplay.blit(lifeImg,(110,0)) 
     gameDisplay.blit(lifeImg,(160,0)) 
    if lives == 1:#Displays 1 spanners 
     gameDisplay.blit(lifeImg,(110,0)) 
    if lives == 0:#Should end game when all lives are lost 
     game_over() 

#Function for button that takes parameters (message,x,y,height,width,active colour,inactive colour,text size,action)  
def button(msg,x,y,w,h,i,a,t,action=None): 
    mouse = pg.mouse.get_pos() #Gets (x,y) position of mouse 
    Text = pg.font.Font("Font.ttf",t) #Lets text size be input 
    click = pg.mouse.get_pressed() #Gets whenther mouse has been clicked (mouse1,middle mouse,mouse2) 

    if x+w > mouse[0] > x and y+50 > mouse[1] > y:#Checks to see if mouse is within boundaries of button 
     pg.draw.rect(gameDisplay, a,(x,y,w,h),5)#Changes color of box when being hovered over 
     if click[0] == 1 and action != None:#If mouse1 is clicked and there is an action to be performed it will be run 
      action()#action is parameter and the() is used to initiate function 
    else: 
     pg.draw.rect(gameDisplay, i,(x,y,w,h),5)#Sets default button 

    TextSurf, TextRect = text_objects(msg, Text)#Runs text_objects with inputted parameter of the message 
    TextRect.center = ((x+(w/2)),(y+(h/2)))#Places text inside the button 
    gameDisplay.blit(TextSurf, TextRect)#Displays text 


def Exit():#Used for button on intro screen to exit 
    pg.quit() 
    quit() 

#What happens when player runs out of lives 
def game_over(): 
    while True: 
     for event in pg.event.get():#lets window be closed 
      if event.type == pg.QUIT: 
       pg.quit() 
       quit() 
     gameDisplay.blit(inBackgroundImg, (0, 0)) 
     TextSurf, TextRect = text_objects(("Game"), largeText)#Places large text on screen 
     TextRect.center = ((display_width/2+10),(180))#location of text 
     gameDisplay.blit(TextSurf, TextRect) 
     TextSurf, TextRect = text_objects(("Over"), largeText) 
     TextRect.center = ((display_width/2+10),(280)) 
     gameDisplay.blit(TextSurf, TextRect) 
     pg.draw.rect(gameDisplay, yellow,(290,125,445,220),5)#Puts yellow box over game over 

     button("Retry",410,600,207,70,yellow,lightGrey,45,game_loop) 
     button("Quit",display_width-105,display_height-55,100,50,yellow,lightGrey,33,Exit) 

     pg.display.update()#updates screen 
     clock.tick(15) 

#Function for control screen 
def controls(): 
    while True: 
     for event in pg.event.get():#lets window be closed 
      if event.type == pg.QUIT: 
       pg.quit() 
       quit() 
     gameDisplay.blit(controlsImg, (0, 0))#Places controlsImg on screen 
     button("Back",5,display_height-55,100,50,black,grey,30,game_intro)#places back button in bottom left corner 

     pg.display.update()#updates screen 
     clock.tick(15)#sets clock rate 


#Function to gun the intro screen 
def game_intro(): 
    while True: 
     for event in pg.event.get():#lets window be closed 
      if event.type == pg.QUIT: 
       pg.quit() 
       quit() 
     gameDisplay.blit(inBackgroundImg, (0, 0))#Places background image 
     TextSurf, TextRect = text_objects(("Lost on planet X"), largeText)#Places large text on screen 
     TextRect.center = ((display_width/2),(200))#location of text 
     gameDisplay.blit(TextSurf, TextRect)#displays text 
     pg.draw.rect(gameDisplay, yellow,(65,130,900,150),5)#Puts yellow box over title 

     button("Play",412,550,200,50,yellow,lightGrey,36,game_loop) #Play button at middle bottom 
     button("Controls",412,450,200,50,yellow,lightGrey,32,controls) #Controls button in middle of screen 
     button("Quit",display_width-105,display_height-55,100,50,yellow,lightGrey,33,Exit) #Quit button in bottom right 


     pg.display.update()#updates screen 
     clock.tick(15)#sets clock rate 

#game loop 
def game_loop(): 
    global PLATFORM_LIST 
    global startX 
    global startY 
    global backgroundImg 
    Robot.RobotPos = (startX,startY) 
    lives = 3 #Robot Lives 
    Robot_friction = -0.3 #Friction value 
    vec = pg.math.Vector2 #Setting vec as vector quantity 
    while True: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       pg.quit 
       quit() 
      #Starts acceleration when key is pressed 
      if event.type == pg.KEYDOWN: 
       if event.key == pg.K_LEFT: 
        Robot.acc.x = -Robot_acc 
       elif event.key == pg.K_RIGHT: 
        Robot.acc.x = Robot_acc 
       elif event.key == pg.K_UP: 
        jump() 
      #Adds friction to accleration to slow robot down when key is not being pressed 
      if event.type == pg.KEYUP: 
       if event.key == pg.K_LEFT or event.key == pg.K_RIGHT: 
        Robot.acc.x = Robot.acc.x * Robot_friction 

     #Adjusts velocity of robot by adding the acceleration on each cycle 
     Robot.vel = Robot.vel+ Robot.acc 
     #gameDisplay.fill(sky) 
     gameDisplay.blit(backgroundImg,(0,0)) 
     #Draws the platforms to the screen and adds them to platform group 
     draw() 
     #Changes Robot position according to its velocity,acceleration and the friction 
     Robot.RobotPos = Robot.RobotPos + Robot.vel + 0.5 * Robot.acc 
     #Loads robot onto screen 
     gameDisplay.blit(robotImg,(Robot.rect)) 
     #pg.draw.rect(gameDisplay, red, Robot.rect, 2 
     display_lives(lives)#lives 
     pg.display.update()#Updates display 
     clock.tick(60) 

     #Sets bottom of robot to its position 
     Robot.rect.midbottom = Robot.RobotPos 

     #Collision detection 
     if Robot.vel.y > 0: 
      hits = pg.sprite.spritecollide(Robot , platforms, False) 
      if hits: 
       #Puts Robot on top of platform 
       Robot.RobotPos.y = hits[0].rect.top + 1 
       Robot.vel.y = 0   
     #Scrolling 
     if Robot.rect.left < display_width/4: 
      Robot.RobotPos.x = Robot.RobotPos.x + abs(Robot.vel.x) 
      startX = startX + abs(Robot.vel.x) 
      for plat in platforms: 
       plat.rect.x = plat.rect.x + int(abs(Robot.vel.x)) 
       draw() 
     if Robot.rect.right > (display_width-display_width/4): 
      Robot.RobotPos.x = Robot.RobotPos.x - abs(Robot.vel.x) 
      startX = startX - abs(Robot.vel.x) 
      for plat in platforms: 
       plat.rect.x = plat.rect.x - int(abs(Robot.vel.x)) 
       draw() 

     #Losing a life 
     if Robot.rect.top > display_height: 
      lives = lives - 1 
      Robot.RobotPos.y = Robot.RobotPos.y - (40+Robot_height) 
      Robot.RobotPos.x = Robot.RobotPos.x - 200 
      Robot.vel.x = 0 


     #Sets top velocity of robot  
     if Robot.vel.x > 6: 
      Robot.vel.x = 6 
     if Robot.vel.x < -6: 
      Robot.vel.x = -6 
     #Makes robot velocity = 0 when it is close to 0 
     if Robot.vel.x < 0.05 and Robot.vel.x > -0.05: 
      Robot.acc.x = 0 
      Robot.vel.x = 0 

game_intro() 
pg.quit() 
quit() 
+0

질문 할 때 한 가지 질문 만하십시오. 이렇게하면 답이 줄어들고 질문을 이해하기 쉬워지며 다른 사용자의 검색 가능성이 높아집니다. 또한 SO 질문에 대해 너무 많은 코드를 읽을 수 있으므로 [mcve]를 질문에 포함하고 (필요한 경우) 코드는 포함시키지 마십시오. 그리고 이전 질문에 썼습니다. 이름 지정 규칙을 따르십시오. 그렇지 않으면 이름에 변수, 클래스, 함수 또는 상수가 무엇인지 명확하지 않으므로 코드를 읽고 이해하는 것이 훨씬 어려워집니다. 즉, 발생할 때마다 정의 된 위치를 찾아야합니다. . –

+0

[이] (http://stackoverflow.com/a/14357169/6486738) 그래서 당신이 (당신의 첫 번째 질문에 대한) 찾고있는 것을 찾을거야 대답. –

+0

지연을 줄이려면 매 루프마다 한 번만 화면에 그려야합니다. 게임 루프에서 (draw() 함수를 사용하여) 드로잉을하고, 스크롤을 확인한 후에 ('if Robot.rect.left

답변

0

어떤 도움 감사 감사하겠습니다 을 물어 주시기 도움이됩니다.

스크롤 한 화면이 원래 위치에서 얼마나 떨어져 있는지 추적하는 off set이라는 변수를 추가했습니다.

offset = 0 

는 그때 그때 경우이 기능에 추가를 추가

if Robot.rect.left < display_width/4: 
      Robot.RobotPos.x = Robot.RobotPos.x + abs(Robot.vel.x) 
      startX = startX + int(abs(Robot.vel.x)) 
      offset = offset + int(abs(Robot.vel.x)) 
      for plat in platforms: 
       plat.rect.x = plat.rect.x + int(abs(Robot.vel.x)) 
      for coin in coins: 
       coin.rect.x = coin.rect.x + int(abs(Robot.vel.x)) 

if Robot.rect.right > (display_width-display_width/4): 
      Robot.RobotPos.x = Robot.RobotPos.x - abs(Robot.vel.x) 
      startX = startX - int(abs(Robot.vel.x)) 
      offset = offset - int(abs(Robot.vel.x)) 
      for plat in platforms: 
       plat.rect.x = plat.rect.x - int(abs(Robot.vel.x)) 
      for coin in coins: 
       coin.rect.x = coin.rect.x - int(abs(Robot.vel.x)) 

내 스크롤 코드로이 변수를 추가했다. 오프셋이 0보다 작 으면 코드에서 스크롤 부분 만 실행합니다. 0보다 큰 값은 화면이 왼쪽으로 옵셋되었음을 나타냅니다. 이 코드는 그러므로 단지 화면의 가장자리에 없을 때 스크롤 할 수 있습니다

if offset > 0: 
      pass 
elif Robot.rect.left < display_width/4: 
      Robot.RobotPos.x = Robot.RobotPos.x + abs(Robot.vel.x) 
      startX = startX + int(abs(Robot.vel.x)) 
      offset = offset + int(abs(Robot.vel.x)) 
      for plat in platforms: 
       plat.rect.x = plat.rect.x + int(abs(Robot.vel.x)) 
      for coin in coins: 
       coin.rect.x = coin.rect.x + int(abs(Robot.vel.x)) 

다음 내 플랫폼 기능을 사용하여 가장자리에 벽을 추가에 추가 화면에서 떨어지는 스프라이트를 중지하려면 나중에 더 많은 벽을 사용해야하는 경우를 대비 한 그룹.

w1 = Platform(0,0,10,display_height) 
wall = pg.sprite.Group() 
wall.add(w1) 

이 후 나는이이 일이 스프라이트는 멀리 이동하기 힘들었다 문제가되었다하지만 스프라이트가 벽

hit = pg.sprite.spritecollide(Robot , wall,False) 
     if hit: 
      Robot.vel.x = 0 
      Robot.acc.x = 0 

접촉 여부를 확인하기 위해 spritecollide를 사용하여 조건을 생성 그 속도가 항상 0으로 설정되고 이후 벽 그래서 왼쪽 화살표이 다음만을 재설정

hit = pg.sprite.spritecollide(Robot , wall,False) 
     if hit: 
      if left == True: 
       Robot.vel.x = 0 
       Robot.acc.x = 0 

가압하고 있는지 확인 다른 변수를 추가 값은 내가 무엇을 놓친 경우 함께 여기

if event.type == pg.KEYDOWN: 
       if event.key == pg.K_LEFT: 
        Robot.acc.x = -Robot_acc 
        left = True 
       elif event.key == pg.K_RIGHT: 
        Robot.acc.x = Robot_acc     
       elif event.key == pg.K_UP: 
        jump() 
      elif Robot.acc.x < 0: 
       left = True 
      else: 
       left = False 

를 왼쪽으로 이동하기 위해 내 모든 코드입니다하려는 경우 또는 당신이보고 싶을 다른 부분이있다.

* 편집이 코드는 현재 벽을 스크롤하거나 표시하지 않습니다.화면 이동 중에 로봇이 화면 중앙에 있어야 벽이 여전히 존재하지만 충돌 할 수있는 방법이 없기 때문에 보이지 않아 볼 수 없으므로 스크롤은 중요하지 않습니다.

부품을 잘 설명하지 않았거나 추가 정보가 필요하면 질문하십시오.

import pygame as pg 
import time 
import random 


pg.init()#initiates pygame 

display_height = 690#Creates width and height of screen 
display_width = 1024 

#Colours 
white = (255,255,255) 
black = (0,0,0) 
red = (255,0,0) 
green = (0,255,0) 
blue = (0,0,255) 
grass = (24,85,36) 
yellow = (255,255,0) 
lightGrey = (184,184,184) 
grey = (73,71,65) 
brown = (66, 40, 13) 

Robot_height = 99#Height of robot 
Robot_width = 112#Width of robot 
Bullet_Fired = False 
PowerUp_Active = False 
Robot_acc = 0.3 #Robot Acceleration 
vec = pg.math.Vector2 

gameDisplay = pg.display.set_mode((display_width,display_height)) #Sets display properties of window 
pg.display.set_caption ("Game") #Title on window 
clock = pg.time.Clock() 
robotImg = pg.image.load("robot1.png") #Loads robots image 
lifeImg = pg.image.load("Life.png")#Loads image from folder 
lifeImg = pg.transform.scale(lifeImg, (80, 80))#Sets dimensions of image 
backgroundImg = pg.image.load("Background.png")#Loads background image 
backgroundImg = pg.transform.scale(backgroundImg, (display_width, display_height))#Sets dimensions of background to fit the screen 
inBackgroundImg = pg.image.load("IntroBackground1.png")#Loads intro background 
controlsImg = pg.image.load("controls.png")#Loads controls screen background 
controlsImg = pg.transform.scale(controlsImg, (display_width, display_height))#Sets dimensions to fit screen 
largeText = pg.font.Font("Font.ttf",77)#Large text set 
smallText = pg.font.Font("Font.ttf",32)#Small text set 

#Class for platforms 
class Platform(pg.sprite.Sprite): 
    def __init__(self, x,y,w,h): 
     pg.sprite.Sprite.__init__(self) 
     self.image = pg.Surface((w,h))#sets width and height of platform 
     self.image.fill(grass)#Fills rectangle with blue 
     self.rect = self.image.get_rect()#Rectangle set 
     self.rect.x = x#x position 
     self.rect.y = y#y position 

#List of platforms x , y , width , height 
PLATFORM_LIST = [[-5,display_height - 40,2005,40], 
       [2300,display_height - 40,1000,40], 
       [1100,display_height - 190,300,20], 
       ] 
#Platform group 
platforms = pg.sprite.Group() 

w1 = Platform(0,0,10,display_height) 
wall = pg.sprite.Group() 
wall.add(w1) 

#Checks through "PLATFORM_LIST" and adds all the platforms the the grounp "platforms" 
for plat in PLATFORM_LIST: 
    p = Platform(*plat) 
    platforms.add(p) 

#Draws platforms to the screen 
def draw(): 
    for plat in platforms: 
     pg.draw.rect(gameDisplay, grass, plat) 

#Class for robot 
class RobotClass(pg.sprite.Sprite): 
    def __init__(self): 
     pg.sprite.Sprite.__init__(self) 
     self.image = pg.Surface((Robot_width,Robot_height))#Height and width of the robot 
     self.rect = self.image.get_rect()#Gets rectangle of robot 
     self.rect.center = (display_width/2, display_height/2)#Location of center of robot 
     self.RobotPos = vec(display_width/2, display_height/2)#Position of robot as vector 
     self.bottom = (0,0)#Bottom of robot 
     self.vel = vec(0, 0)#Robots velocity 
     self.acc = vec(0, 0.3)#Robots Acceleration 

startX = display_width/2 
startY = display_height/2 

#Creates Robot 
Robot = RobotClass() 

#Jump function 
def jump(): 
    #Checks pixel below robot to see if there is a collision 
    Robot.rect.x = Robot.rect.x +1 
    hits = pg.sprite.spritecollide(Robot , platforms, False) 
    Robot.rect.x = Robot.rect.x -1 
    if hits: 
     #Gives robot velocity of 5 upwards 
     Robot.vel.y = -10 

#Settings for text 
def text_objects(text, font): 
    textSurface = font.render(text, True, white) 
    return textSurface, textSurface.get_rect() 

#Displays lives of Robot 
def display_lives(lives): 
    global lifeImg 
    TextSurf, TextRect = text_objects("Lives:", smallText)#Sets text and font 
    TextRect.topleft = (1,16)#Places text in top left of screen 
    gameDisplay.blit(TextSurf, TextRect) #displays text 
    if lives == 3:#Displays 3 spanners 
     gameDisplay.blit(lifeImg,(110,0)) 
     gameDisplay.blit(lifeImg,(160,0)) 
     gameDisplay.blit(lifeImg,(210,0)) 
    if lives == 2:#Displays 2 spanners 
     gameDisplay.blit(lifeImg,(110,0)) 
     gameDisplay.blit(lifeImg,(160,0)) 
    if lives == 1:#Displays 1 spanners 
     gameDisplay.blit(lifeImg,(110,0)) 
    if lives == 0:#Should end game when all lives are lost 
     game_over() 

#Displays score in top right of screen and takes in parameter score 
def display_score(score): 
    #Displays the score itself 
    TextSurf, TextRect = text_objects(str(score), smallText)#Sets text and font 
    TextRect.topleft = (display_width-120,16)#Places text in top left of screen 
    gameDisplay.blit(TextSurf, TextRect) 

    #Displays the text "score" infront of the score 
    TextSurf, TextRect = text_objects("Score:", smallText)#Sets text and font 
    TextRect.topleft = (display_width-255,16)#Places text in top left of screen 
    gameDisplay.blit(TextSurf, TextRect) 

#Function for button that takes parameters (message,x,y,height,width,active colour,inactive colour,text size,action)  
def button(msg,x,y,w,h,i,a,t,action=None): 
    mouse = pg.mouse.get_pos() #Gets (x,y) position of mouse 
    Text = pg.font.Font("Font.ttf",t) #Lets text size be input 
    click = pg.mouse.get_pressed() #Gets whenther mouse has been clicked (mouse1,middle mouse,mouse2) 

    if x+w > mouse[0] > x and y+50 > mouse[1] > y:#Checks to see if mouse is within boundaries of button 
     pg.draw.rect(gameDisplay, a,(x,y,w,h),5)#Changes color of box when being hovered over 
     if click[0] == 1 and action != None:#If mouse1 is clicked and there is an action to be performed it will be run 
      action()#action is parameter and the() is used to initiate function 
    else: 
     pg.draw.rect(gameDisplay, i,(x,y,w,h),5)#Sets default button 

    TextSurf, TextRect = text_objects(msg, Text)#Runs text_objects with inputted parameter of the message 
    TextRect.center = ((x+(w/2)),(y+(h/2)))#Places text inside the button 
    gameDisplay.blit(TextSurf, TextRect)#Displays text 


def Exit():#Used for button on intro screen to exit 
    pg.quit() 
    quit() 

#What happens when player runs out of lives 
def game_over(): 
    while True: 
     for event in pg.event.get():#lets window be closed 
      if event.type == pg.QUIT: 
       pg.quit() 
       quit() 
     gameDisplay.blit(inBackgroundImg, (0, 0)) 
     TextSurf, TextRect = text_objects(("Game"), largeText)#Places large text on screen 
     TextRect.center = ((display_width/2+10),(180))#location of text 
     gameDisplay.blit(TextSurf, TextRect) 
     TextSurf, TextRect = text_objects(("Over"), largeText) 
     TextRect.center = ((display_width/2+10),(280)) 
     gameDisplay.blit(TextSurf, TextRect) 
     pg.draw.rect(gameDisplay, yellow,(290,125,445,220),5)#Puts yellow box over game over 

     button("Retry",410,600,207,70,yellow,lightGrey,45,game_loop) 
     button("Quit",display_width-105,display_height-55,100,50,yellow,lightGrey,33,Exit) 

     pg.display.update()#updates screen 
     clock.tick(15) 

#Function for control screen 
def controls(): 
    while True: 
     for event in pg.event.get():#lets window be closed 
      if event.type == pg.QUIT: 
       pg.quit() 
       quit() 
     gameDisplay.blit(controlsImg, (0, 0))#Places controlsImg on screen 
     button("Back",5,display_height-55,100,50,black,grey,30,game_intro)#places back button in bottom left corner 

     pg.display.update()#updates screen 
     clock.tick(15)#sets clock rate 


#Function to gun the intro screen 
def game_intro(): 
    while True: 
     for event in pg.event.get():#lets window be closed 
      if event.type == pg.QUIT: 
       pg.quit() 
       quit() 
     gameDisplay.blit(inBackgroundImg, (0, 0))#Places background image 
     TextSurf, TextRect = text_objects(("Lost on planet X"), largeText)#Places large text on screen 
     TextRect.center = ((display_width/2),(200))#location of text 
     gameDisplay.blit(TextSurf, TextRect)#displays text 
     pg.draw.rect(gameDisplay, yellow,(65,130,900,150),5)#Puts yellow box over title 

     button("Play",412,550,200,50,yellow,lightGrey,36,game_loop) #Play button at middle bottom 
     button("Controls",412,450,200,50,yellow,lightGrey,32,controls) #Controls button in middle of screen 
     button("Quit",display_width-105,display_height-55,100,50,yellow,lightGrey,33,Exit) #Quit button in bottom right 


     pg.display.update()#updates screen 
     clock.tick(15)#sets clock rate 

#game loop 
def game_loop(): 
    left = False 
    offset = 0 
    global PLATFORM_LIST 
    global startX 
    global startY 
    global backgroundImg 
    Robot.RobotPos = (startX,startY) 
    score = 0 #Score 
    lives = 3 #Robot Lives 
    Robot_friction = -0.3 #Friction value 
    vec = pg.math.Vector2 #Setting vec as vector quantity 
    while True: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       pg.quit 
       quit() 
      #Starts acceleration when key is pressed 
      if event.type == pg.KEYDOWN: 
       if event.key == pg.K_LEFT: 
        Robot.acc.x = -Robot_acc 
        left = True 
       elif event.key == pg.K_RIGHT: 
        Robot.acc.x = Robot_acc     
       elif event.key == pg.K_UP: 
        jump() 
      elif Robot.acc.x < 0: 
       left = True 
      else: 
       left = False 

      print(left) 
      #Adds friction to accleration to slow robot down when key is not being pressed 
      if event.type == pg.KEYUP: 
       if event.key == pg.K_LEFT or event.key == pg.K_RIGHT: 
        Robot.acc.x = Robot.acc.x * Robot_friction 

     #Adjusts velocity of robot by adding the acceleration on each cycle 
     Robot.vel = Robot.vel+ Robot.acc 
     #gameDisplay.fill(sky) 
     gameDisplay.blit(backgroundImg,(0,0)) 
     #Changes Robot position according to its velocity,acceleration and the friction 
     Robot.RobotPos = Robot.RobotPos + Robot.vel + 0.5 * Robot.acc 
     #Loads robot onto screen 
     gameDisplay.blit(robotImg,(Robot.rect)) 
     #pg.draw.rect(gameDisplay, red, Robot.rect, 2 
     display_lives(lives) 
     display_score(score)#lives 

     #Sets bottom of robot to its position 
     Robot.rect.midbottom = Robot.RobotPos 

     #Collision detection 
     if Robot.vel.y > 0: 
      hits = pg.sprite.spritecollide(Robot , platforms, False) 
      if hits: 
       if Robot.RobotPos.y < hits[0].rect.bottom: 
        #Puts Robot on top of platform 
        Robot.RobotPos.y = hits[0].rect.top + 1 
        Robot.vel.y = 0 

     hit = pg.sprite.spritecollide(Robot , wall,False) 
     if hit: 
      if left == True: 
       Robot.vel.x = 0 
       Robot.acc.x = 0 
      else: 
       pass 

     #Scrolling 
     if offset > 0: 
      pass 
     elif Robot.rect.left < display_width/4: 
      Robot.RobotPos.x = Robot.RobotPos.x + abs(Robot.vel.x) 
      startX = startX + int(abs(Robot.vel.x)) 
      offset = offset + int(abs(Robot.vel.x)) 
      for plat in platforms: 
       plat.rect.x = plat.rect.x + int(abs(Robot.vel.x)) 

     if Robot.rect.right > (display_width-display_width/4): 
      Robot.RobotPos.x = Robot.RobotPos.x - abs(Robot.vel.x) 
      startX = startX - int(abs(Robot.vel.x)) 
      offset = offset - int(abs(Robot.vel.x)) 
      for plat in platforms: 
       plat.rect.x = plat.rect.x - int(abs(Robot.vel.x)) 

     draw() 

     #Losing a life 
     if Robot.rect.top > display_height: 
      lives = lives - 1 
      Robot.RobotPos.y = Robot.RobotPos.y - (40+Robot_height) 
      Robot.RobotPos.x = Robot.RobotPos.x - 200 
      Robot.vel.x = 0 


     #Sets top velocity of robot  
     if Robot.vel.x > 6: 
      Robot.vel.x = 6 
     if Robot.vel.x < -6: 
      Robot.vel.x = -6 
     #Makes robot velocity = 0 when it is close to 0 
     if Robot.vel.x < 0.05 and Robot.vel.x > -0.05: 
      Robot.acc.x = 0 
      Robot.vel.x = 0 

     pg.display.update()#Updates display 
     clock.tick(60) 

game_intro() 
pg.quit() 
quit()