基於pyagme用python做接小球遊戲

2021-10-01 04:12:21 字數 2642 閱讀 1215

如果不會可以看看這個:python安裝工具包教程

下面是程式:

#導入庫

import pygame

import sys

import time

import random

#初始化

pygame.init(

)screen = pygame.display.set_mode(

(600

,500))

# 設定視窗

pygame.display.set_caption(

'猩球大戰'

)ball_x, ball_y = random.randint(50,

550),50

rect_x, rect_y, rect_w, rect_h =0,

500-80,

200,

20down =

true

score =

0font = pygame.font.font(

"ziti.ttf",24

)font2 = pygame.font.font(

"ziti.ttf",50

)xue =

5pygame.mixer.init(

)#事件

game_over =

false

# 載入

# 判斷事件型別是不是退出

pygame.quit(

) sys.exit(

)elif event.

type

== pygame.mousemotion:

rect_x, rect_y = event.pos

rect_x = rect_x -

100 rect_y =

480 screen.fill(

(255

,255,0

))screen.blit(bg,(0

,0))

ball_y +=

1# 是否接到球

if ball_y >

450:

ball_y =

50 ball_x = random.randint(50,

550)

if xue >1:

xue = xue -

1else

: xue =

0 game_over =

true

if game_over:

test3 = font2.render(

"game over!!!"

,true,(

255,0,

0)) screen.blit(test3,

(150

,210))

else

:if ball_y +

50>

480and rect_x +

200> ball_x > rect_x:

hit.play(

) score = score +

1 ball_y =

50 ball_x = random.randint(50,

550)

text1 = font.render(

"分數:%d"

% score,

true,(

0,0,

0)) screen.blit(text1,(0

,0))

text2 = font.render(

"血條:%d"

% xue,

true,(

0,0,

0)) screen.blit(text2,(0

,30))

# 畫圓

pygame.draw.circle(screen,(2

,20,200),

(ball_x, ball_y),50

) screen.blit(boss,

(ball_x, ball_y)

)# 畫矩形

pygame.draw.rect(screen,

(200

,200

,200),

(rect_x, rect_y, rect_w, rect_h)

) time.sleep(

0.01

) pygame.display.update(

)# 重新整理

# 結束

pygame.quit(

)# 退出遊戲

用有效值填充矩陣(基於python)

有乙個300 300的矩陣ary,矩陣中,所有值為 1的地方都是無效值,大於 1的都是有效值。需要用有效值來填充無效值 如i行j列的值是 1,用此點上方的值填充此點 若上方為有效值,則此點的填充完畢 若上方也為 1,則繼續按順時針方向,用右側的值填充 若右側也是 1,以此類推,用下方 左方的值進行填...

基於os庫用python批量重新命名檔案

import os a r c users jrcode02 desktop 123 絕對路徑 一定能能找到 相對路徑 短一些 os.mkdir c users jrcode02 pycharmprojects 123 12345 123456 os.mkdir 12345 1234567 os.c...

用C 實現基於用C 實現基於TCP協議的網路通訊

tcp 協議是乙個基本的網路 協議,基本上所有的網路服務都是基於 tcp協議的,如http,ftp等等,所以要了解網路程式設計就必須了解基於 tcp協議的程式設計。然而 tcp協議是乙個龐雜的體系,要徹底的弄清楚它的實現不是一天兩天的功夫,所幸的是在.net framework環境下,我們不必要去追...