2017-03-10 5 views
0

어떻게 플레이어를 다른쪽으로 돌릴 수 있습니까 (팩맨이지도의 한쪽에서 다른쪽으로 움직이는 것과 유사)? 또한 총알을 쏘을 때 사운드 효과를 추가하고 싶지만 파이 게임이 3.6에 대해서는 아무 것도 찾을 수 없다는 것을 알게되었습니다. 현재 obj_player.move 방법은 단순히 현재의 방향으로 이동하는 공간이 있는지 여부를 확인하고, 움직임이 있으면내 플레이어가 벽을 치는 대신 레벨 주위를 돌게 만드는 방법은 무엇입니까?

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() 
+0

@ tigerhawkT3 - 복사 및 붙여 넣기를 시도했을 때의 전체 서식 지정 항목이 다소 이상했기 때문에 서식 가이드를 사용했지만 여전히 업로드 할 수 없었습니다. 나는이 웹 사이트를 사용하는 것에 정말 익숙하다. lmfao –

+0

붙여 넣기하고, 코드를 모두 선택하고, "코드 블록"버튼을 눌러 4 개의 공백으로 모두 들여 쓰기하여 코드로 포맷한다. 그렇다면 아무도 코드를 실행하거나 다른 방식으로 작업하기 위해 코드를 수동으로 다시 입력하려고하지 않을 것입니다. – TigerhawkT3

+0

@ TigerhawkT3이 (가) 고쳐주었습니다. –

답변

0

() 메소드

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 

이것이 의미하는 당신의 이동이 추가 : 플레이어가 상대 측에 화면 "이동"그 부족합니다.

0

(이 게시 할 수 있도록이 난 코드를 수동으로 이격 포맷 될 필요가 있음). 각 방향에 대해 현재 방향으로 이동할 공간이 없을 때 플레이어를 화면의 다른면으로 이동시키는 비헤이비어를 추가해야합니다.

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