2017-12-26 7 views
-3

나는 겨울 방학 동안 파이 게임 (Pygame)을위한 학교 프로젝트를 총재로하고있다. 나는 게임 (Flappy Bird)의 기초를 만들었지 만, 문제는 충돌이 제대로 작동하지 않는다는 것입니다. 문제는 파이프를 통해 충돌하는 것으로 간주된다는 것입니다.누군가 내 파이 게임 충돌 코드를 수정하겠습니까?

내 코드를 확인하고 고쳐 주시겠습니까?

import pygame 
import sys 
import math 
import random 
from pygame.locals import * 
a=320 

b=240 
da=1 
db=0 
x=25 
y=25 
e=50 
da2=0 
c=200 
d=10 
c_change=0 
d_change=0 
clock = pygame.time.Clock() 
bg=(36,38,82) 
wood=(253,197,136) 
green=(79, 255, 101) 
pipe=(152,228,86) 
end=(137, 226, 57) 
bg1=(40,42,86) 
gold=(219,178,58) 
golden=(254, 197, 34) 
golder=(255, 206, 63) 
black=(0,0,0) 
red=(255, 47, 47) 
white=(255,255,255) 
pygame.init() 
screen = pygame.display.set_mode((1400,700)) 
class Wall(pygame.sprite.Sprite): 

    def __init__(self, x, y, width, height): 
     super().__init__() 


     self.image = pygame.Surface([width, height]) 
     self.image.fill(GREY) 


     self.rect = self.image.get_rect() 
     self.rect.y = y 
     self.rect.x = x 

crashFlag=0 

done = False 
while not done: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
     if event.type==pygame.KEYDOWN: 
      if event.key==pygame.K_SPACE: 
       c_change=-1 
       d_change=1 
     if event.type==pygame.KEYUP: 
      if event.key==pygame.K_SPACE: 
       c_change=1 
     if event.type==pygame.KEYDOWN: 
      crashFlag==0 


    c+=c_change 
    d+=d_change 

    screen.fill(bg) 
    pygame.draw.rect(screen,bg1, (0,350,1400,350), 0) 


    pygame.draw.circle(screen,white, (d,c),5) 
    #for f in range(195, 390 ,5): 
    if d>200 and d<275: 
     if c>340: 
      crashFlag=1 

    pygame.draw.rect(screen,end, (195,300,80,40), 0) 
    pygame.draw.rect(screen,pipe, (200,0,70,300), 0) 
    pygame.draw.rect(screen,pipe, (350,0,70,450), 0) 
    pygame.draw.rect(screen,end, (345,420,80,40), 0) 
    pygame.draw.rect(screen,pipe, (490,0,70,480), 0) 
    pygame.draw.rect(screen,end, (485,480,80,40), 0) 
    pygame.draw.rect(screen,pipe, (630,0,70,450), 0) 
    pygame.draw.rect(screen,end, (625,450,80,40), 0) 
    pygame.draw.rect(screen,pipe, (770,0,70,430), 0) 
    pygame.draw.rect(screen,end, (765,420,80,40), 0) 
    pygame.draw.rect(screen,pipe, (910,0,70,400), 0) 
    pygame.draw.rect(screen,end, (905,400,80,40), 0) 
    pygame.draw.rect(screen,pipe, (1050,0,70,470), 0) 
    pygame.draw.rect(screen,end, (1045,470,80,40), 0) 
    pygame.draw.rect(screen,pipe, (1190,0,70,430), 0) 
    pygame.draw.rect(screen,end, (1185,430,80,40), 0) 
    pygame.draw.rect(screen,gold, (1330,0,70,410), 0) 
    pygame.draw.rect(screen,golder, (1350,0,70,410), 0) 
    pygame.draw.rect(screen,golden, (1325,410,80,40), 0) 
    pygame.draw.rect(screen,pipe, (200,400,70,240), 0) 
    #lower pipes 
    pygame.draw.rect(screen,end, (195,400,80,40), 0) 
    pygame.draw.rect(screen,pipe, (350,520,70,500), 0) 
    pygame.draw.rect(screen,end, (345,515,80,40), 0) 
    pygame.draw.rect(screen,pipe, (490,570,70,100), 0) 
    pygame.draw.rect(screen,end, (485,570,80,40), 0) 
    pygame.draw.rect(screen,pipe, (630,570,70,100), 0) 
    pygame.draw.rect(screen,end, (625,540,80,40), 0) 
    pygame.draw.rect(screen,pipe, (770,550,70,120), 0) 
    pygame.draw.rect(screen,end, (765,510,80,40), 0) 
    pygame.draw.rect(screen,pipe, (910,530,70,220), 0) 
    pygame.draw.rect(screen,end, (905,490,80,40), 0) 
    pygame.draw.rect(screen,pipe, (1050,560,70,220), 0) 
    pygame.draw.rect(screen,end, (1045,560,80,40), 0) 
    pygame.draw.rect(screen,pipe, (1190,530,70,220), 0) 
    pygame.draw.rect(screen,end, (1185,530,80,40), 0) 
    pygame.draw.rect(screen,gold, (1330,530,70,220), 0) 
    pygame.draw.rect(screen,golder, (1350,530,70,220), 0) 
    pygame.draw.rect(screen,golden, (1325,510,80,40), 0) 
    pygame.draw.rect(screen, wood, (0,650,1400,50), 0) 
    pygame.draw.rect(screen,green, (0,640,1400,10), 0) 

    if crashFlag==1: 
     pygame.draw.rect(screen,white, (0,0,1400,700), 0) 
     font = pygame.font.SysFont("Berlin Sans FB Demi", 100, True, False) 

     text = font.render("You Lost", True, black) 
     screen.blit(text, (500, 100)) 
     pygame.draw.rect(screen,green, (600,400,190,60), 0) 
     font = pygame.font.SysFont("Aharoni", 50, True, False) 

     text = font.render("RESET", True, white) 
     screen.blit(text, (630, 410)) 



    pygame.display.update() 
    clock.tick(150) 
pygame.quit() 
+3

"내 코드를 확인하고 그것을 해결"SO위한 것이 아닙니다. 특정 질문을하면 다른 사람이 디버깅을 도와줍니다. –

+1

'print()'를 사용하여 코드의 어떤 부분이 실행되는지를 나타내는 변수와 메시지에 값을 표시합니다.이 코드를 "인쇄 디버깅"이라고하며 코드에서 무슨 일이 벌어지고 있는지 확인하는 데 도움이됩니다. 때로는 코드가 예상 한대로하거나 변수에 다른 값을 가지고 있지 않습니다. – furas

+0

목록이나 사전을 사용하여 모든 파이프의 위치와 크기를 유지 한 다음'for' 루프를 사용하여 그 파이프를 그리고 충돌을 확인할 수 있습니다. 파이썬은 위치와 크기를 유지하기 위해 [pygame.Rect()] (http://pygame.org/docs/ref/rect.html)를 가지고 있으며'colliderect','collidepoint' 등의 메소드를 가지고 있습니다. 현재 충돌을 어디서 확인했는지 알 수는 없으므로 작동하지 않습니다. – furas

답변

1

모두를 설명하는 데 너무 많은 변경 사항이 있습니다.

나는 (END, pygame.Rect(195,300,80,40))로 목록 all_pipes의 모든 파이프를 유지하고 나는 그들에게

for pipe_color, pipe_rect in all_pipes: 
     pygame.draw.rect(screen, pipe_color, pipe_rect, 0) 

을 그릴과 선수와의 충돌을 확인 for 루프를 사용할 수 있습니다.

for pipe_color, pipe_rect in all_pipes: 
     if pipe_rect.collidepoint(player_x, player_y): 
      state = STATE_GAMEOVER 
      break # no need to check other 

전체 코드는

import pygame 

# --- constants --- (UPPER_CASE_NAMES) 

# - colors - 

BACKGROUND_0 = (36, 38, 82) 
BACKGROUND_1 = (40, 42, 86) 

BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 47, 47) 
GREEN = (79, 255, 101) 

WOOD = (253, 197, 136) 
PIPE = (152, 228, 86) 
END = (137, 226, 57) 
GOLD = (219, 178, 58) 
GOLDEN = (254, 197, 34) 
GOLDER = (255, 206, 63) 

# - states - 

STATE_INTRO = 1 
STATE_GAME  = 2 
STATE_GAMEOVER = 3 

# --- classes --- (CamelCaseNames) 

#class Wall(pygame.sprite.Sprite): 
# 
# def __init__(self, x, y, width, height): 
#  super().__init__() 
# 
#  self.image = pygame.Surface((width, height)) 
#  self.image.fill(GREY) 
# 
#  self.rect = self.image.get_rect() 
#  self.rect.y = y 
#  self.rect.x = x 

# --- functions --- (lower_case_names) 

# empty 

# --- main --- 

# - init - 

pygame.init() 
screen = pygame.display.set_mode((1400,700)) 

font1 = pygame.font.SysFont("Berlin Sans FB Demi", 100, True, False) 
text1 = font1.render("You Lost", True, BLACK) 
font2 = pygame.font.SysFont("Aharoni", 50, True, False) 
text2 = font2.render("RESET", True, WHITE) 

# - objects - 

player_y = 200 
player_x = 10 

x_change = 5 
y_change = 0 

all_pipes = [ 
    #upper pipes 
    (END, pygame.Rect(195,300,80,40)), 
    (PIPE, pygame.Rect(200,0,70,300)), 
    (PIPE, pygame.Rect(350,0,70,450)), 
    (END, pygame.Rect(345,420,80,40)), 
    (PIPE, pygame.Rect(490,0,70,480)), 
    (END, pygame.Rect(485,480,80,40)), 
    (PIPE, pygame.Rect(630,0,70,450)), 
    (END, pygame.Rect(625,450,80,40)), 
    (PIPE, pygame.Rect(770,0,70,430)), 
    (END, pygame.Rect(765,420,80,40)), 
    (PIPE, pygame.Rect(910,0,70,400)), 
    (END, pygame.Rect(905,400,80,40)), 
    (PIPE, pygame.Rect(1050,0,70,470)), 
    (END, pygame.Rect(1045,470,80,40)), 
    (PIPE, pygame.Rect(1190,0,70,430)), 
    (END, pygame.Rect(1185,430,80,40)), 
    (GOLD, pygame.Rect(1330,0,70,410)), 
    (GOLDER, pygame.Rect(1350,0,70,410)), 
    (GOLDEN, pygame.Rect(1325,410,80,40)), 
    (PIPE, pygame.Rect(200,400,70,240)), 
    #lower pipes 
    (END, pygame.Rect(195,400,80,40)), 
    (PIPE, pygame.Rect(350,520,70,500)), 
    (END, pygame.Rect(345,515,80,40)), 
    (PIPE, pygame.Rect(490,570,70,100)), 
    (END, pygame.Rect(485,570,80,40)), 
    (PIPE, pygame.Rect(630,570,70,100)), 
    (END, pygame.Rect(625,540,80,40)), 
    (PIPE, pygame.Rect(770,550,70,120)), 
    (END, pygame.Rect(765,510,80,40)), 
    (PIPE, pygame.Rect(910,530,70,220)), 
    (END, pygame.Rect(905,490,80,40)), 
    (PIPE, pygame.Rect(1050,560,70,220)), 
    (END, pygame.Rect(1045,560,80,40)), 
    (PIPE, pygame.Rect(1190,530,70,220)), 
    (END, pygame.Rect(1185,530,80,40)), 
    (GOLD, pygame.Rect(1330,530,70,220)), 
    (GOLDER, pygame.Rect(1350,530,70,220)), 
    (GOLDEN, pygame.Rect(1325,510,80,40)), 
    (WOOD, pygame.Rect(0,650,1400,50)), 
    (GREEN, pygame.Rect(0,640,1400,10)), 
] 

# - mainloop - 

state = STATE_INTRO # STATE_GAME, STATE_GAMEOVER 

clock = pygame.time.Clock() 
done = False 

while not done: 

    # - events - 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 

     if state == STATE_INTRO: 
      if event.type == pygame.KEYDOWN: 
       state = STATE_GAME 

     elif state == STATE_GAME: 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_SPACE: 
        y_change = -5 
      elif event.type == pygame.KEYUP: 
       if event.key == pygame.K_SPACE: 
        y_change = 5 

     elif state == STATE_GAMEOVER: 
      if event.type == pygame.KEYDOWN: 
       state = STATE_INTRO 
       player_y = 200 
       player_x = 10 

    # - updates (without draws) - 

    if state == STATE_GAME: 
     player_x += x_change 
     player_y += y_change 

     # check collisions with all pipes 

     for pipe_color, pipe_rect in all_pipes: 
      if pipe_rect.collidepoint(player_x, player_y): 
       state = STATE_GAMEOVER 
       break # no need to check other 

    # - draws (without updates) - 

    if state in (STATE_INTRO, STATE_GAME): 
     screen.fill(BACKGROUND_0) 
     pygame.draw.rect(screen, BACKGROUND_1, (0, 350, 1400, 350), 0) 

     # draw all pipes 

     for pipe_color, pipe_rect in all_pipes: 
      pygame.draw.rect(screen, pipe_color, pipe_rect, 0) 

     pygame.draw.circle(screen, WHITE, (player_x, player_y), 5) 

    if state == STATE_GAMEOVER: 
     screen.fill(WHITE) 
     screen.blit(text1, (500, 100)) 
     pygame.draw.rect(screen, GREEN, (600, 400, 190, 60), 0) 
     screen.blit(text2, (630, 410)) 

    pygame.display.update() 
    clock.tick(25) 

# - end - 

pygame.quit() 
+0

내 문제와 코드를 수정 해 주셔서 감사합니다. 어떻게 도움이 되었습니까? – 12batman21

+0

BTW : Stackoverflow 포털은 [Code Review] (https://codereview.stackexchange.com/)이며 대부분의 변경 사항은이 포털에서 수행해야합니다. . – furas

+0

Stackoverflow also portal [게임 개발] (https://gamedev.stackexchange.com/) – furas