Pygame:只有一部分圈子在吃东西

时间:2015-06-21 18:47:59

标签: python pygame

我试图让一个圆圈在接触它时摧毁它。然后它会增长一点。最终,在一定的尺寸下,我注意到圆圈内只有一个正方形区域正在吃它。

import pygame, sys, random
from pygame.locals import *

# set up pygame
pygame.init()
mainClock = pygame.time.Clock()

# set up the window
windowW = 800
windowH = 600
theSurface = pygame.display.set_mode((windowW, windowH), 0, 32)
pygame.display.set_caption('')

basicFont = pygame.font.SysFont('calibri', 36)

# set up the colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
size = 10
playercolor = BLACK
# set up the player and food data structure
foodCounter = 0
NEWFOOD = 35
FOODSIZE = 10
player = pygame.draw.circle(theSurface, playercolor, (60, 250), 40)
foods = []
for i in range(20):
    foods.append(pygame.Rect(random.randint(0, windowW - FOODSIZE), random.randint(0, windowH - FOODSIZE), FOODSIZE, FOODSIZE))

# set up movement variables
moveLeft = False
moveRight = False
moveUp = False
moveDown = False

MOVESPEED = 10.3

score = 0
# run the game loop
while True:
    # check for events
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            # change the keyboard variables
            if event.key == K_LEFT or event.key == ord('a'):
                moveRight = False
                moveLeft = True
            if event.key == K_RIGHT or event.key == ord('d'):
                moveLeft = False
                moveRight = True
            if event.key == K_UP or event.key == ord('w'):
                moveDown = False
                moveUp = True
            if event.key == K_DOWN or event.key == ord('s'):
                moveUp = False
                moveDown = True
            if event.key == ord('x'):
                player.top = random.randint(0, windowH - player.windowH)
                player.left = random.randint(0, windowW - player.windowW)
            if event.key == K_SPACE:
                pygame.draw.circle(theSurface, playercolor,(player.centerx,player.centery),int(size/2))
                pygame.draw.circle(theSurface, playercolor,(player.centerx+size,player.centery+size),int(size/2))      
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
            if event.key == K_LEFT:
                moveLeft = False
            if event.key == K_RIGHT:
                moveRight = False
            if event.key == K_UP:
                moveUp = False
            if event.key == K_DOWN:
                moveDown = False


        if event.type == MOUSEBUTTONUP:
            foods.append(pygame.Rect(event.pos[0], event.pos[1], FOODSIZE, FOODSIZE))

    foodCounter += 1
    if foodCounter >= NEWFOOD:
        # add new food
        foodCounter = 0
        foods.append(pygame.Rect(random.randint(0, windowW - FOODSIZE), random.randint(0, windowH - FOODSIZE), FOODSIZE, FOODSIZE))
    if 100>score>50:
        MOVESPEED = 9
    elif 150>score>100:
        MOVESPEED = 8
    elif 250>score>150:
        MOVESPEED = 6
    elif 400>score>250:
        MOVESPEED = 5
    elif 600>score>400:
        MOVESPEED = 3
    elif 800>score>600:
        MOVESPEED = 2
    elif score>800:
        MOVESPEED = 1
    # move the player
    if moveDown and player.bottom < windowH:
        player.top += MOVESPEED
    if moveUp and player.top > 0:
        player.top -= MOVESPEED
    if moveLeft and player.left > 0:
        player.left -= MOVESPEED
    if moveRight and player.right < windowW:
        player.right += MOVESPEED
    theSurface.blit(bg, (0, 0))

    # draw the player onto the surface
    pygame.draw.circle(theSurface, playercolor, player.center, size)

    # check if the player has intersected with any food squares.
    for food in foods[:]:
        if player.colliderect(food):
            foods.remove(food)
            size+=1
            score+=1

    # draw the food
    for i in range(len(foods)):
        pygame.draw.rect(theSurface, GREEN, foods[i])

    pygame.display.update()
    # draw the window onto the theSurface
    pygame.display.update()
    mainClock.tick(80)

我怎样才能让整个圈子能够吃掉周围的东西?

1 个答案:

答案 0 :(得分:1)

您将player在开头设置为初始大小的圆圈,然后您再也不会更新它。当您重新绘制播放器时,您只需绘制一个新尺寸的新圆圈。您需要将新绘制的圈子重新分配给player变量。您可能还需要将旧player中的一些数据复制到新播放器中,但我并不是在pygame上超级好,所以我不确定哪些属性(如果有的话) center(如leftx=[(('hijacked', 'negative'), 302), (('left', 'negative'), 88), (('one', 'negative'), 79), (('befo', 'negative'), 72), (('radical', 'negative'), 70), (('tcot', 'negative'), 70), (('tanker', 'negative'), 55), (('like', 'negative'), 47), (('right', 'negative'), 36), (('oil', 'negative'), 26), (('hijacked', 'positive'), 22), (('pirates', 'negative'), 21), (('people', 'negative'), 21), (('terrorist', 'negative'), 21), (('loony', 'negative'), 18), (('morons', 'negative'), 18), (('get', 'negative'), 17), (('account', 'negative'), 12), (('flag', 'negative'), 11), (('time', 'negative'), 10), (('angry', 'negative'), 9), (('like', 'positive'), 8), (('day', 'negative'), 8), (('know', 'negative'), 8), (('dem', 'negative'), 7), (('tanker', 'positive'), 7), (('long', 'negative'), 6), (('left', 'positive'), 6), (('driver', 'negative'), 6), (('one', 'positive'), 6), (('got', 'negative'), 6), (('tcot', 'positive'), 5), (('radical', 'positive'), 5), (('befo', 'positive'), 5), (('pirates', 'positive'), 3), (('live', 'negative'), 3), (('oil', 'positive'), 2), (('time', 'positive'), 2), (('pay', 'negative'), 2), (('none', 'positive'), 1), (('people', 'positive'), 1), (('morons', 'positive'), 1), (('long', 'positive'), 1), (('account', 'positive'), 1), (('mt', 'negative'), 1), (('got', 'positive'), 1), (('day', 'positive'), 1), (('loony', 'positive'), 1), (('know', 'positive'), 1), (('fathers', 'negative'), 1)] newd=dict(x) for i in newd.keys(): if i[1]=="negative": z=list(i) z[1]="positive" z=tuple(z) if z in newd.keys(): if newd[z]<newd[i]: print i )由pygame处理,您自己创建。