Cov txheej txheem:

Tsim Koj Thawj Qhov Kev Sib Tw Ua Si: 10 Kauj Ruam
Tsim Koj Thawj Qhov Kev Sib Tw Ua Si: 10 Kauj Ruam

Video: Tsim Koj Thawj Qhov Kev Sib Tw Ua Si: 10 Kauj Ruam

Video: Tsim Koj Thawj Qhov Kev Sib Tw Ua Si: 10 Kauj Ruam
Video: LOKY -Txhob Ua Yog Toog (Official MV )Hmong New Song 2024, Kaum ib hlis
Anonim
Image
Image
Duab
Duab

Yog tias koj tau ua qee qhov Python coding thiab xav sau game koj yuav muaj hla Pygame Zero.

Hauv qhov kev qhia no peb yuav sau qhov kev sib tw yooj yim.

Kauj Ruam 1: Cov Txheej Txheem Dav Dav

Pygame module ntxiv ntau txoj haujlwm uas pab koj sau kev ua si hauv Python.

Pygame Zero mus rau ib kauj ruam ntxiv kom koj hla hla txoj kev nyuaj ntawm kev ua txhua qhov kev ua si loops thiab teeb tsa koj cov qauv txheej txheem.

Pygame Zero yog qhov kev xaiv zoo rau txhua tus uas xav pib sau computer game ntawm Raspberry Pi lossis ib lub tshuab Linux.

Koj Yuav Tsum Tau:

  • Tshuab uas khiav Linux OS
  • Editor los sau cov program nab
  • Keyboard
  • Ib txhia xav

Cov ntaub ntawv:

github.com/AhmNouira/-Racing-Game

Kauj ruam 2: Yam yuavtsum tau kawm uantej

Ua ntej kuv yuav siv kuv lub laptop nrog Ubuntu 18.04 OS. Tom qab ntawd peb yuav ua qhov kev ua si ntawm Raspberry Pi hauv qib 8.

Ua ntej txuas ntxiv nrog cov lus qhia no, xyuas kom tseeb tias koj tau nkag rau hauv tus neeg siv nrog sudo txoj cai.

Txhim kho pip rau Python 3

Pib los ntawm kev hloov kho cov npe pob uas siv cov lus txib hauv qab no:

sudo apt hloov tshiab

Siv cov lus txib hauv qab no los teeb tsa pip rau Python 3:

sudo apt nruab python3-pip

txheeb xyuas qhov teeb tsa los ntawm kev txheeb xyuas qhov pip version:

pip3 -rov qab

Txhim kho Pygame Zero

pip3 nruab pgzero --user

Qhov no tseem yuav nruab Pygame. Npaj ua ntej pob Pygame

Yog li thawj kauj ruam hauv koj txoj kev yuav yog qhib Python 3 IDLE lossis tus kws kho mob uas koj nyiam tshaj plaws.

Kauj Ruam 3: "Nyob Zoo Ntiaj Teb" hauv Pygame Zero

Los ntawm lub neej ntawd, Pygame Zero qhov rai qhib ntawm qhov loj ntawm 800 pixels dav los ntawm 600 pixels siab. Koj tuaj yeem hloov kho

qhov loj ntawm koj lub qhov rais, muaj ob qhov piv txwv tseem ceeb uas koj tuaj yeem teeb tsa, yog tias koj suav nrog WIDTH = 700 HIGH = 800.

Pygame xoom muab lub luag haujlwm ua ntej los tswj cov game game ib txwm ua:

Qhov kos () Muaj nuj nqi

Peb tuaj yeem sau txoj haujlwm no rau hauv peb qhov haujlwm ib yam li peb ib txwm txhais lub luag haujlwm hauv Python.

Pygame Zero ua raws tib txoj cai kev teeb tsa zoo li Python, yog li koj yuav tsum tau saib xyuas kom nkag mus rau koj tus lej kom raug.

#!/usr/bin/python3 # teeb tus neeg txhais lus

ntshuam pgzrun # ntshuam pgzero module WIDTH = 700 # dav ntawm lub qhov rais HEIGHT = 800 # qhov siab ntawm lub qhov rais def kos (): # pygame xoom kos ua haujlwm screen.fill ((128, 128, 128)) # sau lub vijtsam nrog RGB xim screen.draw.text ("Nyob Zoo Ntiaj Teb!", (270, 320), # kos "Nyob Zoo Ntiaj Teb!" xim = (255, 255, 255), fontsize = 40) pgzrun.go ()

Ua ntej tshaj plaws koj yuav tsum khaws koj cov ntaub ntawv program thiab muab nws lub npe.

Tom qab ntawd qhib lub Terminal qhov rai, mus rau koj cov ntaub ntawv qhov chaw thiab ntaus:

./.py ib

Kauj Ruam 4: Teeb Koj Tus Ua Yeeb Yam

Teeb Koj Tus Actor
Teeb Koj Tus Actor

Tam sim no uas peb muaj peb theem teeb tsa, peb tuaj yeem tsim peb Cov Kws Ua Haujlwm, lawv yog cov khoom muaj zog hauv Pygame Zero.

Peb tuaj yeem thauj tus Actor los ntawm kev ntaus sab saum toj ntawm txoj haujlwm:

tsheb = Actor ("racecar")

Hauv Pygame Zero peb cov duab yuav tsum tau muab khaws cia rau hauv cov npe hu ua cov duab, nyob ib sab ntawm peb cov program file. Yog li ntawd, peb tus Actor yuav tab tom nrhiav cov duab hauv cov duab nplaub tshev hu ua racecar.png. Nws tuaj yeem yog GIF lossis-j.webp

Koj tuaj yeem teeb tsa nws txoj haujlwm ntawm lub vijtsam los ntawm kev ntaus:

car.pos = 350, 560

Tom qab ntawd hauv peb qhov kos () ua haujlwm peb tuaj yeem ntaus

car.draw () # kos peb lub tsheb sib tw ntawm nws txoj haujlwm

Qhov kev zov me nyuam yuav zoo li no:

#!/usr/bin/python3

ntshuam pgzrun WIDTH = 700 # dav ntawm lub qhov rais HAUJ LWM = 800 # qhov siab ntawm lub qhov rooj tsheb = Actor ("racecar") car.pos = 350, 560 def kos (): # pygame xoom kos ua haujlwm screen.fill ((128, 128, 128)) # sau qhov screen nrog car.draw () pgzrun.go ()

Kuaj koj qhov program kom paub tseeb tias qhov no ua haujlwm.

Kauj Ruam 5: Tswj Tus Ua Yeeb Yam

Tswj tus Actor
Tswj tus Actor

Thaum peb muaj peb lub tsheb kos duab ntawm lub vijtsam, theem tom ntej yog kom cov neeg ua si txav nws.

Peb tuaj yeem ua qhov no nrog nias qhov tseem ceeb. Peb tuaj yeem nyeem lub xeev ntawm cov yuam sij no hauv lwm qhov kev ua haujlwm ua ntej hu ua hloov tshiab ().

Hloov tshiab () Muaj nuj nqi

Lub luag haujlwm no tau tshuaj xyuas tas li thaum qhov kev ua si tau khiav.

Peb yuav tsum sau qee qhov chaws txhawm rau txheeb xyuas qhov tseem ceeb nias ntawm cov xub thiab tseem yuav ua qee yam txog nws.

Yog li peb yuav ntxiv txoj haujlwm no rau peb qhov program

def hloov tshiab ():

yog keyboard.left: car.x -= 2 yog keyboard.right: car.x += 2 yog keyboard.up: car.y -= 2 yog keyboard.down: car.y += 2

Cov kab ntawv ntawm txoj cai no yuav txav lub tsheb Actor Sab laug, sab xis, rau pem hauv ntej thiab rov qab.

Kauj Ruam 6: Tsim Txoj Kev

Tsim Txoj Kev
Tsim Txoj Kev

Tam sim no peb muaj lub tsheb uas peb tuaj yeem txav tau, peb xav tau txoj hauv kev kom nws tsav tsheb.

Peb yuav tsim peb txoj hauv kev los ntawm Cov Kws Ua Haujlwm, ib kab zuj zus. Peb yuav tsum tau ua qee cov npe kom taug qab cov Actors peb tsim.

Peb tseem yuav xav teeb tsa ob peb yam ntxiv rau qhov khiav.

Tom qab ntawd cia peb ua txoj haujlwm tshiab hu ua makeTrack (), Lub luag haujlwm yuav ntxiv ib txoj hauv kev Actor nyob rau sab laug thiab ib sab xis, ob qho tib si siv cov duab liab qab-p.webp

trackLeft = # cov npe khaws cov laug barries

trackRight = # daim ntawv khaws cia txoj cai barries trackCount = 0 # suav tus naj npawb ntawm barries trackPosition = 350 trackWidth = 150 # dav ntawm sab laug thiab sab xis barries def makeTrack (): # muaj nuj nqi los ua ib barrie ntawm sab laug thiab sab xis trackCount ntiaj teb, trackLeft, trackRight, trackPosition, trackWidth trackLeft.append (Actor ("liab qab", pos = (trackPosition-trackWidth, 0))) trackRight.append (Actor ("liab qab", pos = (trackPosition + trackWidth, 0))) trackCount += 1

Qhov txuas ntxiv uas peb yuav tsum tau ua yog txav cov ntu ntawm txoj kab nqes los ntawm lub vijtsam mus rau lub tsheb.

Wb sau ib txoj haujlwm tshiab hu ua updateTrack (), Cov haujlwm no hloov tshiab qhov twg cov kab thaiv yuav tshwm.

trackDriction = Tsis tseeb

SPEED = 4 # teeb tsa qhov nrawm ntawm qhov kev ua si los ntawm Random import randint # ntshuam chav kawm randint los ntawm kev hloov pauv tsis raug defTrack (): thoob ntiaj teb trackCount, trackPosition, trackDirection, trackWidth, SPEED b = 0 thaum b 32: yog trackDirection == Tsis tseeb: trackPosition += 16 yog trackDirection == Tseeb: trackPosition - = 16 yog randint (0, 4) == 1: trackDirection = tsis trackDirection yog trackPosition> 700 - trackWidth: trackDirection = Tseeb yog trackPosition < trackWidth: trackDirection = Cuav makeTrack () # tsim txoj hauv kev tshiab nyob rau sab saum toj ntawm lub vijtsam

Thov xa mus rau zip cov ntaub ntawv hauv qab no npe "RaceGameDemo".

Yog tias peb khiav peb cov cai tam sim no, peb yuav tsum pom txoj hauv kev nqes mus rau ntawm lub tsheb. Qhov teeb meem tsuas yog tias peb tuaj yeem txav lub tsheb hla txoj kev teeb tsa thiab peb xav kom lub tsheb sab hauv lawv nrog qee qhov kev sib tsoo pom.

Kauj Ruam 7: Tsheb Crash

Tsheb Crash
Tsheb Crash
Tsheb Crash
Tsheb Crash
Tsheb Crash
Tsheb Crash

Peb yuav tsum tau ua kom ntseeg tau tias peb lub tsheb tsis kov cov neeg ua yeeb yam.

peb kuj tseem yuav sim ntsuas kev sib tsoo siv txoj hauv kev colliderect () hauv peb qhov updateTrack () muaj nuj nqi.

Hauv qhov kev ua si no peb yuav muaj peb lub xeev sib txawv rau qhov kev ua si khaws cia hauv peb qhov gameStatus sib txawv:

  • gameStatus == 0 # game is running
  • gameStatus == 1 # tsheb sib tsoo
  • gameStatus == 2 # game tas

Peb yuav tsum tau hloov pauv peb kos () ua haujlwm thiab peb hloov tshiab () ua haujlwm los teb rau gameStatus sib txawv.

Tiav tiav

Txhua yam peb yuav tsum tau ua tam sim no yog los tso saib qee yam yog gameStatus tau teeb tsa rau 1 lossis 2, piv txwv li peb yuav tsum tso saib liab

chij, yog lub tsheb poob. Peb tuaj yeem ua qhov ntawd nrog cov cai hauv qab no:

screen.blit ("redflag", (230, 230))

Txhawm rau pom tias lub tsheb tau mus txog qhov kawg, peb yuav tsum suav pes tsawg txoj kab ntu uas tau tsim thiab tom qab ntawd tej zaum thaum peb mus txog 200, teeb gameStatus rau 2. Tom qab ntawd tso tus chij checkered:

screen.blit ("finishflag", (230, 230))

Peb tseem yuav tso qee cov ntawv hauv qhov screen zoo li cov qhab nia tam sim no ntawm qhov kev ua si.

Saib ntawm daim ntawv teev npe tag nrho kom pom tias txhua qhov no haum ua ke.

Kauj Ruam 8: Khiav Qhov Kev Ua Si ntawm Raspberry Pi

Khiav Qhov Kev Ua Si ntawm Raspberry Pi
Khiav Qhov Kev Ua Si ntawm Raspberry Pi
Khiav Qhov Kev Ua Si ntawm Raspberry Pi
Khiav Qhov Kev Ua Si ntawm Raspberry Pi

Ntawm Raspberry Pi pgzero tau raug teeb tsa los ntawm lub neej ntawd txij li tso tawm Raspbian Jessie thaum lub Cuaj Hlis 2015.

Tsuas yog hloov kho koj li Raspberry Pi los ntawm kev siv cov lus txib:

sudo apt-tau hloov tshiab

Mus rau koj cov ntaub ntawv qhov chaw thiab ntaus hauv Terminal.

pog.py

Kauj Ruam 9: Koj Puas Yeej?

Koj Puas Yeej?
Koj Puas Yeej?

Koj tuaj yeem ua qhov kev ua si yooj yim dua lossis nyuab dua los ntawm kev hloov qhov trackWidth sib txawv los ua txoj kab dav sib txawv. Koj tuaj yeem hloov tus nqi ntawm SPEED kom ua rau txoj kev khiav nrawm dua lossis qeeb dua.

Kauj ruam 10: Xaus

Pygame Zero yog qhov kev xaiv zoo rau txhua tus neeg uas xav pib sau computer games.

Yog tias koj muaj lus nug ntawm chav kawm koj tuaj yeem tawm lus saib kom pom ntau ntxiv txog kuv cov haujlwm thov mus saib kuv tus channel:

kuv tus YouTube

kuv tus Twitter

myLinkedin

Ua tsaug rau kev nyeem cov lus qhia no ^^ thiab muaj hnub zoo. Saib koj. Ahmed Nouira: koj puas xav tau ntau tus thwjtim?

Pom zoo: