发新话题
打印

分享] pygame学习第三个游戏代码案例教程,可以总结收藏一下啊!

分享] pygame学习第三个游戏代码案例教程,可以总结收藏一下啊!

#/usr/bin/env python
#coding=utf-8

try:
#Import Modules
import os
import sys
import pygame
import math
from pygame.locals import *
except ImportError, err:
print "couldn't load module. %s" % (err)
sys.exit(2)

if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'

class Point( object ):
def __init__(self, vector):
self.rect = pygame.Rect( 250,250, 0, 0)
self.vector = vector # angle, z

def update(self):
newpos = self.calcnewpos(self.rect,self.vector)
self.rect = newpos

def calcnewpos(self,rect,vector):
[angle,z] = vector
(dx,dy) = (z*math.cos(angle),z*math.sin(angle))
self.vector[0] -= 1 # angle
self.vector[1] += 1 # distance
return rect.move(dx,dy)

if __name__ == "__main__":
pygame.init()

size = width, height = 500,500
screen = pygame.display.set_mode ( size )
background = pygame.image.load('bg.bmp').convert()
screen.blit(background, (0, 0))

ball = Point( [0, 10])

while 1:
for event in pygame.event.get():
if event.type in ( pygame.QUIT,):
sys.exit()
old_rect = ball.rect
ball.update()
pygame.draw.line( pygame.display.get_surface(),
(255,255,255), (old_rect.left,old_rect.top),
(ball.rect.left, ball.rect.top) )
pygame.display.update()
pygame.time.delay(100)
本文源自:ht tp://w w w.c svt.ne t/

TOP

发新话题