2 Star 0 Fork 0

孙启哲 / 弹球

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
game_functions.py 4.53 KB
一键复制 编辑 原始数据 按行查看 历史
sunsh 提交于 2019-03-31 07:50 . 改进版
import pygame as pg
from marbles import Ball
from marbles import Black_ball
# create screen
bg = [255, 255, 0]
pg.init()
screen = pg.display.set_mode([640, 480])
screen.fill(bg)
def move_paddle(paddle, x):
paddle.centerx = x
heart = pg.image.load('h.png')
width, height = heart.get_size()
heart = pg.transform.smoothscale(heart, (width // 2, height // 2))
text_x = 0
text_y = 0
ball = [100, 100]
ball22 = [200, 200]
ball33 = [300, 300]
ball44 = [400, 400]
speed = [1, 1]
speed22 = [1, 1]
speed33 = [1, 1]
speed44 = [1, 1]
line_width = 0
circle_radius = 10
running = True
paddle = pg.Rect(280, 400, 100, 20)
flag2 = True # if the game is not over
clock = pg.time.Clock()
def quit_game():
pg.quit()
quit()
def game_intro():
intro = True
while intro:
# input listener
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()
# set background color
screen.fill(bg)
# draw title
large_text = pg.font.Font(None, 115)
text_surf, text_rect = text_objects('Pinball game', large_text)
text_rect.center = ((640 / 2), (480 / 2))
screen.blit(text_surf, text_rect)
# draw two buttons
button("GO", 150, 350, 100, 50, [0, 255, 0], [0, 200, 0], game_loop)
button("Quit", 350, 350, 100, 50, [255, 0, 0], [200, 0, 0], quit_game)
pg.display.update()
clock.tick(15)
def game_loop():
global running, flag2
global speed33, speed, speed22
while running:
ball1 = Ball.Ball(ball, speed, circle_radius, screen)
if Ball.score == 10:
ball2 = Ball.Ball(ball22, speed22, circle_radius, screen)
Ball.isBall2 = True
if Ball.score == 20:
ball3 = Ball.Ball(ball33, speed33, circle_radius, screen)
Ball.isBall3 = True
if Ball.score >= 20:
for i in range(3):
screen.blit(heart, (300 + width * i, 0))
pg.display.update()
if Ball.score == 30:
blball = Black_ball.BlackBall(ball44, speed44, circle_radius, screen)
Black_ball.isBlball = True
x, y = pg.mouse.get_pos()
# move
if flag2:
move_paddle(paddle, x)
ball1.move_ball(paddle)
if Ball.isBall2:
ball2.move_ball(paddle)
if Ball.isBall3:
ball3.move_ball(paddle)
print('Draw')
if Black_ball.isBlball:
blball.move_ball(paddle)
# update the ball
screen.fill([255, 255, 0])
ball1.draw([255, 0, 255])
if Ball.isBall2:
ball2.draw([255, 0, 255])
if Ball.isBall3:
ball3.draw([255, 0, 255])
if Black_ball.isBlball:
blball.draw()
# update the paddle
pg.draw.rect(screen, [0, 255, 255], paddle)
# update the score
font = pg.font.Font(None, 30)
text = font.render('Your score: ' + str(Ball.score), True, [0, 0, 0])
screen.blit(text, [0, 0])
# if game is over
if Ball.flag1:
font1 = pg.font.Font(None, 50)
text1 = font1.render('Game over, Click here to restart', True, [0, 0, 0])
screen.blit(text1, [0, 240])
flag2 = False
Ball.isBall2 = False
Ball.isBall3 = False
pg.display.flip()
# input listener
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if event.type == pg.MOUSEBUTTONDOWN:
xx, yy = pg.mouse.get_pos()
if 0 < xx < 550 and 240 < yy < 280:
Ball.flag1 = False
flag2 = True
def text_objects(text, font):
text_surface = font.render(text, True, [0, 0, 0])
return text_surface, text_surface.get_rect()
def button(msg, x, y, w, h, ic, ac, action=None):
# the position
mouse = pg.mouse.get_pos()
# if pressed
click = pg.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pg.draw.rect(screen, ac, (x, y, w, h))
if click[0] == 1 and action is not None:
# do the thing
action()
else:
pg.draw.rect(screen, ic, (x, y, w, h))
# write message
small_text = pg.font.Font(None, 20)
text_surf, text_rect = text_objects(msg, small_text)
text_rect.center = ((x + (w / 2)), (y + (h / 2)))
screen.blit(text_surf, text_rect)
Python
1
https://gitee.com/sunshawn/marbles.git
git@gitee.com:sunshawn/marbles.git
sunshawn
marbles
弹球
master

搜索帮助