Bob - 对接小球

import pygame
import random
yihaowanjiadefen = 0
erhaowanjiadefen = 0
import time as t
def ball(screen,x,y,down,right,green):
    if down:
        y = y + 1
    else:
        y = y - 1

    if right:
        x += 1
    else:
        x -= 1

    if y > 600 :
        down = False
    if x > 800:
        right  = False
    if 0 > y :
        down = True
    if x < 0:
        right = True

    pygame.draw.circle(screen, (green, 255, 20), (x, y), 50)
    return  x,y,down,right



pygame.init()
screen = pygame.display.set_mode((800, 600))

x, y, down,right = random.randint(0,800),random.randint(0,600),True,True
ban_x,ban_y,ban_k,ban_g = 700,250,100,100
ban_x2,ban_y2,ban_k2,ban_g2 = 0,250,100,100
# 电脑每隔多少毫秒时间响应一次
# 1秒=1000毫秒
pygame.key.set_repeat(1, 1)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        # elif event.type == pygame.MOUSEMOTION:
        #     ban_x, ban_y = event.pos    # pos---> position
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                ban_y -= 5
                if ban_y < 0:
                    ban_y = 0
            if event.key == pygame.K_DOWN:
                ban_y += 5
                if ban_y > 500:
                    ban_y = 600-100
            if event.key == pygame.K_w:
                ban_y2 -= 5
                if ban_y2 < 0:
                    ban_y2 = 0
            if event.key == pygame.K_s:
                ban_y2 += 5
                if ban_y2 > 500:
                    ban_y2 = 600-100



    screen.fill((2, 2, 1))
    x, y, down, right = ball(screen,x,y,down,right,0)
    if x > 700 and y > ban_y and y < ban_y+100:
        yihaowanjiadefen += 1
        print("%s : %s"%(yihaowanjiadefen,erhaowanjiadefen))
        t.sleep(10)
    if x < 100 and y > ban_y2 and y < ban_y2+100:
        erhaowanjiadefen += 1
        print("%s : %s"%(yihaowanjiadefen,erhaowanjiadefen))
        t.sleep(10)
    # px,py,pdown,pright = ball(screen, px,py,pdown,pright,255)
    pygame.draw.rect(screen,(220,0,0),(ban_x,ban_y,ban_k,ban_g))
    pygame.draw.rect(screen,(220,100,0),(ban_x2,ban_y2,ban_k2,ban_g2 ))
    

    pygame.display.update()

pygame.quit()
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读