Pygame簡單應用 1

2021-08-30 18:37:33 字數 3262 閱讀 2052

前幾天趁著空閒一點,myeclpse又跑的慢,懶的開,就開始琢磨python了。簡單的學習了一下pygame。仿照著例子編了點**。

這段**的功能就是控制綠的方塊去碰撞白色的方塊,撞到一起的時候,白色方塊會消失,綠色方塊會變大,繼而在另外乙個隨機的地方出現乙個白色的方 塊,等於貪吃蛇的雛形了。如果再加上點複雜邏輯,不是讓綠色方塊簡單的變大,而是形成乙個方塊的列表,在方塊的「頭」移動時,判斷方向,可以轉彎,就可以 實現貪吃蛇的效果了。

附上**:

import pygame,sys,random

from pygame.locals import *

pygame.init()

mainclock = pygame.time.clock()

# set up the window

windowwidth = 400

windowheight = 400

windowsu***ce = pygame.display.set_mode((windowwidth,windowheight),0,32)

pygame.display.set_caption('test')

# set up the colors

black = (0, 0, 0)

green = (0, 255, 0)

white = (255, 255, 255)

# set up movement variables

moveleft = false

moveright = false

moveup = false

movedown = false

movespeed = 5

# set up the nake and food data structure

foodsize = 15

player = pygame.rect(300, 100, 15, 15)

food = pygame.rect(random.randint(0, windowwidth - foodsize), random.randint(0, windowheight - foodsize), foodsize, foodsize)

while true:

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.type == keyup:

if event.key == k_escape:

pygame.quit()

sys.exit()

if event.key == k_left or event.key == ord('a'):

moveleft = false

if event.key == k_right or event.key == ord('d'):

moveright = false

if event.key == k_up or event.key == ord('w'):

moveup = false

if event.key == k_down or event.key == ord('s'):

movedown = false

# move the player

if movedown and player.bottom < windowheight:

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 < windowwidth:

player.right += movespeed

# draw the black background onto the su***ce

windowsu***ce.fill(black)

# check if the player has intersected with any food squares.

if player.colliderect(food):

temp = food

food = pygame.rect(random.randint(0, windowwidth - foodsize), random.randint(0, windowheight - foodsize), foodsize, foodsize)

while temp==food:

food = pygame.rect(random.randint(0, windowwidth - foodsize), random.randint(0, windowheight - foodsize), foodsize, foodsize)

player.height = player.height+15

# draw the player onto the su***ce

pygame.draw.rect(windowsu***ce, green, player)

pygame.draw.rect(windowsu***ce, white, food)

pygame.display.update()

mainclock.tick(50)

效果圖見附件,乙個是初始時的,乙個是吃了幾個白色方塊後的。

Python日記 5 Pygame簡單應用 1

終於把枯燥的基礎知識過了一遍了,準備按照書本的引導,結合pygame的庫做乙個alien invasion小遊戲。雖然很早就接觸程式設計,但是卻始終談不上乙個真正的程式設計師,不管是在專業儲備還是髮量上。我感覺學習過程就是從 script kid 到 coder 再到 programmer 最後可能...

pygame螢幕繪製(1)

這是學習嵩天老師的課程,習得並引以為筆記。pygame提供有乙個pygame.display.set mode可以設定遊戲螢幕的尺寸以及相關設定,pygame.display.set mode r 0,0 flags 0 其中r是遊戲螢幕的解析度,採用 width,height 方式輸入 flags...

django tutorial 簡單應用(1)

實現乙個公共 可以讓人們瀏覽投票並且在上邊投票,管理員介面可以讓你新增,改變刪除投票 略過檢視django版本和安裝 django admin.py startproject myvote 建立乙個工程目錄 myvote 乙個django設定和和例項的集合,包括資料庫配置,django特殊選項,和特...