Felhasználói eszközök

Eszközök a webhelyen


oktatas:programozas:python:pygame

< Python

PyGame

PyGame kezdés

kezdes.py
import pygame
import time
 
pygame.init()
 
display = pygame.display.set_mode((400, 250))
pygame.display.set_caption('Teszt')
 
time.sleep(5)
pygame.quit()
quit() 

Események

esemeny.py
import pygame
 
pygame.init()
 
display = pygame.display.set_mode((400, 250))
pygame.display.set_caption('Teszt')
 
gameEnd = False
 
while not gameEnd:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
			quit()

Kurzormozgatók

kurzorMozgatok.py
import pygame
 
pygame.init()
 
display = pygame.display.set_mode((400, 250))
pygame.display.set_caption('Teszt')
 
gameEnd = False
 
while not gameEnd:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
			quit()
		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_LEFT:
				print("Balra")
			if event.key == pygame.K_RIGHT:
				print("Jobb")
			if event.key == pygame.K_UP:
				print("Fel")
			if event.key == pygame.K_DOWN:
				print("Le")

Események megtekintése

esemenyMutat.py
import pygame
 
pygame.init()
 
display = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Teszt')
 
clock = pygame.time.Clock()
 
 
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()            
        print(event)
 
    pygame.display.update()
    clock.tick(1)

Osztály használata

osztaly.py
import pygame
 
class Jatek(object):
	def main(self, kepernyo):
		while True:
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and \
						event.key == pygame.K_ESCAPE:
					return
if __name__ == '__main__':
	pygame.init()
	kepernyo = pygame.display.set_mode((640, 480))
	Jatek().main(kepernyo)

Rajzolás a képernyőre

     0 ->  X        640
0   |------------------|
    |                  |
|   |                  |
v   |                  |
    |                  |
    |                  |
Y   |                  |
    |                  |
480 |------------------|

   

Kép beillesztése

kep.py
import pygame
 
class Jatek(object):
	def main(self, kepernyo):
		kep = pygame.image.load('kep.png')
 
		while True:
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and \
						event.key == pygame.K_ESCAPE:
					return
			kepernyo.fill((200, 200, 200))  # A képernyő színezése
			kepernyo.blit(kep, (100, 100))  # A kép elhelyezése
			pygame.display.flip()           # A puffer megjelenítése
if __name__ == '__main__':
	pygame.init()
	kepernyo = pygame.display.set_mode((640, 480))
	Jatek().main(kepernyo)

Kevesebb processzoridő beállítása

proc.py
import pygame
 
class Jatek(object):
	def main(self, kepernyo):
		clock = pygame.time.Clock()
 
		kep = pygame.image.load('kep.png')
 
		while True:
			clock.tick(30)
 
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and \
						event.key == pygame.K_ESCAPE:
					return
			kepernyo.fill((200, 200, 200))
			kepernyo.blit(kep, (100, 100))
			pygame.display.flip()
if __name__ == '__main__':
	pygame.init()
	kepernyo = pygame.display.set_mode((640, 480))
	Jatek().main(kepernyo)

Animáció

animacio.py
import pygame
 
class Jatek(object):
	def main(self, kepernyo):
		clock = pygame.time.Clock()
 
		kep = pygame.image.load('kep.png')
		kep_x = 250
		kep_y = 150
 
		while True:
			clock.tick(30)
 
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and \
						event.key == pygame.K_ESCAPE:
					return
			kep_x += 10
			kepernyo.fill((200, 200, 200))
			# ~ kepernyo.blit(kep, (100, 100))
			kepernyo.blit(kep, (kep_x, kep_y))
			pygame.display.flip()
if __name__ == '__main__':
	pygame.init()
	kepernyo = pygame.display.set_mode((640, 480))
	Jatek().main(kepernyo)

Kurzormozgatók figyelése

jatek.py
import pygame
 
class Jatek(object):
	def main(self, kepernyo):
		clock = pygame.time.Clock()
 
		kep = pygame.image.load('kep.png')
		kep_x = 250
		kep_y = 150
 
		while True:
			clock.tick(30)
 
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and \
						event.key == pygame.K_ESCAPE:
					return
			# ~ kep_x += 10
			key = pygame.key.get_pressed()
			if key[pygame.K_LEFT]:
				kep_x -= 10
			if key[pygame.K_RIGHT]:
				kep_x += 10
			if key[pygame.K_UP]:
				kep_y -= 10
			if key[pygame.K_DOWN]:
				kep_y += 10
 
			kepernyo.fill((200, 200, 200))
			# ~ kepernyo.blit(kep, (100, 100))
			kepernyo.blit(kep, (kep_x, kep_y))
			pygame.display.flip()
if __name__ == '__main__':
	pygame.init()
	kepernyo = pygame.display.set_mode((640, 480))
	Jatek().main(kepernyo)

Egéresemények

  • MOUSEBUTTONDOWN
  • MOUSEBUTTONUP
  • MOUSEMOTION

Jatekos osztály

jatek.py
import pygame
 
class Jatekos(pygame.sprite.Sprite):
	def __init__(self, *groups):
		super(Jatekos, self).__init__(*groups)		
		self.image = pygame.image.load('jatekos.png')
		self.rect = pygame.rect.Rect((320, 240), self.image.get_size())
 
	def update(self):
		key = pygame.key.get_pressed()
		if key[pygame.K_LEFT]:
			self.rect.x -= 10
		if key[pygame.K_RIGHT]:
			self.rect.x += 10
		if key[pygame.K_UP]:
			self.rect.y -= 10
		if key[pygame.K_DOWN]:
			self.rect.y += 10
 
class Jatek(object):
	def main(self, kepernyo):
		clock = pygame.time.Clock()
		sprites = pygame.sprite.Group()
		self.player = Jatekos(sprites)
 
		while True:
			clock.tick(30)
 
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and \
						event.key == pygame.K_ESCAPE:
					return
			sprites.update()
			kepernyo.fill((200, 200, 200))
			sprites.draw(kepernyo)
			pygame.display.flip()
 
if __name__ == '__main__':
	pygame.init()
	kepernyo = pygame.display.set_mode((640, 480))
	Jatek().main(kepernyo)

Háttér

jatek.py
import pygame
 
class Jatekos(pygame.sprite.Sprite):
	def __init__(self, *groups):
		super(Jatekos, self).__init__(*groups)		
		self.image = pygame.image.load('jatekos.png')
		self.rect = pygame.rect.Rect((320, 240), self.image.get_size())
 
	def update(self, dt):
		key = pygame.key.get_pressed()
		if key[pygame.K_LEFT]:
			self.rect.x -= 300 * dt
		if key[pygame.K_RIGHT]:
			self.rect.x += 300 * dt
		if key[pygame.K_UP]:
			self.rect.y -= 300 * dt
		if key[pygame.K_DOWN]:
			self.rect.y += 300 * dt
 
class Jatek(object):
	def main(self, kepernyo):
		clock = pygame.time.Clock()
 
		background = pygame.image.load('hatter.png')
		sprites = pygame.sprite.Group()
		self.player = Jatekos(sprites)
 
		while True:
			dt = clock.tick(30)
 
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and \
						event.key == pygame.K_ESCAPE:
					return
			sprites.update(dt / 1000.)
			# ~ kepernyo.fill((200, 200, 200))
			kepernyo.blit(background, (0, 0))
			sprites.draw(kepernyo)
			pygame.display.flip()
 
if __name__ == '__main__':
	pygame.init()
	kepernyo = pygame.display.set_mode((640, 480))
	Jatek().main(kepernyo)

Finom időzítés

jatek.py
import pygame
 
class Jatekos(pygame.sprite.Sprite):
	def __init__(self, *groups):
		super(Jatekos, self).__init__(*groups)		
		self.image = pygame.image.load('jatekos.png')
		self.rect = pygame.rect.Rect((320, 240), self.image.get_size())
 
	def update(self, dt):
		key = pygame.key.get_pressed()
		if key[pygame.K_LEFT]:
			self.rect.x -= 300 * dt
		if key[pygame.K_RIGHT]:
			self.rect.x += 300 * dt
		if key[pygame.K_UP]:
			self.rect.y -= 300 * dt
		if key[pygame.K_DOWN]:
			self.rect.y += 300 * dt
 
class Jatek(object):
	def main(self, kepernyo):
		clock = pygame.time.Clock()
		sprites = pygame.sprite.Group()
		self.player = Jatekos(sprites)
 
		while True:
			dt = clock.tick(30)
 
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and \
						event.key == pygame.K_ESCAPE:
					return
			sprites.update(dt / 1000.)
			kepernyo.fill((200, 200, 200))
			sprites.draw(kepernyo)
			pygame.display.flip()
 
if __name__ == '__main__':
	pygame.init()
	kepernyo = pygame.display.set_mode((640, 480))
	Jatek().main(kepernyo)

Ütközésfigyelés

jatek.py
import pygame
 
class Jatekos(pygame.sprite.Sprite):
	def __init__(self, *groups):
		super(Jatekos, self).__init__(*groups)		
		self.image = pygame.image.load('jatekos.png')
		self.rect = pygame.rect.Rect((320, 240), self.image.get_size())
 
	def update(self, dt, game):
		last = self.rect.copy()
 
		key = pygame.key.get_pressed()
		if key[pygame.K_LEFT]:
			self.rect.x -= 300 * dt
		if key[pygame.K_RIGHT]:
			self.rect.x += 300 * dt
		if key[pygame.K_UP]:
			self.rect.y -= 300 * dt
		if key[pygame.K_DOWN]:
			self.rect.y += 300 * dt
		for cell in pygame.sprite.spritecollide(self, game.walls, False):
			self.rect = last
 
class Jatek(object):
	def main(self, kepernyo):
		clock = pygame.time.Clock()
 
		background = pygame.image.load('hatter.png')
		sprites = pygame.sprite.Group()
		self.player = Jatekos(sprites)
 
 
		self.walls = pygame.sprite.Group()
		szikla = pygame.image.load('szikla.png')
		for x in range(0, 640, 32):
			for y in range(0, 480, 32):
				if x in (0, 640-32) or y in (0, 480-32):
					wall = pygame.sprite.Sprite(self.walls)
					wall.image = szikla
					wall.rect = pygame.rect.Rect((x, y), wall.image.get_size())
		sprites.add(self.walls)
 
		while True:
			dt = clock.tick(30)
 
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and \
						event.key == pygame.K_ESCAPE:
					return
			sprites.update(dt / 1000., self)
			kepernyo.blit(background, (0, 0))
			sprites.draw(kepernyo)
			pygame.display.flip()
 
if __name__ == '__main__':
	pygame.init()
	kepernyo = pygame.display.set_mode((640, 480))
	Jatek().main(kepernyo)

Gravitáció ugró játékossal

jatek.py
import pygame
 
class Jatekos(pygame.sprite.Sprite):
	def __init__(self, *groups):
		super(Jatekos, self).__init__(*groups)		
		self.image = pygame.image.load('jatekos.png')
		self.rect = pygame.rect.Rect((320, 240), self.image.get_size())
		self.resting = False
		self.dy = 0
 
	def update(self, dt, game):
		last = self.rect.copy()
 
		key = pygame.key.get_pressed()
		if key[pygame.K_LEFT]:
			self.rect.x -= 300 * dt
		if key[pygame.K_RIGHT]:
			self.rect.x += 300 * dt
		# ~ if key[pygame.K_UP]:
			# ~ self.rect.y -= 300 * dt
		# ~ if key[pygame.K_DOWN]:
			# ~ self.rect.y += 300 * dt
 
		if self.resting and key[pygame.K_SPACE]:
			self.dy = -500
		self.dy = min(400, self.dy + 40)
 
		self.rect.y += self.dy * dt
 
		new = self.rect
		self.resting = False
		for cell in pygame.sprite.spritecollide(self, game.walls, False):
			# ~ self.rect = last
			cell = cell.rect
			if last.right <= cell.left and new.right > cell.left:
				new.right = cell.left
			if last.left >= cell.right and new.left < cell.right:
				new.left = cell.right
			if last.bottom <= cell.top and new.bottom > cell.top:
				self.resting = True
				new.bottom = cell.top
				self.dy = 0
			if last.top >= cell.bottom and new.top < cell.bottom:
				new.top = cell.bottom
				self.dy = 0
 
class Jatek(object):
	def main(self, kepernyo):
		clock = pygame.time.Clock()
 
		background = pygame.image.load('hatter.png')
		sprites = pygame.sprite.Group()
		self.player = Jatekos(sprites)
 
 
		self.walls = pygame.sprite.Group()
		szikla = pygame.image.load('szikla.png')
		for x in range(0, 640, 32):
			for y in range(0, 480, 32):
				if x in (0, 640-32) or y in (0, 480-32):
					wall = pygame.sprite.Sprite(self.walls)
					wall.image = szikla
					wall.rect = pygame.rect.Rect((x, y), wall.image.get_size())
		sprites.add(self.walls)
 
		while True:
			dt = clock.tick(30)
 
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and \
						event.key == pygame.K_ESCAPE:
					return
			sprites.update(dt / 1000., self)
			kepernyo.blit(background, (0, 0))
			sprites.draw(kepernyo)
			pygame.display.flip()
 
if __name__ == '__main__':
	pygame.init()
	kepernyo = pygame.display.set_mode((640, 480))
	Jatek().main(kepernyo)

PyGame és OpenGL

Telepítés

pip3 install pygame

Szükséges még a OpenGL:

pip3 install pyopengl pyopengl-accelerate

Ellenőrzés

Indítsuk el az IDLE programot. Mondjuk írjuk be parancssorba:

idle

Majd python értelmezőbe írjuk be:

import pygame
import OpenGL

Ha nem kapunk hibaüzenetet, akkor rendben telepítve van.

Kezdés

Egy tipikus PyGame programot így szokás kezdeni:

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

Importáljuk a pygame modult és a pygame.locals-t. Ezt követően a OpenGL.GL, és az OpenGL.GLU modulokra lesz szükségünk.

Vonal

line.py
#!/usr/bin/python3
 
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
 
 
def Haromszog():
	glBegin(GL_LINES)
	glVertex3f(-0.5, 0.5, 0)
	glVertex3f(0.5, 0, 0)
	glVertex3f(-0.5, -0.5, 0)
	glEnd()
 
def main():
	pygame.init()
	display = (800, 600)
	pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
	gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
	glTranslatef(0.0, 0.0, -5)
	while True:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				pygame.quit()
				quit()
		glRotatef(1, 3, 1, 1)
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
		Haromszog()
		pygame.display.flip()
		pygame.time.wait(10)
 
main()

Források

oktatas/programozas/python/pygame.txt · Utolsó módosítás: 2023/08/21 20:56 szerkesztette: admin