Notifications
Retirer tout
Remise à niveau : Création d'un jeu Pong ( joueur vs petite I.A.)
Partagez vos progrès
1
Posts
1
Utilisateurs
0
Likes
51
Vu
Début du sujet 14/11/2023 3:39
Bonjour, pour commencer dans ma formation de développeur de jeux vidéo, je viens de faire une remise à niveau en créant sur l'un des ateliers un jeu Pong.
Voici le résultat en langage Lua, avec le framework LÖve (love2d)
screenWidth = love.graphics.getWidth() screenHeight = love.graphics.getHeight() middleScreenW = screenWidth / 2 middleScreenH = screenHeight / 2 rect1 = { x =15, y =300, w =10, h =100, SpeedX = 7, SpeedY = 7 } rect2 = { x =770, y =300, w =10, h =100, SpeedX = 5, SpeedY = 5 } ball = { x =middleScreenW, y =middleScreenH, w =10, h =10, SpeedX = 7, SpeedY = 7 } middleBallx = ball.w/2 middleBally = ball.h/2 middleRecty = rect2.h/2 middleStartRect = middleScreenH - middleRecty function middleBallFunc() ball.x = middleScreenW - ball.w ball.y = middleScreenH - ball.h ball.SpeedX = 5 ball.SpeedY = 5 end --var score default scoreRect1 = 0 scoreRect2 = 0 --iaComTracker = 5 --list trails trailList ={} function love.load() --sound and music soundFxRect = love.audio.newSource("collide.mp3","static") soundFxArea = love.audio.newSource("buzzer.mp3","static") musicBack = love.audio.newSource("music.mp3","stream") middleBallFunc() -- start position rect1 rect1.x = 10 rect1.y = middleStartRect -- start position rect2 rect2.x = screenWidth - rect2.w -10 rect2.y = middleStartRect --modify size value varValueFontSize = 40 font = love.graphics.newFont(varValueFontSize) scallFontSize = (varValueFontSize / 2) end function love.update(dt) --music play musicBack:play() --rect1 control keyboard --up and limit area top if love.keyboard.isDown("up") then rect1.y = rect1.y - rect1.SpeedY if rect1.y < 0 then rect1.y = 0 end end --down and limit area bottom if love.keyboard.isDown("down") then rect1.y = rect1.y + rect1.SpeedY if rect1.y + rect1.h > screenHeight then rect1.y = screenHeight - rect1.h end end --ball coords start speed ball.x = ball.x + ball.SpeedX ball.y = ball.y + ball.SpeedY --interact area right if ball.x > screenWidth - ball.w then soundFxArea:play() ball.SpeedX = - ball.SpeedX middleBallFunc() --increment Win rect1 scoreRect1 = scoreRect1 + 1 end --interact area left if ball.x < 0 then soundFxArea:play() ball.SpeedX = - ball.SpeedX middleBallFunc() --increment Win rect2 scoreRect2 = scoreRect2 + 1 end --top interact and inverse direction if ball.y > screenHeight - ball.h then ball.SpeedY = - ball.SpeedY end --bottom interact and inverse direction if ball.y < 0 then ball.SpeedY = - ball.SpeedY end --interact rect 1 and ball if ball.x <= rect1.x + rect1.w then if ball.y + ball.h > rect1.y and ball.y + ball.h < rect1.y + rect1.h + rect1.w then --if ball.y + ball.h > rect1.y and ball.y < rect1.y + rect1.h then soundFxRect:play() ball.SpeedX = - ball.SpeedX --ball.SpeedY = - ball.SpeedY ball.x = rect1.x + rect1.w end end --interact rect 2 and ball if ball.x + ball.w >= rect2.x then if ball.y + ball.w > rect2.y and ball.y + ball.w + ball.h < rect2.y + rect2.h then --if ball.y + ball.h + ball.w > rect2.y and ball.y + ball.w < rect2.y + rect2.h then soundFxRect:play() ball.SpeedX = - ball.SpeedX --ball.SpeedY = - ball.SpeedY ball.x = rect2.x - ball.w end end --rect2 tracker ball --conditionnal instruction if ball.y + middleBally > rect2.y + middleRecty then rect2.y = rect2.y + rect2.SpeedY elseif ball.y + middleBally < rect2.y + middleRecty then rect2.y = rect2.y - rect2.SpeedY end if rect2.y <= 0 then rect2.y = 0 end if rect2.y >= screenHeight - rect2.h then rect2.y = screenHeight - rect2.h end --remove trail ball for n=#trailList,1,-1 do t = trailList[n] t.life = t.life - dt --particle collide if ball.x <= rect1.x + rect1.w or ball.x + ball.w >= rect2.x then t.x = t.x + t.vx t.y = t.y + t.vy end --remove last trail limit time if t.life <= 0 then table.remove(trailList,n) end end --my trail local myTrail = { x = ball.x, y = ball.y, vx = math.random(-1,5), vy = math.random(-1,5), r = math.random(), g = math.random(), b = math.random(), life = 0.5 } --2d list table.insert(trailList,myTrail) --print score score = scoreRect1.." : "..scoreRect2 lenScoreMiddle = string.len(score)/2 numValueScoreMiddle = tonumber(lenScoreMiddle) end function love.draw() --Draw rect1 and rect2 rect1Draw = love.graphics.rectangle("fill",rect1.x,rect1.y,rect1.w,rect1.h) rect2Draw = love.graphics.rectangle("fill",rect2.x,rect2.y,rect2.w,rect2.h) --draw trails and modify color for n=1,#trailList do local t = trailList[n] love.graphics.setColor(0,255,255,t.life/2) love.graphics.rectangle("fill",t.x,t.y,ball.w,ball.h) end --line middle line000 = love.graphics.rectangle("fill",middleScreenW - 1,55,2,screenHeight) --draw ball love.graphics.setColor(1,1,1,1) ballDraw = love.graphics.rectangle("fill",ball.x,ball.y,ball.w,ball.h) --print and modify value text love.graphics.setFont(font) love.graphics.print(score,(middleScreenW - 2.5 * scallFontSize + 5),10) end
Citation
Forum Jump:
Sujets associés
-
Mon premier jeu 💪
Il y a 7 mois
-
Mesh Arena (love2d + web build)
Il y a 8 mois
-
LostInSpace (Love jam 2023)
Il y a 9 mois
-
Advance wire (love jam 2023 thème : connexion)
Il y a 9 mois
-
PermaGame, découvrir la permaculture à travers de mini-jeux.
Il y a 10 mois
Recently viewed by users: Chisk Il y a 43 minutes.
Forum Information
- 6 Forums
- 213 Sujets
- 735 Posts
- 0 En ligne
- 45.2 {numéro}K Membres
Notre nouveau membre: ThomasP
Icônes du forum:
Le forum ne contient aucun message non lus
Le forum contient des messages non lus
Icônes de sujet:
Pas répondu
Repondu
Actif
Important
Épinglé
Non approuvé
Résolu
Privé
Fermé