Kako programirati igru u Pythonu s Pygameom (sa slikama)

Sadržaj:

Kako programirati igru u Pythonu s Pygameom (sa slikama)
Kako programirati igru u Pythonu s Pygameom (sa slikama)

Video: Kako programirati igru u Pythonu s Pygameom (sa slikama)

Video: Kako programirati igru u Pythonu s Pygameom (sa slikama)
Video: Windows 10 - Upravljanje folderima i datotekama 2024, Maj
Anonim

Ovo je uvod u Pygame za ljude koji već znaju Python. Ovaj članak će vas naučiti koracima za izgradnju jednostavne igre u kojoj igrač izbjegava odbijanje lopti.

Koraci

1. dio od 8: Instaliranje Pygame -a

Korak 1. Preuzmite Pygame

Pronađite je za svoju platformu na

Korak 2. Pokrenite instalacijski program

Korak 3. Provjerite radi li instalacija

Otvorite Python terminal. Upišite "import pygame." Ako ne vidite greške, Pygame je uspješno instaliran.

    import pygame

Dio 2 od 8: Postavljanje osnovnog prozora

Korak 1. Otvorite novu datoteku

Korak 2. Uvezite Pygame

Pygame je biblioteka koja omogućava pristup grafičkim funkcijama. Ako želite više informacija o tome kako ove funkcije funkcioniraju, možete ih potražiti na web stranici Pygame.

    uvoz pygame iz pygame.locals import *

Korak 3. Podesite rezoluciju prozora

Napravit ćete globalnu varijablu za rezoluciju ekrana tako da se može referencirati u nekoliko dijelova igre. Također je lako pronaći na vrhu datoteke pa se kasnije može promijeniti. Za napredne projekte, stavljanje ovih podataka u zasebnu datoteku bila bi bolja ideja.

    rezolucija = (400, 300)

Korak 4. Definirajte neke boje

Boje u pigame -u su (RBGA koje se kreću u vrijednostima između 0 i 255. Alfa vrijednost (A) nije obavezna, ali ostale boje (crvena, plava i zelena su obavezne).

    bijela = (255, 255, 255) crna = (0, 0, 0) crvena = (255, 0, 0)

Korak 5. Pokrenite ekran

Koristite varijablu rezolucije koja je definirana ranije.

    screen = pygame.display.set_mode (rezolucija)

Korak 6. Napravite petlju igre

Ponovite određene radnje u svakom okviru naše igre. Napravite petlju koja će se uvijek ponavljati kroz sve ove radnje.

    dok je True:

Korak 7. Obojite ekran

    screen.fill (bijelo)

Korak 8. Prikažite ekran

Ako pokrenete program, ekran će postati bijel, a zatim će se program srušiti. To je zato što operativni sistem šalje događaje u igru i igra ne radi ništa s njima. Kad igra primi previše neobrađenih događaja, srušit će se.

    dok je True:… pygame.display.flip ()

Korak 9. Upravljajte događajima

Dobijte listu svih događaja koji su se dogodili u svakom okviru. Bit će vam stalo samo do jednog događaja, događaja prestanka. To se događa kada korisnik zatvori prozor igre. Ovo će također spriječiti rušenje našeg programa zbog previše događaja.

    dok je True:… za događaj u pygame.event.get (): if event.type == QUIT: pygame.quit ()

ProgramPygamePart1
ProgramPygamePart1

Korak 10. Isprobajte

Evo kako bi kod sada trebao izgledati:

    uvoz pygame iz pygame.locals import * rezolucija = (400, 300) bijela = (255, 255, 255) crna = (0, 0, 0) crvena = (255, 0, 0) ekran = pygame.display.set_mode (rezolucija) dok je True: screen.fill (bijelo) pygame.display.flip () za događaj u pygame.event.get (): if event.type == QUIT: pygame.quit ()

3. dio od 8: Izrada objekta igre

Korak 1. Napravite novu klasu i konstruktor

Postavite sva svojstva objekta. Također pružate zadane vrijednosti za sva svojstva.

    klasa Ball: def _init _ (self, xPos = rezolucija [0] / 2, yPos = rezolucija [1] / 2, xVel = 1, yVel = 1, rad = 15): self.x = xPos self.y = yPos self.dx = xVel self.dy = yVel self.radius = rad self.type = "ball"

Korak 2. Definirajte kako nacrtati objekt

Koristite svojstva koja su definirana u konstruktoru za crtanje loptice kao kruga, kao i za dodavanje površine u funkciju za crtanje objekta. Površina će biti objekt na ekranu koji je kreiran korištenjem rezolucije ranije.

    def draw (self, surface): pygame.draw.circle (površina, crno, (self.x, self.y), self.radius)

Korak 3. Napravite instancu klase, kao i recite petlji igre da izvuče loptu u svakoj petlji

    ball = Ball () dok je True:… ball.draw (ekran)

Korak 4. Učinite da se objekt pomjeri

Kreirajte funkciju koja će ažurirati položaj objekta. Pozovite ovu funkciju u svakoj petlji igre.

    klasa Ball:… def update (self): self.x += self.dx self.y += self.dy

Korak 5. Ograničite broj sličica u sekundi

Lopta će se kretati jako brzo jer se petlja igre izvodi stotinama puta u sekundi. Pomoću sata Pygame ograničite brzinu snimanja na 60 fps.

    clock = pygame.time. Clock () dok je True:… clock.tick (60)

Korak 6. Držite loptu na ekranu

Dodajte čekove u funkciju ažuriranja da biste promijenili smjer loptice ako pogodi jedan od rubova zaslona.

    klasa Ball:… def update (self):… if (self.x <= 0 or self.x> = resolution [0]): self.dx *= -1 if (self.y <= 0 ili self.y > = rezolucija [1]): self.dy *= -1

ProgramPygamePart2
ProgramPygamePart2

Korak 7. Isprobajte

Evo kako bi kod sada trebao izgledati:

    uvoz pygame iz pygame.locals import * rezolucija = (400, 300) bijela = (255, 255, 255) crna = (0, 0, 0) crvena = (255, 0, 0) ekran = pygame.display.set_mode (rezolucija) klasa Lopta: def _init _ (self, xPos = rezolucija [0] / 2, yPos = rezolucija [1] / 2, xVel = 1, yVel = 1, rad = 15): self.x = xPos self.y = yPos self.dx = xVel self.dy = yVel self.radius = rad self.type = "ball" def draw draw (self, surface): pygame.draw.circle (površina, crno, (self.x, self.y), self.radius) def update (self): self.x += self.dx self.y += self.dy if (self.x <= 0 ili self.x> = rezolucija [0]): self.dx *= -1 if (self.y <= 0 ili self.y> = rezolucija [1]): self.dy *= -1 ball = Ball () sat = pygame.time. Clock () while True: ekran. fill (white) ball.draw (screen) ball.update () pygame.display.flip () clock.tick (60) za događaj u pygame.event.get (): if event.type == QUIT: pygame.quit ()

4. dio 8: Organiziranje igre

Korak 1. Koristite razrede da sve organizirate

Igra će se zakomplicirati. Koristite objektno orijentirane tehnike za organiziranje koda.

Korak 2. Pretvorite petlju igre u klasu

Budući da naša igra sada ima podatke uključujući vaše objekte i funkcije igre, ima smisla pretvoriti vašu petlju u klasu.

    razredna igra ():

Korak 3. Dodajte konstruktor

Ovdje ćete instalirati neke objekte igre, kreirati naš ekran i sat te pokrenuti Pygame. Pygame mora biti inicijaliziran za korištenje određenih funkcija poput teksta ili zvuka.

    class game (): def _init _ (self): pygame.init () self.screen = pygame.display.set_mode (rezolucija) self.clock = pygame.time. Clock ()

Korak 4. Rukovanje događajima u funkciji

    class game ():… def handleEvents (self): za događaj u pygame.event.get (): if event.type == QUIT: pygame.quit ()

Korak 5. Neka petlja igre postane funkcija

Pozovite funkciju upravljanja događajima svaku petlju.

    class game ():… def run (self): while True: self.handleEvents () self.screen.fill (bijelo) self.clock.tick (60) pygame.display.flip ()

Korak 6. Rukujte s više objekata igre

Trenutno ovaj kod mora pozivati crtanje i ažuriranje svakog objekta na svakom okviru. Ovo bi postalo neuredno da imate puno predmeta. Dodajmo naš objekt nizu, a zatim ažuriramo i crtamo sve objekte u nizu svaku petlju. Sada možete jednostavno dodati još jedan objekt i dati mu drugačiju početnu poziciju.

    class game (): def _init _ (self):… self.gameObjects = self.gameObjects.append (Ball ()) self.gameObjects.append (Ball (100))… def run (self): while True: self.handleEvents () za gameObj u self.gameObjects: gameObj.update () self.screen.fill (bijelo) za gameObj u self.gameObjects: gameObj.draw (self.screen) self.clock.tick (60) pygame.display.flip ()

ProgramPygamePart3
ProgramPygamePart3

Korak 7. Isprobajte

Evo kako bi kod sada trebao izgledati:

    uvoz pygame iz pygame.locals import * rezolucija = (400, 300) bijela = (255, 255, 255) crna = (0, 0, 0) crvena = (255, 0, 0) ekran = pygame.display.set_mode (rezolucija) klasa Lopta: def _init _ (self, xPos = rezolucija [0] / 2, yPos = rezolucija [1] / 2, xVel = 1, yVel = 1, rad = 15): self.x = xPos self.y = yPos self.dx = xVel self.dy = yVel self.radius = rad self.type = "ball" def draw draw (self, surface): pygame.draw.circle (površina, crno, (self.x, self.y), self.radius) def update (self): self.x += self.dx self.y += self.dy if (self.x <= 0 ili self.x> = rezolucija [0]): self.dx *= -1 if (self.y <= 0 or self.y> = resolution [1]): self.dy *= -1 class game (): def _init _ (self): pygame.init () self.screen = pygame.display.set_mode (rezolucija) self.clock = pygame.time. Clock () self.gameObjects = self.gameObjects.append (Ball ()) self.gameObjects.append (Ball (100)) def handleEvents (self): za događaj u pygame.event.get (): if event.type == QUIT: pygame.quit () def run (self): while True: self.handleEvent s () za gameObj u self.gameObjects: gameObj.update () self.screen.fill (bijelo) za gameObj u self.gameObjects: gameObj.draw (self.screen) self.clock.tick (60) pygame.display. flip () game (). run ()

5. dio od 8: Dodavanje objekta igrača

Korak 1. Napravite klasu igrača i konstruktor

Napravit ćete drugi krug kojim upravlja miš. Inicijalizirajte vrijednosti u konstruktoru. Radijus je jedina važna vrijednost.

    klasa Igrač: def _init _ (self, rad = 20): self.x = 0 self.y = 0 self.radius = rad

Korak 2. Definirajte kako nacrtati objekt igrača

Bit će isto kao što ste nacrtali ostale objekte igre.

    klasa Player:… def draw (self, surface): pygame.draw.circle (surface, red, (self.x, self.y), self.radius)

Korak 3. Dodajte kontrolu miša za objekt playera

U svakom okviru provjerite lokaciju miša i postavite lokaciju objekata igrača na tu točku.

    klasa Player:… def update (self): cord = pygame.mouse.get_pos () self.x = cord [0] self.y = cord [1]

Korak 4. Dodajte objekt igrača u gameObjects

Kreirajte novu instancu igrača i dodajte je na listu.

    class game (): def _init _ (self):… self.gameObjects.append (Player ())

ProgramPygamePart4
ProgramPygamePart4

Korak 5. Isprobajte

Evo kako bi kod sada trebao izgledati:

    uvoz pygame iz pygame.locals import * rezolucija = (400, 300) bijela = (255, 255, 255) crna = (0, 0, 0) crvena = (255, 0, 0) ekran = pygame.display.set_mode (rezolucija) klasa Lopta: def _init _ (self, xPos = rezolucija [0] / 2, yPos = rezolucija [1] / 2, xVel = 1, yVel = 1, rad = 15): self.x = xPos self.y = yPos self.dx = xVel self.dy = yVel self.radius = rad self.type = "ball" def draw draw (self, surface): pygame.draw.circle (površina, crno, (self.x, self.y), self.radius) def update (self): self.x += self.dx self.y += self.dy if (self.x <= 0 ili self.x> = rezolucija [0]): self.dx *= -1 if (self.y <= 0 ili self.y> = rezolucija [1]): self.dy *= -1 klasa Igrač: def _init _ (self, rad = 20): self.x = 0 self.y = 0 self.radius = rad self.type = "player" def draw draw (self, surface): pygame.draw.circle (surface, red, (self.x, self.y), self.radius) def update (self): cord = pygame.mouse.get_pos () self.x = cord [0] self.y = cord [1] class game (): def _init _ (self): pygame.init () self.screen = pygame.display.set_ način (rezolucija) self.clock = pygame.time. Clock () self.gameObjects = self.gameObjects.append (Player ()) self.gameObjects.append (Ball ()) self.gameObjects.append (Ball (100)) def handleEvents (self): za događaj u pygame.event.get (): if event.type == QUIT: pygame.quit () def run (self): while True: self.handleEvents () za gameObj u sebi.gameObjects: gameObj.update () self.screen.fill (bijelo) za gameObj u self.gameObjects: gameObj.draw (self.screen) self.clock.tick (60) pygame.display.flip () game (). run ()

Dio 6 od 8: Stvaranje objekata u interakciji s playerom

Korak 1. Promijenite funkcije ažuriranja

Da bi objekti stupili u interakciju, oni moraju imati pristup jedni drugima. Dodajmo još jedan parametar za ažuriranje da bi prošao na popisu gameObjects. Morat ćete ga dodati objektu igrača i objektima Ball. Ako imate mnogo objekata za igre, nasljeđivanje bi vam moglo pomoći da zadržite sve potpise metoda istim.

    class Ball:… def update (self, gameObjects):… class Player:… def update (self, gameObjects):

Korak 2. Provjerite sudare između igrača i loptica

Prođite kroz sve objekte igre i provjerite je li tip predmeta lopta. Zatim upotrijebite radijuse dva objekta i formulu udaljenosti da provjerite da li se sudaraju. Krugove je zaista lako provjeriti na sudarima. Ovo je najveći razlog što niste koristili neki drugi oblik za ovu igru.

    klasa Player:… def update (self, gameObjects):… za gameObj u gameObjects: if gameObj.type == "ball": if (gameObj.x - self.x) ** 2 + (gameObj.y - self.y) ** 2 <= (gameObj.radius + self.radius) ** 2:

ProgramPygamePart5
ProgramPygamePart5

Korak 3. Završite igru ako igrač dobije "udarac"

Za sada napustimo igru.

    if (gameObj.x - self.x) ** 2 + (gameObj.y - self.y) ** 2 <= (gameObj.radius + self.radius) ** 2: pygame.quit ()

Korak 4. Isprobajte

Evo kako bi kôd trebao izgledati sada:

    uvoz pygame iz pygame.locals import * rezolucija = (400, 300) bijela = (255, 255, 255) crna = (0, 0, 0) crvena = (255, 0, 0) ekran = pygame.display.set_mode (rezolucija) klasa Lopta: def _init _ (self, xPos = rezolucija [0] / 2, yPos = rezolucija [1] / 2, xVel = 1, yVel = 1, rad = 15): self.x = xPos self.y = yPos self.dx = xVel self.dy = yVel self.radius = rad self.type = "ball" def draw draw (self, surface): pygame.draw.circle (površina, crno, (self.x, self.y), self.radius) def ažuriranje (self, gameObjects): self.x += self.dx self.y += self.dy if (self.x <= 0 ili self.x> = rezolucija [0]): self.dx *= -1 if (self.y <= 0 ili self.y> = rezolucija [1]): self.dy *= -1 klasa Igrač: def _init _ (self, rad = 20): self.x = 0 self.y = 0 self.radius = rad self.type = "player" def draw draw (self, surface): pygame.draw.circle (površina, crveno, (self.x, self.y), self.radius) def update (self, gameObjects): cord = pygame.mouse.get_pos () self.x = cord [0] self.y = cord [1] za gameObj u gameObjects: if gameObj.type == "ball": if (gameObj.x - self.x) ** 2 + (gameObj.y - self.y) ** 2 <= (gameObj.radius + self.radius) ** 2: pygame.quit () klasa game (): def _init _ (self): pygame.init () self.screen = pygame.display.set_mode (rezolucija) self.clock = pygame.time. Clock () self.gameObjects = self.gameObjects.append (Player ()) self.gameObjects.append (Ball ()) self.gameObjects.append (Ball (100)) def handleEvents (self): za događaj u pygame.event.get (): if event.type == QUIT: pygame.quit () def run (self): while True: self.handleEvents () za gameObj u self.gameObjects: gameObj.update (self.gameObjects) self.screen.fill (bijelo) za gameObj u self.gameObjects: gameObj.draw (self.screen) self.clock.tick (60) pygame.display.flip () game (). run ()

Dio 7 od 8: Dodavanje kontrolera za igru za stvaranje objekata

Korak 1. Kreirajte klasu kontrolera igre

Kontrolori igara su odgovorni za "pokretanje" igre. Razlikuje se od naše klase igara koja je odgovorna za crtanje i ažuriranje svih naših objekata. Kontroler će povremeno dodavati još jednu loptu na ekran kako bi otežao igru. Dodajte konstruktor i inicijalizirajte neke osnovne vrijednosti. Interval će biti vrijeme prije nego što se doda druga lopta.

    klasa GameController: def _init _ (self, interval = 5): self.inter = interval self.next = pygame.time.get_ticks () + (2 * 1000) self.type = "kontroler igre"

Korak 2. Dodajte funkciju ažuriranja

Ovo će provjeriti koliko je vremena prošlo od trenutka dodavanja lopte ili od početka igre. Ako je vrijeme više od intervala, vratit ćete vrijeme i dodati loptu.

    klasa GameController:… def update (self, gameObjects): if self.next <pygame.time.get_ticks (): self.next = pygame.time.get_ticks () + (self.inter * 1000) gameObjects.append (Ball ())

Korak 3. Dajte kuglicama nasumične brzine

Morat ćete koristiti nasumične brojeve kako biste igru svaki put učinili drugačijom. Međutim, brzine kuglica sada su broj s pomičnim zarezom umjesto cijeli broj.

    klasa GameController:… def update (self, gameObjects): if self.next <pygame.time.get_ticks (): self.next = pygame.time.get_ticks () + (self.inter * 1000) gameObjects.append (Ball (xVel = random ()*2, yVel = random ()*2))

Korak 4. Popravite funkciju izvlačenja

Funkcija izvlačenja neće prihvatiti plovke. Pretvorimo položaj lopte u cijele brojeve prije nego što se kuglice izvuku.

    klasa Ball:… def draw (self, surface): pygame.draw.circle (surface, black, (int (self.x), int (self.y)), self.radius)

Korak 5. Definirajte način izvlačenja za kontroler igre

Budući da se radi o igračkom objektu, glavna petlja će ga pokušati nacrtati. Morat ćete definirati funkciju izvlačenja koja ne radi ništa kako se igra ne bi srušila.

    klasa GameController:… def draw (self, screen): pass

Korak 6. Dodajte kontroler igre u gameObjects i uklonite 2 loptice

Igra bi sada trebala izroditi loptu svakih pet sekundi.

    klasa game (): def _init _ (self):… self.gameObjects = self.gameObjects.append (GameController ()) self.gameObjects.append (Player ())

ProgramPygamePart6
ProgramPygamePart6

Korak 7. Isprobajte

Evo kako bi kod sada trebao izgledati:

    uvozi pygame iz slučajnog uvoza nasumično iz pygame.locals import * rezolucija = (400, 300) bijela = (255, 255, 255) crna = (0, 0, 0) crvena = (255, 0, 0) ekran = pygame. display.set_mode (rezolucija) klasa Ball: def _init _ (self, xPos = resolution [0] / 2, yPos = resolution [1] / 2, xVel = 1, yVel = 1, rad = 15): self.x = xPos self.y = yPos self.dx = xVel self.dy = yVel self.radius = rad self.type = "ball" def draw draw (self, surface): pygame.draw.circle (površina, crno, (int (self) x), int (self.y)), self.radius) def update (self, gameObjects): self.x += self.dx self.y += self.dy if (self.x <= 0 ili self. x> = rezolucija [0]): self.dx *= -1 if (self.y <= 0 ili self.y> = rezolucija [1]): self.dy *= -1 klasa Igrač: def _init _ (self, rad = 20): self.x = 0 self.y = 0 self.radius = rad self.type = "player" def draw draw (self, surface): pygame.draw.circle (površina, crveno, (self.x, self.y), self.radius) def update (self, gameObjects): cord = pygame.mouse.get_pos () self.x = cord [0] self.y = cord [1] za gameObj u igri Objekti: if gameObj.type == "ball": if (gameObj.x - self.x) ** 2 + (gameObj.y - self.y) ** 2 <= (gameObj.radius + self.radius)* * 2: pygame.quit () klasa GameController: def _init _ (self, interval = 5): self.inter = interval self.next = pygame.time.get_ticks () + (2 * 1000) self.type = "kontroler igre "def update (self, gameObjects): if self.next <pygame.time.get_ticks (): self.next = pygame.time.get_ticks () + (self.inter * 1000) gameObjects.append (Ball (xVel = random ()*2, yVel = random ()*2)) def draw (self, screen): pass class game (): def _init _ (self): pygame.init () self.screen = pygame.display.set_mode (rezolucija) self.clock = pygame.time. Clock () self.gameObjects = self.gameObjects.append (GameController ()) self.gameObjects.append (Player ()) def handleEvents (self): za događaj u pygame.event.get (): if event.type == QUIT: pygame.quit () def run (self): while True: self.handleEvents () za gameObj u self.gameObjects: gameObj.update (self.gameObjects) self.screen.fill (bijelo) za gameObj u self.gameO bjects: gameObj.draw (self.screen) self.clock.tick (60) pygame.display.flip () game (). run ()

8. dio od 8: Dodavanje rezultata i završetak igre

Korak 1. Dodajte rezultat klasi kontrolera igre

Kreirajte objekt fonta i varijablu rezultata. Nacrtaćete font u svakom okviru da biste prikazali rezultat i povećali rezultat u svakom okviru u ažuriranju.

    klasa GameController: def _init _ (self, interval = 5):… self.score = 0 self.scoreText = pygame.font. Font (Nijedan, 12) def update (self, gameObjects):… self.score += 1 def draw (self, screen): screen.blit (self.scoreText.render (str (self.score), True, black), (5, 5))

Korak 2. Izmijenite način na koji se igra završava

Riješimo se prekida kad igrač otkrije sudar. Umjesto toga ćete postaviti varijablu u playeru koju igra može provjeriti. Kada je gameOver postavljen, zaustavite ažuriranje objekata. Ovo će zamrznuti sve na mjestu kako bi igrač mogao vidjeti što se dogodilo i provjeriti njihov rezultat. Imajte na umu da se objekti još uvijek crtaju, samo da se ne ažuriraju.

    klasa Player: def _init _ (self, rad = 20):… self.gameOver = Lažno ažuriranje def (self, gameObjects):… za gameObj u gameObjects: if gameObj.type == "ball": if (gameObj.x - self.x) ** 2 + (gameObj.y - self.y) ** 2 <= (gameObj.radius + self.radius) ** 2: self.gameOver = True class game (): def _init _ (self): … Self.gameOver = False def run (self): while True: self.handleEvents () ako nije self.gameOver: za gameObj u self.gameObjects: gameObj.update (self.gameObjects) if gameObj.type == "player": self.gameOver = gameObj.gameOver

ProgramPygameFinal
ProgramPygameFinal

Korak 3. Isprobajte

Evo kako bi gotov kod trebao izgledati sada:

    uvezi pygame iz slučajnog uvoza nasumično iz pygame.locals import * rezolucija = (400, 300) bijela = (255, 255, 255) crna = (0, 0, 0) crvena = (255, 0, 0) ekran = pygame. display.set_mode (rezolucija) klasa Ball: def _init _ (self, xPos = resolution [0] / 2, yPos = resolution [1] / 2, xVel = 1, yVel = 1, rad = 15): self.x = xPos self.y = yPos self.dx = xVel self.dy = yVel self.radius = rad self.type = "ball" def draw draw (self, surface): pygame.draw.circle (površina, crno, (int (self) x), int (self.y)), self.radius) def update (self, gameObjects): self.x += self.dx self.y += self.dy if (self.x <= 0 ili self. x> = rezolucija [0]): self.dx *= -1 if (self.y <= 0 ili self.y> = rezolucija [1]): self.dy *= -1 klasa Igrač: def _init _ (self, rad = 20): self.x = 0 self.y = 0 self.radius = rad self.type = "player" self.gameOver = False def draw (self, surface): pygame.draw.circle (površina, crveno, (self.x, self.y), self.radius) def ažuriranje (self, gameObjects): cord = pygame.mouse.get_pos () self.x = cord [0] self.y = cord [1] za gameObj u gameObjects: if gameObj.type == "ball": if (gameObj.x - self.x) ** 2 + (gameObj.y - self.y) ** 2 <= (gameObj.radius + self.radius) ** 2: self.gameOver = Istinska klasa GameController: def _init _ (self, interval = 5): self.inter = interval self.next = pygame.time.get_ticks () + (2*1000) self. type = "kontroler igre" self.score = 0 self.scoreText = pygame.font. Font (Ništa, 12) def update (self, gameObjects): if self.next <pygame.time.get_ticks (): self.next = pygame.time.get_ticks () + (self.inter*1000) gameObjects.append (Lopta (xVel = random ()*2, yVel = random ()*2)) self.score + = 1 def draw (self, screen): screen.blit (self.scoreText.render (str (self.score), True, black), (5, 5)) class game (): def _init _ (self): pygame.init () self.screen = pygame.display.set_mode (rezolucija) self.clock = pygame.time. Clock () self.gameObjects = self.gameObjects.append (GameController ()) self.gameObjects.append (Player ()) self.gameOver = False def handleEvents (self): za događaj u pygame.event.get (): ako je ev ent.type == QUIT: pygame.quit () def run (self): while True: self.handleEvents () ako nije self.gameOver: za gameObj u self.gameObjects: gameObj.update (self.gameObjects) if gameObj. upišite == "player": self.gameOver = gameObj.gameOver self.screen.fill (bijelo) za gameObj u self.gameObjects: gameObj.draw (self.screen) self.clock.tick (60) pygame.display.flip () game (). run ()

Preporučuje se: