0% found this document useful (0 votes)
7 views2 pages

Temp

This document defines a Game class that initializes and runs a 2D platformer game using Pygame. The Game class loads sprite images, initializes the map, handles player input and movement, checks for collisions with the map, and updates the display. The main functions are __init__() to set up the game, running() as the main game loop, check() to detect collisions, and move() to handle player movement and input each frame.

Uploaded by

22119054
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Temp

This document defines a Game class that initializes and runs a 2D platformer game using Pygame. The Game class loads sprite images, initializes the map, handles player input and movement, checks for collisions with the map, and updates the display. The main functions are __init__() to set up the game, running() as the main game loop, check() to detect collisions, and move() to handle player movement and input each frame.

Uploaded by

22119054
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pygame

from maps import map

class game:
def __init__(self) -> None:
pygame.init()

#nguoi choi
self.pos=[280,0]
self.vec=pygame.math.Vector2(0,-2)
self.time_air=0
self.s=False
self.img=(pygame.transform.scale(pygame.image.load('sprites/img5.png'),
(16,22)))
#map
self.map=map()

self.run=True

self.scroll=[250,140]
self.screen=pygame.display.set_mode((600,360))
self.fps=pygame.time.Clock()

def running(self) -> None:


while self.run:
for event in pygame.event.get():
if event.type==pygame.QUIT:
self.run=False
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_a:
self.vec[0]=-1
elif event.key==pygame.K_d:
self.vec[0]=1
if event.key==pygame.K_SPACE and self.s==True:
self.time_air=20
if event.type==pygame.KEYUP:
if event.key==pygame.K_a:
self.vec[0]=0
elif event.key==pygame.K_d:
self.vec[0]=0

self.screen.fill((100,255,150))
#draw_map di chuyen map ve map

self.move(self.check(self.map.draw_map(self.screen,self.pos[0]*0.8,self.pos[1]*0.8)
,pygame.Rect((280-2+self.pos[0]*0.2,150+self.pos[1]*0.2,20,26))),pygame.Rect((280-
2+self.pos[0]*0.2,150+self.pos[1]*0.2,20,26)))

self.screen.blit(self.img,(280-
2+self.pos[0]*0.2,150+self.pos[1]*0.2))

self.fps.tick(60)
pygame.display.update()

def check(self,rect,player_rect) -> []:


temp=[]
self.s=False
for i in rect:
if player_rect.colliderect(i) and (player_rect.bottom-2==i.top or
player_rect.bottom-4==i.top):
self.s=True
elif player_rect.colliderect(i) and player_rect.bottom-2!=i.top:
temp.append(i)
if player_rect.colliderect(i) and (player_rect.top+4==i.bottom or
player_rect.top+2==i.bottom):
self.time_air=0
self.s=False
if self.s==False and self.time_air==0:
self.pos[1]+=4
return temp

def move(self,rect,player_rect) -> None:


if self.time_air !=0 :
self.pos[1]+=self.vec[1]*2
self.time_air-=1
dic={'left':False,'right':False}
for i in rect:
if player_rect.right -2 == i.left:
dic['right']=True
if player_rect.left +2 == i.right:
dic['left']=True
if dic['right']==False and self.vec[0]>0:
self.pos[0]+=self.vec[0]*2
if dic['left']==False and self.vec[0]<0:
self.pos[0]+=self.vec[0]*2

if __name__=='__main__':
run=game()
run.running()

You might also like