Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- pygame.init()
- clock = pygame.time.Clock()
- fps = 160
- screen = pygame.display.set_mode((500,500))
- pygame.display.set_caption("Game 2")
- screen.fill((255,255,255))
- class Player(pygame.sprite.Sprite):
- def __init__(self,x,y,width,height):
- pygame.sprite.Sprite.__init__(self)
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- self.right = False
- self.left = False
- self.up = False
- self.down = False
- self.surf = pygame.Surface((50,50))
- self.rect = self.surf.get_rect()
- def draw(self):
- pygame.draw.rect(screen, (0,0,0), (self.x, self.y, self.width, self.height))
- def collision_test(self, block1):
- if pygame.sprite.collide_rect(self, block1):
- print("a collision is detected")
- def move_player():
- if player.right == True:
- player.x += 2
- elif player.left == True:
- player.x -= 2
- elif player.up == True:
- player.y -= 2
- elif player.down == True:
- player.y += 2
- class Block1(pygame.sprite.Sprite):
- def __init__(self,x,y,width,height):
- pygame.sprite.Sprite.__init__(self)
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- self.surf = pygame.Surface((self.width,self.height))
- self.rect = self.surf.get_rect()
- def draw(self):
- pygame.draw.rect(screen, (150,150,150), (self.x, self.y, self.width, self.height))
- player = Player(50,50,50,50)
- block1 = Block1(200, 200, 100, 100)
- def redrawGameWindow():
- screen.fill((255, 255, 255))
- player.draw()
- block1.draw()
- pygame.display.update()
- run = True
- while run:
- clock.tick(fps)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- keys = pygame.key.get_pressed()
- if keys[pygame.K_RIGHT]:
- player.right = True
- player.left = False
- player.up = False
- player.down = False
- elif keys[pygame.K_LEFT]:
- player.left = True
- player.right = False
- player.up = False
- player.down = False
- elif keys[pygame.K_UP]:
- player.up = True
- player.right = False
- player.left = False
- player.down = False
- elif keys[pygame.K_DOWN]:
- player.down = True
- player.right = False
- player.left = False
- player.up = False
- else:
- player.right = False
- player.left = False
- player.up = False
- player.down = False
- move_player()
- player.collision_test()
- redrawGameWindow()
Advertisement
Add Comment
Please, Sign In to add comment