Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --create a player class
- Player = Object:extend()
- function Player:new()
- self.image = love.graphics.newImage("panda.png")
- -- enable player to move with arrow keys
- self.x = 300
- self.y = 20
- self.speed = 500
- self.width = self.image:getWidth()
- end
- function Player:update(dt)
- if love.keyboard.isDown("left") then
- self.x = self.x - self.speed*dt
- elseif love.keyboard.isDown("right") then
- self.x = self.x + self.speed*dt
- end
- -- the player to can move out of the window. use if statements to fix this
- local window_width = love.graphics.getWidth()
- --if x is too far to the left
- if self.x < 0 then
- self.x = 0
- -- or too far to the right
- elseif self.x + self.width > window_width then
- --set the right side to the window's width
- self.x = window_width - self.width
- -- the player can no longer
- end
- end
- function Player:draw()
- love.graphics.draw(self.image, self.x, self.y)
- end
- function Player:keyPressed(key)
- --if the spacebar is pressed
- if key == "space" then
- -- put a new instance of a bullet inside list of bullets
- --table.insert(listOfBullets, Bullet(self.x, self.y))
- table.insert(listOfBullets, Bullet(self.x, self.y))
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement