0

How could I make the player loop around to the other side (similar to how Pacman would move from one side of the map to the other)? also, I would like to add some sound effects for when I shoot bullets, but I have found it rather difficult to find anything in depth about Pygame for 3.6. (this might need to be reformatted, i manually spaced the code so it would post)

import pygame, time, os, random
from pygame.locals import
pygame.init()
bcolor = pygame.Color("red")
 surfacex = 400  # Surface X size
 surfacey = 400  # Surface Y size
 surface = pygame.display.set_mode([surfacex, surfacey])
 surface.fill(bcolor)

  move_direction = 0
  enemy_direction = 0


  class obj_player(object):
   def __init__(self):
    self.rect = pygame.Rect(200, 350, 16, 16)

def move(self, dir):
    if dir == 0 and self.rect.x <= surfacex - 16:
        self.rect.x += 5
    if dir == 1 and self.rect.x >= 0:
        self.rect.x -= 5


 class shoot_bullet(object):
def __init__(self):
    list_bullets.append(self)
    self.rect = pygame.Rect(player.rect.x + 8, 350, 4, 8)

def update(self):
    self.rect.y -= 5

    for enemy in list_enemies:
        if self.rect.colliderect(enemy.rect):
            list_bullets.remove(self)
            list_enemies.remove(enemy)


  class obj_enemy(object):
    def __init__(self, pos):
    list_enemies.append(self)
       self.rect = pygame.Rect(pos[0], pos[1], 16, 16)

def update(self, dir):
    if dir == 0:
        self.rect.x += 2
    if dir == 1:
        self.rect.x -= 2


   player = obj_player()
   clock = pygame.time.Clock()

   can_shoot = True

   list_bullets = []
   list_enemies = []

  pygame.time.set_timer(USEREVENT + 1, 2000)

  for yy in range(5):
   for xx in range(5):
    obj_enemy((0 + 35 * xx, 50 + 35 * yy))

 while True:
clock.tick(60)
surface.fill([0, 0, 0])

if pygame.event.peek(pygame.QUIT):
    pygame.display.quit()
    quit()

key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
    move_direction = 1
if key[pygame.K_RIGHT]:
    move_direction = 0
if key[pygame.K_SPACE] and can_shoot == True:
    can_shoot = False
    shoot_bullet()
if key[pygame.K_SPACE] == False and can_shoot == False:
    can_shoot = True
player.move((move_direction))

for bullet in list_bullets:
    bullet.update()
    pygame.draw.rect(surface, (46, 222, 16), bullet.rect)

for enemy in list_enemies:
    enemy.update(enemy_direction)
    pygame.draw.rect(surface, (46, 222, 16), enemy.rect)

pygame.draw.rect(surface, (0, 255, 255), player.rect)
pygame.display.flip()

   for event in pygame.event.get():
    if event.type == USEREVENT + 1:
        if enemy_direction == 0:
            enemy_direction = 1
        else:
            enemy_direction = 0
   if len(list_enemies) == 0:
    print("YOU WIN")
    break
  pygame.display.quit()
  • @tigerhawkT3 - that whole formatting stuff when i tried to copy and paste it was kinda weird, i used their formatting guide and i still wasn't able to upload it. i'm really new to using this website so bear with me lmfao – Joshua Garcia Mar 10 '17 at 04:39
  • Paste it in, select all your code, and hit the "code block" button to indent it all by four spaces, which formats it as code. As it is, no one's going to want to manually retype your code just to run it or otherwise work with it. – TigerhawkT3 Mar 10 '17 at 04:40
  • @TigerhawkT3 fixed it, thanks – Joshua Garcia Mar 10 '17 at 04:51

2 Answers2

0

The current obj_player.move method simply checks whether there's room to move in the current direction, and moves if there is. For each direction, you need to add behavior that moves the player to the other side of the screen when there's no room to move in the current direction.

def move(self, direction):
    if direction:
        if self.rect.x:
            self.rect.x -= 5
        else:
            self.rect.x = surfacex-20
    else:
        if self.rect.x <= surfacex-16:
            self.rect.x += 5
        else:
            self.rect = 0
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

Add this to your move() method

if self.rect.x > surfacex:
    self.rect.x = 0

if self.rect.right < 0:
    self.rect.right = surfacex

if self.rect.y > surfacey:
    self.rect.y = 0

if self.rect.bottom < 0:
    self.rect.bottom = surfacey

this means: If your player is out of the screen "move" him to the opponent side.

Jan Meisel
  • 259
  • 3
  • 11